2

I want to use OpenGL to draw on top of a webcam stream. I'm using an SDL_Surface named screen_surface_ containing webcam data, that I'm rendering to the screen using (1):

SDL_UpdateTexture(screen_texture_, NULL, screen_surface_->pixels, screen_surface_->pitch); SDL_RenderClear(renderer_); SDL_RenderCopy(renderer_, screen_texture_, NULL, NULL); 

Then I try to draw some geometry on top:

glLoadIdentity(); glColor3f(1.0, 0.0, 1.0); glBegin( GL_QUADS ); 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); //<- I need this to make sure the webcam stream isn't pink? SDL_RenderPresent(renderer_); 

I have initialized OpenGL using (excerpt):

glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glViewport( 0, 0, res_width_, res_height_ ); glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(0.0f, res_width_, res_height_, 0.0f, -1.0f, 1.0f); 

Subquestion: If I don't reset the glColor to white the whole webcam stream is colored pink. I find this odd, because I thought that SDl_RenderCopy had already rendered that texture before the first call to glColor. So how does SDL_RenderCopy actually work?

Main question: I get a neat 40x40 square in the top left of the screen on top of my webcam feed (good!). However, in stead of pink, it is a kind of flickering dark purple color; seemingly dependent on the camera feed in the background. Could you please tell me what I'm overlooking?

Edit:

As per @rodrigo's comment, these are some images with the color set to R, G, B and white, respectively:

Red Square Green Square Blue Square White Square

Looking at these, it seems that the underlying texture has some effect on the color. Could it be that OpenGL is still applying (some part of) the texture to the quad?

Edit:

I suspect now that the geometry is drawn using the render texture as a texture, even though I've called glDisable(GL_TEXTURE_2D). Looking at the "White Square" screenshot (below), you can see that the white quad is the same color as the bottom-right pixel. I guess that the quad has no texture coordinates, so only the bottom-right texel is used. Knowing this, better question: how do I disable texturing?.

White Square 2

3
  • From your description, it sounds like you have additional drawing occurring after SDL_RenderPresent (...). Can you include the actual full sequence of events in-order each frame? Commented Jan 27, 2014 at 12:01
  • This is the full code of all relevant functions: gist.github.com/noio/8647696. AFAIK, nothing happens after SDL_RenderPresent Commented Jan 27, 2014 at 12:28
  • Maybe the color space of the GL context is not RGB, but YUV or something like that. You can check trying simple colors: (1,0,0), (0,1,0) and (0,0,1) and checking what you see. Commented Jan 27, 2014 at 13:13

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.