Advertisement Jump to content
Sign in to follow this  

Help with texture mapping [newbie]

This topic is 4213 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

Hi All: I am having some trouble with displaying a simple bitmap as a texture on a square 2D polygon displayed on the top left corner of a window. I have: glOrtho(-1.0, 1.0, 1.0, -1.0, 1.0, -1.0); so left: -1.0, right 1.0, top -1.0, bottom 1.0. I draw the square polygon without any troubles as: glBegin(GL_POLYGON); glVertex2d(-1.0, -1.0); glVertex2d(-0.5, -1.0); glVertex2d(-0.5, -0.5); glVertex2d(-1.0, -0.5); glEnd(); When I enter the texture co-ordinates however, I do not see the texture drawn as expected (I am expecting to just see the whole texture covering the whole polygon. I followed Gamedev's suggestion at re texture co-ordinates) Here is the code i use to draw the texture-mapped polygon: glBegin(GL_POLYGON); glTexCoord2d(0.0, 1.0); glVertex2d(-1.0, -1.0); glTexCoord2d(1.0, 1.0); glVertex2d(-0.5, -1.0); glTexCoord2f(1.0, 0.0); glVertex2d(-0.5, -0.5); glTexCoord2f(0.0, 0.0); glVertex2d(-1.0, -0.5); glEnd(); Any one who can help me get these co-ordinates straight because I am getting a bit confused?! Thanks Duncan Camilleri

Share this post

Link to post
Share on other sites
What does the texture look like, though? Does it render at all?

I'm a little confused by using a negative value for the top in glOrtho. Although I assume there is a reason you have done this, I have never personally seen it (usually I see people put 0,0 in the upper left, and screenwidth/screenheight in the lower right). I know negative values are allowed for the z-clipping planes, however I'm not 100% certain on the others. I guess it shouldnt be a problem, since its just a matrix multiplication, but it may be worth a try.
Perhaps try glOrtho(0.0, 2.0, 2.0, 0.0, -1.0, 1.0) and shift your polygon over, simply to eliminate this as a possible problem.

Additionally, I believe polygons are usually wound counter-clockwise for culling reasons in OpenGL. So you may want to change the order you draw it in.

I am not certain what you're problem is, so these are just a few things you may wish to try while waiting for more replies.

Good luck.

Share this post

Link to post
Share on other sites
Having a negative value in glOrtho() as such simply means that the coordinates start from the top-left, rather than the bottom-left, I believe.

Also, change your texture polygon code to this:

glBegin gl_Quad
glTexCoord2f 0,0
glVertex2f 0,0
glTexCoord2f 1,0
glVertex2f 1,0
glTexCoord2f 1,1
glVertex2f 1,1
glTexCoord2f 0,1
glVertex2f 0,1

Does that work? Are you binding the texture before hand? Have you verified that the texture has loaded correctly?

Share this post

Link to post
Share on other sites
All your comments are much appreciated.

I tried to apply the same thing counter clockwise with 0.0 replacing -1.0 but all is in vain.

The image does get rendered, but is being drawn incorrectly (unexpected rotation and skewing is being done) so I do not think the problem is with binding or enabling textures.

Something very funny that I note, is that I am trying this with two bitmap versions. A chequered bitmap generated via code (as seen in some external sample) which works perfectly. However, when I simply replace the bit data with that of a bitmap loaded from file, then the problems start to appear. For both bitmaps, the data stored in memory is formatted as 32 bits per pixel.



Share this post

Link to post
Share on other sites
Aaaaahh ok I got the solution!

From the text, I have read how the width and height should be 2n + 2 so I created the bitmap to be 66X66 (for the +2). Changing the size of my texture bitmap back to 64X64 solved the problem.

I also am confirming that with -1.0 in glOrtho and when drawing the polygon clockwise rather than counter clockwise, still works fine!

I thank you all for your input!



Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!