Skip to main content
GameDev.net gamedev.net
🔒 Locked

texture mapping

Started by stella1016 Jul 7, 2008 at 7:40 AM 11 replies 1.1k views
Original Post
stella1016
stella1016
I have one window to display some images. I shoule be able to open any image files, and display it in the middle of the window (the window size if fixed.) I set allowed max image size as (maxTextureWidth, maxTextureHeight). And if opened image size is larger than this size, I will display a scaled version of this image. For reserving the size of ratio, I compute sth of the image and update the texture: { imageWidth = currentImageIpl->width; imageHeight = currentImageIpl->height; int channel = currentImageIpl->nChannels; textureDisplayRatio_x = (float)imageWidth / (float)maxTextureWidth; textureDisplayRatio_y = (float)imageHeight / (float)maxTextureHeight; if(textureDisplayRatio_x > 1.0 || textureDisplayRatio_y > 1.0) { if(textureDisplayRatio_x >= textureDisplayRatio_y) { imageWidth_display = maxTextureWidth; imageHeight_display = imageHeight / textureDisplayRatio_x; } else { imageWidth_display = imageWidth / textureDisplayRatio_y; imageHeight_display = maxTextureHeight; } textureDisplayRatio_x = (float)imageWidth_display / (float)maxTextureWidth; textureDisplayRatio_y = (float)imageHeight_display / (float)maxTextureHeight; } else { imageWidth_display = imageWidth; imageHeight_display = imageHeight; } imageData = currentImageIpl->imageData; if(channel == 3) glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, imageData); if(channel ==1) glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, imageWidth, imageHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData); update = false; } I did it like this in PaintGL(): (I am using OpenGL with Qt) glLoadIdentity(); {... glTranslatef(GLfloat((maxTextureWidth - imageWidth_display) / 2),GLfloat((maxTextureHeight - imageHeight_display) / 2), 0); glBegin(GL_QUADS); glTexCoord2f(textureDisplayRatio_x, 0.0f); glVertex2f( imageWidth_display, imageHeight_display); glTexCoord2f(0.0f, 0.0f); glVertex2f(0, imageHeight_display); glTexCoord2f(0.0f, textureDisplayRatio_y); glVertex2f(0, 0); glTexCoord2f(textureDisplayRatio_x, textureDisplayRatio_y); glVertex2f( imageWidth_display, 0); glEnd(); ... } The problem is that the image is not displayed in window. What could be the problem? I checked all the computations are right. Thanks in advance.
Daaark
Daaark
Did you call glEnable(GL_TEXTURE_2D), and glBindTexture(...)?
stella1016
stella1016
Quote:
Original post by Daaark
Did you call glEnable(GL_TEXTURE_2D), and glBindTexture(...)?


Yes, I did glEnable(GL_TEXTURE_2D), but not glBindTexture(...). I only have one texture. So I don't need to bind, right?
stella1016
stella1016
And in initializeGL(), I did this:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, maxTextureWidth, maxTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

Then later I only substitute part of the texture.
rosewell
rosewell
Do you need to setup an Ortho projection?
stella1016
stella1016
Sorry I didn't mension that, it works well for images which are smaller than my maxTexture size. Only the images bigger than maxTexture size have problem.

OH, I found my problem, the Texture is smaller than my image, I need to new another texture.

:)
stella1016
stella1016
I am so stupid...

Thanks for the helps.
stella1016
stella1016
OK. I have a question, how to scale the image to the size which I want it to be? Then I don't have to new another texture, since I have a fixed size texture already. Is there any function supporting this?

what I have are: input size, output size, image data
Lord_Evil
Lord_Evil
Assuming the resizing is just for display purposes, just draw a bigger quad. OpenGL will do the rest for you.

BTW, did you notice that in your PaintGL() function there are only 2 glVertex2f() calls? For a quad you need 4, so no quad would be rendered. ;)

Edit: OK, just overlooked them. They were hidden due to missing line breaks. :(

Edit2: It could also be a matter of winding. Is backface culling disabled?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
stella1016
stella1016
Quote:
Original post by Lord_Evil
Assuming the resizing is just for display purposes, just draw a bigger quad. OpenGL will do the rest for you.

BTW, did you notice that in your PaintGL() function there are only 2 glVertex2f() calls? For a quad you need 4, so no quad would be rendered. ;)

Edit: OK, just overlooked them. They were hidden due to missing line breaks. :(


The main problem is that here I only have one texture init. Because I need also display webcam output, in order to make it efficient I used glTexSubImage2D(). Here it looks like I need to create another texture to display...I have to, right?

The best way should be using the present texture, but no way...

For the quad, do you mean draw a quad, and change the viewport or something?
stella1016
stella1016
At present there is no 3D information used. All the display is based on 2D planar.I just need to adjust the size when the image is too big.
Lord_Evil
Lord_Evil
You actually can use the same texture by just adjusting the texture coordinates, i.e. you use tex coords smaller than 1.

By drawing a quad I just mean what you do already: glBegin(GL_QUADS); ...
Just change the coordinates of the vertices. However, I assume you want a fixed size quad, so playing around with the texture coords would be better.

Just make sure that the texture is fully initialized, i.e. you provide a large enough image with glTexImage(...) or just feed it some all-0 buffer of the same size.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
stella1016
stella1016
Quote:
Original post by Lord_Evil
You actually can use the same texture by just adjusting the texture coordinates, i.e. you use tex coords smaller than 1.

By drawing a quad I just mean what you do already: glBegin(GL_QUADS); ...
Just change the coordinates of the vertices. However, I assume you want a fixed size quad, so playing around with the texture coords would be better.

Just make sure that the texture is fully initialized, i.e. you provide a large enough image with glTexImage(...) or just feed it some all-0 buffer of the same size.


Now I understand what you meant. I can change my maxTextureWidth and maxTexutreHeight instead.

What a good idea! I've already created it, and didn't take advantage of it!

Thanks very much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.