# Drawing Colored Geometry in OpenGL using SDL

First off, I will confess I have asked the same question on stackoverflow, but I think this forum might be a better fit.

I am trying to combine 2 things using SDL:

1. Draw a webcam feed via an SDL_Surface (from OpenCV).
2. Draw some plain colored geometry on top, using OpenGL.

The problem I have is that—I think—the geometry is textured by the screen texture that I draw the webcam feed in, even if I call glDisable(GL_TEXTURE_2D) right before drawing the quad. See the screenshot below, the square in the top-left is supposed to be white, but it seems to have the color of the bottom-right texel.

The code in my Display function is as follows:

// screen_surface_ contains a frame from the camera
SDL_UpdateTexture(screen_texture_, NULL, screen_surface_->pixels, screen_surface_->pitch);
SDL_RenderClear(renderer_);
SDL_RenderCopy(renderer_, screen_texture_, NULL, NULL);

glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glVertex3f( 10.0f, 50.0f, 0.0f ); /* Top Left */
glVertex3f( 50.0f, 50.0f, 0.0f ); /* Top Right */
glVertex3f( 50.0f, 10.0f, 0.0f ); /* Bottom Right */
glVertex3f( 10.0f, 10.0f, 0.0f ); /* Bottom Left */
glEnd( );

glColor3f(1.0, 1.0, 1.0);
SDL_RenderPresent(renderer_);


You can view all the code of relevant functions here.

I got a blanket answer saying "Don't mix SDL and OpenGL draw code"; as addressed in this bug report. But that would mean I'm simply stuck waiting for that bug to be fixed, so I'm still looking for a way to disable texturing after SDL_RenderCopy has been called.

Edit: I've confirmed that it's indeed still using the screen_texture_. Setting the texCoord to values between 0-100 shows a part of the webcam feed. (I tried 0-1 first but then I read that texCoords are different for rectangular images).

• Please note that this isn't a forum. Please see the tour page. – Lysol Jan 31 '14 at 18:00
• Also, the OpenGL that you are using is old OpenGL. All but one of the functions you called are deprecated. I am not sure if that is the cause of the problem, but you should still upgrade to modern OpenGL. – Lysol Jan 31 '14 at 18:05
• s/forum/site/ ;) Could you point me to the newer functions I'm supposed to use? I copied this code from an "SDL + OpenGL" example. – noio Feb 1 '14 at 13:18
• I can't give you a list of all of them as almost everything changed in modern OpenGL. But I can link you to a tutorial that uses modern OpenGL: arcsynthesis – Lysol Feb 2 '14 at 22:27
• I believe some parts of SFML still use legacy OpenGL (SFML runs on OpenGL in the backgound), so it seemed to give me problems when trying to run certain functions (I think I may have done a bad job of compiling GLEW though). If you happen to have issues, try using SDL instead. Otherwise you can keep using SFML. – Lysol Feb 3 '14 at 17:46

(Crosspost from stackoverflow)

I have fixed the problem by simply adding

glcontext_ = SDL_GL_CreateContext(window_);


to the SDL_Init code. I think all my calls to openGL functions (like glDisable(GL_TEXTURE_2D) were applied to the wrong context. Explicitly creating a context from SDL sets the right context to be active, I'm guessing.

Something else to look out for: when using textured geometry after using SDL_RenderCopy; I had to make sure to reset

glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);


before calling glTexImage2d, because it uses

GL_UNPACK_ROW_LENGTH if it is greater than 0, the width argument to the pixel routine otherwise

(from the OpenGL docs)