Sign in to follow this  
Silex

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627644
    • Total Posts
      2978367
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now