OpenGL couldn't start

Hey,

I’m developing a small OpenGL Application on Ubuntu 14.04. SDL2 aswell (combined)
I installed all libraries.
glxinfo: OpenGL version string: 3.0 Mesa 10.5.9
graphic card: VGA compatible controller [0300]: NVIDIA Corporation GF119 [GeForce GT 520]

Problem: OpenGL is not starting correctly. I wrote an error log function, displaying everything i wanna know in a html file.
Result:

red size: 0
green size: 0
blue size: 0
alpha size: 0
doublebuffer: 0
profile mask: 1
major version: 3
minor version: 2
depth size: 0
SDL Error:
Open GL Error: 0

So SDL_GL_SetAttribute failes for all values, which differs. (Used SDL_GL_GetAttribute for that)
And as you can see, no SDL Error or any GL Error (I guess 0 means everything is okay)

Here is the Code:

///////////////////////////////////////////
///////////////////////////////////////////
// Konstanten
///////////////////////////////////////////
///////////////////////////////////////////

#define RED_BIT_SIZE 8
#define GREEN_BIT_SIZE 8
#define BLUE_BIT_SIZE 8
#define ALPHA_BIT_SIZE 0
#define MAJOR_VERSION 3
#define MINOR_VERSION 2
#define DOUBLE_BUFFER 1
#define DEPTH_SIZE 24 // ? Funktioniert nicht auf 24…
#define GL3_PROTOTYPES 1 // exclude deprecated functions / core only

int CApp::set_ogl_attr()
{
int exit_code = ERR_EN_NO_ERROR;

// Setzen

if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, RED_BIT_SIZE) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, GREEN_BIT_SIZE) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, BLUE_BIT_SIZE) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, ALPHA_BIT_SIZE) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, DOUBLE_BUFFER) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, MAJOR_VERSION) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, MINOR_VERSION) == 0)
{
if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, DEPTH_SIZE) != 0)
{exit_code = ERR_EN_OGL_DEPTHSIZE;}
}
else
{exit_code = ERR_EN_OGL_MINORVERSION;}
}
else
{exit_code = ERR_EN_OGL_MAJORVERSION;}
}
else
{exit_code = ERR_EN_OGL_CONTEXT_PROFILE_MASK;}
}
else
{exit_code = ERR_EN_OGL_DOUBLEBUFFER;}
}
else
{exit_code = ERR_EN_OGL_ALPHA_SIZE;}
}
else
{exit_code = ERR_EN_OGL_BLUE_SIZE;}
}
else
{exit_code = ERR_EN_OGL_GREEN_SIZE;}
}
else
{exit_code = ERR_EN_OGL_RED_SIZE;}

// prüfen

if(exit_code == ERR_EN_NO_ERROR)
{exit_code = this->check_ogl_attributes();}

return exit_code;
}

int CApp::check_ogl_attributes()
{
int exit_code = ERR_EN_NO_ERROR;
int red_size = 0;
int green_size = 0;
int blue_size = 0;
int alpha_size = 0;
int double_buffer = 0;
int profile_mask = 0;
int major_version = 0;
int minor_version = 0;
int depth_size = 0;
char * pBuffer = NULL;

if(!SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red_size) && !SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green_size) && !SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &blue_size) && !SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha_size) && !SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &double_buffer) && !SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask) && !SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major_version) && !SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor_version) && !SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth_size))
{
// prüfe hier die Werte

  if(!(red_size == RED_BIT_SIZE && green_size == GREEN_BIT_SIZE && blue_size == BLUE_BIT_SIZE && alpha_size == ALPHA_BIT_SIZE && major_version == MAJOR_VERSION && minor_version == MINOR_VERSION && depth_size == DEPTH_SIZE && profile_mask == SDL_GL_CONTEXT_PROFILE_CORE))
  {exit_code = ERR_EN_ATTR;}

}
else
{exit_code = ERR_EN_OGL_GET_ATTR;}

#if EMODE == EDEBUG_MODE
if(exit_code == ERR_EN_ATTR)
{
pBuffer = new char[2048];

  	if(pBuffer != NULL)
  	{
  		sprintf(pBuffer, "<table style=\"width: 400px;\"><tr><td style=\"width: 220px; text-decoration: underline; height: 30px;\">GL Attribut</td><td style=\"width: 90px; text-decoration: underline; height: 30px;\">erwartet</td><td style=\"width: 90px; text-decoration: underline; height: 30px;\">gesetzt</td></tr><tr><td>red size:</td><td>%d</td><td>%d</td></tr><tr><td>green size:</td><td>%d</td><td>%d</td></tr><tr><td>blue size:</td><td>%d</td><td>%d</td></tr><tr><td>alpha size:</td><td>%d</td><td>%d</td></tr><tr><td>doublebuffer:</td><td>%d</td><td>%d</td></tr><tr><td>profile mask:</td><td>%d</td><td>%d</td></tr><tr><td>major version:</td><td>%d</td><td>%d</td></tr><tr><td>minor version:</td><td>%d</td><td>%d</td></tr><tr><td>depth size:</td><td>%d</td><td>%d</td></tr><tr><td>SDL Error: %s</td></tr><tr><td>GL Error: %d</td></tr></table>", RED_BIT_SIZE, red_size, GREEN_BIT_SIZE, green_size, BLUE_BIT_SIZE, blue_size, ALPHA_BIT_SIZE, alpha_size, DOUBLE_BUFFER, double_buffer, SDL_GL_CONTEXT_PROFILE_CORE, profile_mask, MAJOR_VERSION, major_version, MINOR_VERSION, minor_version, DEPTH_SIZE, depth_size, SDL_GetError(), glGetError());

  		if(this->pDebug_Str != NULL)
  			{this->write_log(pBuffer, DB_MSG_ERR);}
  		delete [] pBuffer;
  }
  }

#endif

return exit_code;
}

SDL_Init() and SDL_CreateWindow are running before SDL_GL_SetAttribute functions are called.
So why isn’t it working? Thanks for help. Need more code? Just ask (I minimized the code for a better overview)

Perhaps I’m missing it between all the error handling code, but do you actually create an OpenGL context anywhere (SDL_GL_CreateContext). Without it SDL_GL_GetAttribute is bound to not work. Also, your glxinfo output says your system supports OpenGL 3.0, but you are asking for a 3.2 context.

PS: please use [noparse]


[/noparse] around source code to preserve formatting.

Does your call to SDL_CreateWindow include the flag SDL_WINDOW_OPENGL?

Also, you need to call SDL_GL_SetAttribute before calling SDL_CreateWindow, not after.

Thanks.

You were both right. Works fine now :slight_smile: