用OpenGL绘制Sierpinski的麻烦
Trouble drawing Sierpinski with OpenGL
我正在尝试使用绘制点模式并生成垫圈的函数来生成sierpinski垫圈。
但是,当我编译并运行程序时,它除了黑屏外什么都没有显示。是什么原因引起了问题?
这是我的代码:
#include <Windows.h>
#include <gl/GL.h>
#include <glut.h>
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
class GLintPoint {
public:
GLint x, y;
};
int random(int m) {
return rand() % m;
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(drawDot);
myInit();
glutMainLoop();
}
如果要在白色背景上绘制黑点,则必须清除glClear
背景:
glClear( GL_COLOR_BUFFER_BIT );
注意,glClearColor
设置用于清除视图端口的颜色,但没有清除任何内容。
您的代码应该以某种方式看起来:
void drawDot(GLint x, GLint y)
{
glVertex2i(x, y);
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
glClearColor(1.0, 1.0, 1.0, 1.0); // set up white clear color
glClear( GL_COLOR_BUFFER_BIT ); // clear the back ground (white)
glMatrixMode( GL_MODELVIEW );
glPushMatrix(); // setup model matrix
glScalef( 1.5f, 1.5f, 1.0f ); // scale the point distribution
glColor3f( 0.0f, 0.0f, 0.0f ); // set black draw color
glPointSize( 5.0f ); // set the size of the points
glBegin(GL_POINTS);
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glEnd();
glPopMatrix(); // reset model matrix
glFlush();
}
相关文章:
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 查找奇数平方和时遇到麻烦
- 将双倍转换为 LPARAM 的麻烦
- 麻烦得到提升::记录器编译
- 翻转 ppm 图像C++麻烦
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 搬家turtlebot_gazebo麻烦
- CSUF EPP - 你有什么想法(在添加到链接列表末尾时遇到麻烦)
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 作为一个C++新手,我遇到了麻烦
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 找麻木的麻烦
- MySQL 从注入过渡到参数化时遇到麻烦
- 字符串和双精度的麻烦,等值后再得到一个数字
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 自定义QVariant麻烦
- 串和阵列的麻烦
- 在C 向量中修改对象值的麻烦
- C DirectX 11从第二个顶点缓冲区呈现麻烦
- 用OpenGL绘制Sierpinski的麻烦