为什么我的 OpenGL 代码不起作用
Why is my OpenGL code not working
我是OpenGL的新手,已经开始关注一本关于这个主题的书。他们展示的第一个程序是谢尔平斯基垫片计划。以下是书中的代码
:#include<GL/glut.h>
#include<stdlib.h>
void myinit()
{
//attributes
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0); //draw in red
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glClear(GL_COLOR_BUFFER_BIT); //clear the window
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++){
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
但是,当我编译程序时,它只显示一个完全充满白色的窗口,没有其他任何东西。为什么上面的代码没有按照应有的方式工作?
将glFlush()
换成glutSwapBuffers()
:
#include<GL/glut.h>
void display()
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT); //clear the window
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glColor3f(1.0,0.0,0.0); //draw in red
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++)
{
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
glFlush()
实际上不会交换GLUT_DOUBLE
请求的后/前缓冲区。 为此,您需要glutSwapBuffers()
。
相关文章:
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么这段代码不起作用,我该如何解决?
- 为什么这些完全相似的代码不起作用?
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 代码在Visual Studio 2017中不起作用,但在VS代码中工作
- std::string == 运算符在代码中不起作用
- C++ 多态性在代码::块 17.12 中不起作用
- 为什么 pop_back() 在此代码中不起作用?
- 注释一行使代码工作,而没有它,代码不起作用
- C++ 程序在代码块 IDE 中不起作用
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 在 directx11 代码中更改全屏模式下的刷新率不起作用
- 为什么给定的代码不适用于所有测试用例?为什么当输入为 7786 时它不起作用?
- cpp / c ++中的grpc客户端代码,元数据x-api-key/x-goog-api-key不起作用,给了我语音A
- C ++函数重新定义(代码不起作用 - 逻辑错误)
- 试图找到一个数字的平方根,但代码不起作用。C++