0

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:

  1. can't draw triangle when call function with glutDisplayFunc(render)
  2. 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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    You are aware that you're calling `shutdown()` before entering the `glutMainLoop()`, right? There is absolutely no reason why the callback should not be invoked with the code you show us and I bet that if you set a break point in `render()`, the debugger will run into that at least once unless, of course, you unregister the callback again in `shutdown()`... – thokra Aug 26 '16 at 11:45
  • omg I didn't even realize that. ! It works now. Thank you for explaining. – Mirza Nasir Aug 27 '16 at 13:02

1 Answers1

2

glutDisplayFunc does not render itself. It registers a function that should be used by glut when rendering has to be done. The call ifsef happens somewhere inside the glutMainLoop(). Since you are shutting down everything before starting the rendering process, the data is not available when the system tries to render.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • You have explained it really well. I completely understood why I was having that issue now. Thank you for your prompt response its working now :) – Mirza Nasir Aug 27 '16 at 13:03