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();
}
EXT_texture_rectangle
/ARB_texture_rectangle
over ARB_texture_non_power_of_two
- genpfault 2012-04-04 21:52
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();
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.
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.