I am unable to display a simple moving triangle on the screen, and it only happens when I call my function with glutDisplayFunc(render). If I call my function like a normal call render() it display the triangle well but in this case it doesn't animate my triangle.
So basically I have two issues:
- can't draw triangle when call function with glutDisplayFunc(render)
- can't animate triangle when function call like render() but it can draw triangle though.
below is my main and display code:
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();
}
Main:
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;
}
I believe the shaders are compiled well and there is no error in that part of the code because it is drawing triangle without glutDisplayFunc(); and the only problem is when I use glutDisplayFunc(render); nothing is drawn.
Where am I going wrong? And how can I fix the two issues I mentioned above?