0
\$\begingroup\$

I was experimenting with OpenGL ES 2.0 and being new to OpenGL, I was trying to render a simple triangle. But I was shocked to see that, if I do not call SDL_PollEvent(...) after glDrawArrays(...) in the game loop, I see the triangle render on the screen for a split second and then it vanishes altogether ! But, if I call SDL_PollEvent then everything is fine ! Can anyone explain to me the reason for this abnormal behavior???

However, this is the interesting part of my code:

This code works perfectly, if I uncomment the commented block of code:

uint32_t vbo; 
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 
glEnableVertexAttribArray(pos);
glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);

bool run = true;
SDL_Event e;
while (run)
{
    glDrawArrays(GL_TRIANGLES, 0, 3);
  SDL_GL_SwapWindow(window); 

/*while(SDL_PollEvent(&e))
{
      switch(e.type)
      {
           case SDL_QUIT:
                run = false;
                break; 
      }
} */

}

Vertex Shader:

precision mediump float; 
attribute vec3 pos;
void main()
{
    gl_Position = vec4(pos.xyz, 1.0); 
}

Fragment Shader:

precision mediump float; 
void main()
{
      gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); 
}

Every help will be greatly appreciated, Thankyou everyone in advance !

\$\endgroup\$
3
  • 1
    \$\begingroup\$ What platform are you running the code on? \$\endgroup\$
    – Tyyppi_77
    Mar 18 at 12:22
  • \$\begingroup\$ @Tyyppi_77 Android \$\endgroup\$ Mar 18 at 13:55
  • \$\begingroup\$ Not sure if exact reason will make any difference. Program must poll events every so often, otherwise OS may (will) decide that program has hung and must be killed. \$\endgroup\$ Mar 18 at 15:21
2
\$\begingroup\$

This behaviour is normal. Due to internal structure of the SDL, you are required to call SDL_PollEvent or SDL_PumpEvents (it is called internally by SDL_PollEvent) in your main loop so that SDL event subsystem works as expected. Without this call, your loop is stuck infinitely somethere within SDL waiting for the events to be processed.

See details on the SDL_PollEvent wiki page.

Considering your comment the SDL_PumpEvents wiki page mentiones that event pumping should be done on the same thread that initialized the video subsystem. So your intent is, unfortunatelly, not compatible with internal architecture of the SDL either. But event pumping is really extra lightweight stuff, not sure if it's worth it anyway.

\$\endgroup\$
4
  • \$\begingroup\$ Thankyou for your reply! My intention was to run the event processing loop as a separate thread, and let OpenGL be part of the main application. But, that also causes the same problem! \$\endgroup\$ Mar 18 at 14:23
  • 1
    \$\begingroup\$ I've added more information to the answer considering your comment. \$\endgroup\$ Mar 18 at 14:53
  • \$\begingroup\$ Thankyou for your answer!!! It cleared up everything for me!!! :) \$\endgroup\$ Mar 18 at 15:20
  • \$\begingroup\$ I want to render some time consuming graphics using OpenGL. Therefore, the rendering process might crash my application if it's unable to poll events... :) That is why, I thought of doing multi threading, but now I realize that it is also not possible! So, is there any way around this??? :) How can I make the OpenGL rendering separate, from event handling! I attempted almost everything, but, I can't separate out the event handling part from it! :) \$\endgroup\$ Mar 21 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.