Advertisement

[SDL + Opengl] something is wrong with this code, and i can't find the problem

Started by July 01, 2011 11:57 AM
2 comments, last by lavskegg 13 years, 5 months ago
I got a big problem somehow my application have decided to render 2 or more times to the screen every frame i don't like that.

I have use SDL 1.3 successfully on windows 7 and decided to convert my small project to ubuntu 10.


#include <SDL/SDL.h>
# define GL_GLEXT_PROTOTYPES
# include "SDL/SDL_opengl.h"

#include "Vec2.h"
#include "Vec3.h"
#include "Vec4.h"
#include "Matrix.h"

SDL_Window *_window;
SDL_GLContext _mainContext;
int _Width = 1000;
int _Height = 700;

//Quad Data
GLuint _sharedVao;
GLuint _sharedUvloc;
GLuint _sharedBuffer;

//Matrix
Matrix _modelMatrix;
Matrix _pProjectionMatrix;

//Sdhader
GLuint _VertexShader; // Vertex Shader
GLuint _FragmentShader; // Fragment Shader
GLuint _Program;

GLuint _VertexLoc;
GLuint _ProjViewLoc;
GLuint _ModelViewLoc;

void InitSDl( void )
{
if (SDL_Init( SDL_INIT_VIDEO ) < 0){
printf("Could not initiliaze SDL: %s", SDL_GetError() );
SDL_Quit();
return;
}

unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);



// Make the window
_window = SDL_CreateWindow( "Test Case", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, _Width, _Height, flags);

if (!_window){
printf( "Could not create the stupid SDL window: %s", SDL_GetError() );
SDL_Quit();
return;
}

// Make the OpenGL 3.2 context
_mainContext = SDL_GL_CreateContext(_window);


SDL_GL_MakeCurrent( _window, _mainContext );
if( SDL_GL_SetSwapInterval(1) == -1 )
printf( "SwapInterval not supported: %s", SDL_GetError() );

}

