Jump to content
  • Advertisement
Sign in to follow this  
Silex

OpenGL stretching a texture (webcam video) to entire background then drawing "over" it?

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting live feed from a webcam which I want to have as background for an augmented reality type application. As it stands, my draw function called each frame does this: (0) set up projection for depth (1) for each object in world: draw (2) set up 2D ortho view (3) draw text on top I'm a little unfamiliar with opengl texturing, but I'd like to set up it so that I'm stretching a texture to cover the entire viewport, and have everything else drawn as normal (except wireframe, in this case) on top regardless of camera orientation. How do I set up my projection matrix to achieve this? How do I make sure the video covers the entire viewport? Thanks!

Share this post


Link to post
Share on other sites
Advertisement

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
GL11.glFrustum(-width / 64.0f, width / 64.0f, -height / 64.0f, height / 64.0f, 8, 65536);
Where width/height is your resolution.

To draw a fullscreen quad:

glTranslatef(-width / 2.0f, -height / 2.0f, -256.0f);
glBegin(GL_QUADS);
glVertex3f(-width / 2.0f, -height / 2.0f, 0.0f);
glVertex3f(width / 2.0f, -height / 2.0f, 0.0f);
glVertex3f(width / 2.0f, height / 2.0f, 0.0f);
glVertex3f(-width / 2.0f, height / 2.0f, 0.0f);
glEnd();

Share this post


Link to post
Share on other sites
If you set the modelview and projection matrices to identity then a fullscreen quad will go from (-1,-1) to (1,1). Look at Fig. 3-2 from the Red Book...


Figure 3-2 : Stages of Vertex Transformation
(I hope linking to that image worked, if not find it in the link I posted just above)

Setting the modelview and projection matrices to the identity matrix effectively takes out the modelview matrix, projection matrix, and perspective division steps so that you're basically giving OpenGL vertices that are already in normalized device coordinates (in the range [-1.0, 1.0]).

Yes, you can draw the fullscreen quad first then reset the projection matrix and draw the rest of your scene.

EDIT: As you can see there are several ways to do this. Funkapotamus' method is perfectly fine. You can also set an orthographic projection so as to send your vertices to OpenGL in window coordinates, as discussed here.

Share this post


Link to post
Share on other sites
If you're drawing everything to the same plane, then then you'll want to draw the quad first.

You may be able to get away with drawing the other primitives closer to the camera. In which case you can draw in any order you want. However, it's better practice to draw your background, then draw your objects, then draw your text/hud-type elements. I'm assuming you're going to want the full-screen quad to be your background- so I'd draw that first.

Share this post


Link to post
Share on other sites
Neither works for me.

By using the call the glFrustrum I get a black screen (the clear color) and nothing is showing up. Using the identity method, as reproduced below, yields a grey screen with GL_LIGHTING and a screen set to the glColor(...) without GL_LIGHTING.


glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);

glBindTexture( GL_TEXTURE_2D, debug_tex );

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glTexCoord2f( 0.0f, 1.0f); glVertex2d(-1.0, -1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex2d( 1.0, -1.0);
glTexCoord2f( 1.0f, 0.0f); glVertex2d( 1.0, 1.0);
glTexCoord2f( 0.0f, 0.0f); glVertex2d(-1.0, 1.0);
glEnd();



What could be going wrong?

Here's how I load the texture in the init code:


SDL_Surface* tex_image;
if ((tex_image = SDL_LoadBMP("image.bmp")))
{ glGenTextures(1, &debug_tex);
glBindTexture(GL_TEXTURE_2D, debug_tex);
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex_image->w, tex_image->h, 0, GL_BGR,
GL_UNSIGNED_BYTE, tex_image->pixels );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
printf("loaded image!\n");
}
else
{ printf("couldn't load image!\n");
}

if (tex_image) SDL_FreeSurface(tex_image);

Share this post


Link to post
Share on other sites
Quote:
Original post by Silex
Neither works for me.

By using the call the glFrustrum I get a black screen (the clear color) and nothing is showing up. Using the identity method, as reproduced below, yields a grey screen with GL_LIGHTING and a screen set to the glColor(...) without GL_LIGHTING.

*** Source Snippet Removed ***

What could be going wrong?

Here's how I load the texture in the init code:

*** Source Snippet Removed ***
You will most likely want lighting off for the background texture (of course, that depends on what you're trying to achieve).

Are you still drawing the rest of your scene? If you are and it's not showing up, try turning off depth writing when you render the background, then turn it back on afterwards. You can enable/disable depth writing with glDepthMask.

For the texture problem, can you display the texture on other objects or does it not work at all? I assume the latter because the code you posted looks okay. Does the image you're loading have power-of-two dimensions? Are you getting any OpenGL errors (you should always be checking for those)?

This isn't part of your current problem but it's a good idea to use an internalformat with a specific bit-depth. For example, use GL_RGB8 instead of GL_RGB (or 3). This is because if you don't specify the number of bits you want, the driver is free to give you anything that has the correct number of channels. It's quite common that people use GL_RGB for the internalformat and their drivers aren't set to high quality mode, so they get 16-bit textures instead of 24-bit, and they notice significant banding artifacts. But as I said, this isn't your current problem.

Also, why are you setting the projection and modelview matrices to the identity twice in a row? I assume that's just a copy-paste error?

Share this post


Link to post
Share on other sites
I believe the texture is being displayed correctly, however some other texture settings are affecting how its being drawn. To verify this, I changed the glVertex* calls to have the quad cover only half of the screen, and indeed I get half the screen covered in green!

Someone on the opengl.org forums suggested I explicitely set glTexEnv to GL_REPLACE so that the texture isn't modulated with the color specified by glColor (since I do indeed use green to draw fonts elsewhere). This didn't help, however.

I don't know what else I could try.

Share this post


Link to post
Share on other sites
Fixed. Apparently I was initializing the texture too early, or something like that. :D

Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!