I don't know about the memory leak (I would assume it is just a natural growth of the heap.. wouldn't worry about it too much).
What you are rendering is so trivial that you probably don't need VBOs (they are more critical when rendering meshes, e.g., a large complex model). Here is how to do it anyways:
GLuint vbo_ids[2];
glGenBuffers(2,vbo_ids); //generate two buffers.
//create arrays that store the points and colors for first geometry:
GLfloat points1[] = {400.0, 160.0, 320.0, 440.0, 480.0, 440.0};
glBindBuffer(GL_ARRAY_BUFFER, vbo_ids[0]);
glBufferData(GL_ARRAY_BUFFER, 6*sizeof(GLfloat), points1, GL_STATIC_DRAW);
//repeat for second geometry... you can also intertwine the data:
GLfloat buf2[] = { 0, 0, //tex-coord 1
100.0, 100.0, 0.0, //vertex 1
0.5, 0, //tex-coord 2
228.0, 100.0, 0.0, //vertex 2
0.5, 1, //tex-coord 3
228.0, 228.0, 0.0, //vertex 3
0, 1, //tex-coord 4
100.0, 228.0, 0.0 }; //vertex 4
glBindBuffer(GL_ARRAY_BUFFER, vbo_ids[1]);
glBufferData(GL_ARRAY_BUFFER, 20*sizeof(GLfloat), buf2, GL_STATIC_DRAW);
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{ //.. let's skip this stuff.. just to make it cleaner for the example...
}
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,1.0f,1.0f);
glBindBuffer(GL_ARRAY_BUFFER,vbo_ids[0]);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glEnable( GL_TEXTURE_2D );
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, ret );
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1.0f,1.0f,1.0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo_ids[1]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5*sizeof(GLfloat), 2*sizeof(GLfloat));
glTexCoordPointer(2, GL_FLOAT, 5*sizeof(GLfloat), 0);
glDrawArrays( GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable( GL_TEXTURE_2D );
glDisable (GL_BLEND);
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //remark: this only needs to be set once.
SDL_GL_SwapBuffers();
SDL_Delay(1000/30);
}
But, actually, for your application, Display Lists are much more appropriate:
GLuint list_start = glGenLists(2);
glNewList(list_start, GL_COMPILE);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.0f,1.0f);
glVertex2f(400.0, 160.0);
glVertex2f(320.0, 440.0);
glVertex2f(480.0, 440.0);
glEnd();
glEndList();
glNewList(list_start+1,GL_COMPILE);
glBegin( GL_QUADS );
glColor3f(1.0f,1.0f,1.0f);
//Bottom-left vertex (corner)
glTexCoord2d( 0, 0 );
glVertex3f( 100.f, 100.f, 0.0f );
//Bottom-right vertex (corner)
glTexCoord2d( 0.5, 0 );
glVertex3f( 228.f, 100.f, 0.f );
//Top-right vertex (corner)
glTexCoord2d( 0.5, 1 );
glVertex3f( 228.f, 228.f, 0.f );
//Top-left vertex (corner)
glTexCoord2d( 0, 1 );
glVertex3f( 100.f, 228.f, 0.f );
glEnd();
glEndList();
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{ //.. let's skip this stuff.. just to make it cleaner for the example...
}
glClear(GL_COLOR_BUFFER_BIT);
glCallList(list_start);
glEnable( GL_TEXTURE_2D );
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, ret );
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCallList(list_start+1);
glDisable( GL_TEXTURE_2D );
glDisable (GL_BLEND);
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //remark: this only needs to be set once.
SDL_GL_SwapBuffers();
SDL_Delay(1000/30);
}
Hope it helps.. for openGL, make sure to start with NeHe tutorials.
mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457