void InitMyQuad( void )
{
float MyVertex1[] ={0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f};


glGenVertexArrays(1, &_sharedVao);
glBindVertexArray( _sharedVao );
// bind buffer for vertices and copy data into buffer
glGenBuffers(1, &_sharedBuffer );
// bind buffer for vertices and copy data into buffer
glBindBuffer(GL_ARRAY_BUFFER, _sharedBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex1), MyVertex1, GL_STATIC_DRAW);
glEnableVertexAttribArray( _VertexLoc );
glVertexAttribPointer( _VertexLoc, 4, GL_FLOAT, 0, 0, 0);

}
bool printLog( const GLuint &obj, const char* FileName)
{
int infologLength = 0;
GLint WasSuccesful;

if(glIsShader(obj))
glGetShaderiv( obj, GL_COMPILE_STATUS, &WasSuccesful );
else
glGetProgramiv( obj, GL_LINK_STATUS, &WasSuccesful );

GLchar infoLog[1024];

if (glIsShader(obj))
glGetShaderInfoLog(obj, 1023, &infologLength, infoLog);
else
glGetProgramInfoLog(obj, 1023, &infologLength, infoLog);


if (infologLength > 0)
{
if( WasSuccesful )
printf("%s \n %s",FileName, infoLog);
else
printf("%s \n %s",FileName, infoLog);
}
return (WasSuccesful != 0);
}
bool LoadShader( void )
{
const char* vertexShader = {
"#version 150\n"
"uniform mat4 modelMatrix, projMatrix;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = projMatrix * modelMatrix * position ;\n"
"}\n"
};

const char* fragmentShader = {
"#version 150\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_Color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n"
};

_VertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource( _VertexShader, 1, (const GLchar**)&vertexShader, NULL);
glCompileShader( _VertexShader );
if( !printLog( _VertexShader, "QuadShader" ) )
return false;

_FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource( _FragmentShader, 1, (const GLchar**)&fragmentShader, NULL);
glCompileShader( _FragmentShader );
if( !printLog( _FragmentShader, "QuadShader" ) )
return false;

_Program = glCreateProgram();
glAttachShader( _Program, _VertexShader );
glAttachShader( _Program, _FragmentShader );

glBindFragDataLocation( _Program, 0, "Frag_Color");

glLinkProgram( _Program);
if( !printLog( _Program, "QuadShader" ) )
return false;

_VertexLoc = glGetAttribLocation( _Program, "position" );

_ProjViewLoc = glGetUniformLocation( _Program, "projMatrix" );

_ModelViewLoc = glGetUniformLocation( _Program, "modelMatrix" );

printf("ShaderManager::CompileShader()");

return true;

}
void buildOrtogonOrtho( const float &left, const float &right, const float &bottom,
const float &top, const float &nearVal, const float &farVal )
{
_pProjectionMatrix.identity();
_pProjectionMatrix[0] = 2.0f/(right-left);
_pProjectionMatrix[5] = 2.0f/(top-bottom);
_pProjectionMatrix[10] = 2.0f/(farVal-nearVal);

_pProjectionMatrix[12] =-(right + left)/(right - left);
_pProjectionMatrix[13] =-(top+bottom)/(top-bottom);
_pProjectionMatrix[14] =-(farVal+nearVal)/(farVal-nearVal);
}
void RenderQuad( const Vec2 &MinPos, const Vec2 &MaxPos )
{
_modelMatrix.identity();
_modelMatrix.SetUpVec( _modelMatrix.GetUpVec() * (MaxPos.Y - MinPos.Y) );
_modelMatrix.SetRightVec( _modelMatrix.GetRightVec() * (MaxPos.X - MinPos.X) );
_modelMatrix.SetPosition( Vec3( MinPos.X, MinPos.Y, 0.0f ) );

glUseProgram( _Program );
glUniformMatrix4fv( _ProjViewLoc, 1, false, _pProjectionMatrix );
glUniformMatrix4fv( _ModelViewLoc, 1, false, _modelMatrix ); //MyGl::_pModelMatrix


glBindVertexArray( _sharedVao );
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

}
bool check_events()
{
SDL_Event evt;
SDLKey key;

while (SDL_PollEvent(&evt)){
switch(evt.type){
case SDL_KEYDOWN:
key = evt.key.keysym.sym & 0xff;
if ((SDLK_ESCAPE & 0xff) == key)
return false;
break;
};
}
return true;
}
int main( int argc, char* args[] )
{

buildOrtogonOrtho( 0, _Width, _Height, 0, -1, 1 );

InitSDl();

if( !LoadShader() )
return false;
InitMyQuad();

glViewport( 0, 0, _Width, _Height );
glClearColor( 0.5f, 0.5f, 0.5f, 1.0f );
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);

float Offset = 700.0f / 21.0f;
float size = 700.0f / 28.0f;
while( check_events() )
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

for( int y=0; y<24; y++ )
{
// if( y==13 )
// new screen dump before the end
for( int x=0; x<24; x++ )
{
Vec2 MinPos((float)x*Offset, (float)y*Offset);
RenderQuad( MinPos, MinPos + Vec2(size,size));
}
}
SDL_GL_SwapWindow(_window);
SDL_Delay(20);
}

return 0;
}
How do you know it renders 4 times per frame? At least I don't see how you'd know that through debugging, with the SwapWindow being right before the Delay....
Advertisement
when i step through the code I can see how the window changes during the render pass.
example when i pass the glClear( GL_COLOR_BUFFER ); the window go gray, latter 60 of the quads i render are visible and so on to all is rendered.

SDL_GL_SwapWindow( _window ); don't do anythinge. if i remove it don't change a thinge



How do you know it renders 4 times per frame? At least I don't see how you'd know that through debugging, with the SwapWindow being right before the Delay....
Wierd when i remove this line of code it works.
now the SDL_GL_SwapWindow( _window ); works perfect

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);


do any one know why??

This topic is closed to new replies.

Advertisement