Flickering Screen / Questions

Hey,

I’m new to OpenGL and was able to combine SDL2 and OpenGL. The program renders a dark blue screen with a red square on it. The problem is, that the screen is flickering, while running the game loop.

  1. Why is that?
  2. Why aren’t the squares’ sides the same size? It’s not a square really…
  3. I just did write a very basic vertex and fragment shader. I did not write a geometry shader. Does OpenGL just skip this part then?

I uploaded the project. I’m developing on linux platform. There’s a makefile in the main folder. So if you have all the libraries just use “make” command.
Here is the link:

cplusplus-lernen.de/3D_Fighter_Release.tar.gz

Thanks in advance…

Are you using double buffering?

Yes:

#define DOUBLE_BUFFER 1
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, DOUBLE_BUFFER);

Did you set the projection matrix correctly for the size of the window? Are you using the projection matrix correctly (or at all) in the vertex shader?

Yes. A geometry shader is optional.

If you want people to look at your code, it helps if you can reduce the program to a minimal test case which exhibits the problem and either (preferably) include the code in your post, or (failing that), post a link to a pastebin or github (or similar) page where the code can be viewed directly. The likelihood of anyone looking at your code decreases quite sharply as the effort required increases.

Hey,

Nope, I’m not using a matrix at all yet. But that would explain, why the squares’ sides are proportional to the screen resolution.
Here are both fragment and vertex shader:

///////////////////////////////////////////
///////////////////////////////////////////
// Fragment Shader
///////////////////////////////////////////
///////////////////////////////////////////

varying vec4 out_color; // in

void main(void)
{
gl_FragColor = vec4(out_color);
}

and

///////////////////////////////////////////
///////////////////////////////////////////
// Vertex Shader
///////////////////////////////////////////
///////////////////////////////////////////

attribute vec3 in_pos; // in
attribute vec4 in_color; // in
varying vec4 out_color; // out

void main(void)
{
// gl_Position legt die Position für einen Vertex fest

gl_Position = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);

// übergibt die Eingabefarbe an den nächsten Shader

out_color = in_color;
}

And that’s the game loop:

while(SDL_PollEvent(pApp->get_event()) || (exit_code == EXIT_SUCCESS && state != quit))
{
if(exit_code == EXIT_SUCCESS) // von gm_update
{
exit_code = gm_draw(pApp, &state);

if(exit_code == EXIT_SUCCESS)
{
  exit_code = gm_update(pApp, &state);

  // Prozessor messen

  pApp->cpu_set_timestamp_prev();
  pApp->cpu_set_timestamp(SDL_GetTicks());
  pApp->cpu_set_timestep((pApp->cpu_get_timestamp() - pApp->cpu_get_timestamp_prev()) * 0.001);
}

}
}

gm_draw.cpp:

int gm_draw(CApp * pApp, Game_State * pstate)
{
int exit_code = EXIT_SUCCESS;

// Background

glClearColor(0.0, 0.0, 0.11, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(pApp->get_window());

// Spielstatus

switch(*pstate)
{
case load_menu:
{
pApp->draw_model(0);
} break;
case quit:
{

} break;

};

return exit_code;
}

gm_update.cpp:

int gm_update(CApp * pApp, Game_State * pstate)
{
int exit_code = EXIT_SUCCESS;

// Events

event_handler(pApp->get_eventx(), pstate);

// Spielzustände

switch(*pstate)
{
case load_menu:
{

} break;
case quit:
{
  // Shader entladen

  unload_shader(pApp);
} break;

};

return exit_code;
}

I’m not quite sure where in the code the problems might be. so it’s difficult to choose and post the right parts. these are just guesses. If more code is needed, I’ll post it.