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\$ Commented Mar 18, 2021 at 12:22
  • \$\begingroup\$ @Tyyppi_77 Android \$\endgroup\$ Commented Mar 18, 2021 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\$ Commented Mar 18, 2021 at 15:21

2 Answers 2

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\$ Commented Mar 18, 2021 at 14:23
  • 1
    \$\begingroup\$ I've added more information to the answer considering your comment. \$\endgroup\$ Commented Mar 18, 2021 at 14:53
  • \$\begingroup\$ Thankyou for your answer!!! It cleared up everything for me!!! :) \$\endgroup\$ Commented Mar 18, 2021 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\$ Commented Mar 21, 2021 at 1:51
0
\$\begingroup\$

Main polling and rendering clean/swap/copy should usually be done in the main thread and specially the polling should be do always, you can async/defer rendering and event processing by:

  • once pulled, you can send events to be processed in other thread to be processed.
  • you can prepare all the geometry and stuff in other thread.
  • even in some platforms you can upload it to gpu from other thread (this is risky, so make sure you/user can disable it without recompiling if problems arise).
  • you can sometimes render over the same framebuffer thru various frames if you do not clean the buffers, however remember double/triple buffering (when you swap) and that some platforms require to clean/swap framebuffers on each pump... so... busted...
  • so instead you can do the same but rendering to a texture over various frames progresively and finally render or use it.
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.