Stretch OpenGL texture to window dimensions

Go To StackoverFlow.com

0

I have a function that renders RGB video frames to a window. This works fine for the captured dimensions (640 x 480) if the window is the same dimensions but when I resize the window the rendering is not correct:

img http://img855.imageshack.us/img855/1694/screenshot20120404at536.png

Edit: The now working function.

static inline void draw_rgb(
     unsigned char * buf, unsigned x, unsigned y, unsigned window_width,
     unsigned window_height, unsigned frame_width, unsigned frame_height
 )
{
    glEnable(GL_TEXTURE_RECTANGLE_EXT);
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 1);

    glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, window_width * window_height * 2, buf);

    glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, frame_width, frame_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    glViewport(x, y, window_width, window_height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho((GLfloat)0, (GLfloat)window_width, (GLfloat)0, (GLfloat)window_height, -1.0, 1.0);

    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 1);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();

    glBegin(GL_QUADS);
    {
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(0.0f, window_height);
        glTexCoord2f(0.0f, frame_height);
        glVertex2f(0.0f, 0.0f);
        glTexCoord2f(frame_width, frame_height);
        glVertex2f(window_width, 0.0f);
        glTexCoord2f(frame_width, 0.0f);
        glVertex2f(window_width, window_height);
    }
    glEnd();
}
2012-04-04 21:42
by junglecat
What is 'not correct' - Tim 2012-04-04 21:46
Why choose EXT_texture_rectangle/ARB_texture_rectangle over ARB_texture_non_power_of_two - genpfault 2012-04-04 21:52
The image becomes distorted as shown here: linkjunglecat 2012-04-04 21:53
I should have mentioned I am an OpenGL newbie - junglecat 2012-04-04 21:55
@genpfault: Because they're more widely supported, and have non-normalized texture coordinates - Nicol Bolas 2012-04-04 22:55


2

Your problem is a combination of two factors.

First, as datenwolf pointed out, your texture's width/height is constant. It doesn't change when the window's size does. BTW, you shouldn't be rebuilding the texture every frame unless the texture's data changes. And even then, you can upload changes with glTexSubImage2D rather than glTexImage. You don't need to call the glTexParameter stuff either.

Second, this also means that your texture coordinates don't change. So you need to change your quad drawing to be this:

glBegin(GL_QUADS);
{
    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(0.0f, window_height);
    glTexCoord2f(0.0f, texture_height);
    glVertex2f(0.0f, 0.0f);
    glTexCoord2f(texture_width, texture_height);
    glVertex2f(window_width, 0.0f);
    glTexCoord2f(texture_width, 0.0f);
    glVertex2f(window_width, window_height);
}
glEnd();
2012-04-05 00:40
by Nicol Bolas


3

The main problem is here

glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, window_width, window_height, 0,
         GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf);

glTexImage takes the size of your texture data, not the window size or the size of the desired output.

2012-04-05 00:17
by datenwolf
If I give it the size of the texture data (640 x 480) the rendered frames are no longer distorted but instead clipped to the window size - junglecat 2012-04-05 00:30
@junglecat: Well, you determine the output size using the projection and the vertex positions of the textured quads. Adjust those - datenwolf 2012-04-05 07:46


0

Your glViewport call is using the same w and h variables that represent the size of the texture everywhere else.

Try changing the glViewport to use the size of the window you're rendering into, instead of w and h. Everything else should be able to stay the same.

For better results, you could adapt the viewport size to maintain the aspect ratio of your video, by reducing the viewport's width or height so that the video doesn't look stretched.

2012-04-04 21:55
by emackey
I've updated the variable names to make more sense - junglecat 2012-04-04 22:30
@junglecat: You kinda missed the point. Your texture is not the same size as your window. If it were, then you wouldn't need to stretch it. That's where your corruption is coming from: your texture's size is fixed; it's the window's size that changes - Nicol Bolas 2012-04-05 00:37
Ads