我无法在屏幕上显示一个简单的移动三角形,只有当我用glutDisplayFunc(渲染)调用我的函数时才会发生这种情况。如果我像普通调用render()一样调用我的函数,它会很好地显示三角形,但在这种情况下,它不会为我的三角形设置动画。
基本上我有两个问题:
下面是我的主要和显示代码:
void render()
{
currentTime = glutGet(GLUT_ELAPSED_TIME);
glClear(GL_COLOR_BUFFER_BIT);
GLfloat color[] = { (float)sin(currentTime) * 0.5f + 0.5f, (float)cos(currentTime) * 0.5f + 0.5f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);
GLfloat attrib[] = { (float)sin(currentTime) * 0.5f, (float)cos(currentTime) * 0.6f, 0.0f, 0.0f };
glVertexAttrib4fv(0, attrib);
// Use the program object we created earlier for rendering
glUseProgram(rendering_program);
//Draw triangle
glDrawArrays(GL_TRIANGLES,0,3);
glutSwapBuffers();
}
主:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(600,600);
glutInitContextVersion(4,3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Shader");
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
startup();
// render(); // WORKS FINE AND DRAW TRIANGLE
glutDisplayFunc(render); //DOESN'T DRAW TRIANGLE
shutdown();
glutMainLoop();
return 0;
}
我相信着色器编译得很好,代码的那部分没有错误,因为它是绘制三角形而没有glutDisplayFunc();唯一的问题是当我使用glutDisplayFunc(render)时;什么都没画。
我哪里错了?我怎样才能解决上面提到的两个问题?
答案 0 :(得分:2)
glutDisplayFunc
不会呈现自己。它注册了一个在渲染时必须由过剩使用的函数。调用ifsef发生在glutMainLoop()
内的某个地方。由于您在开始渲染过程之前关闭了所有内容,因此在系统尝试渲染时数据不可用。