Advertisement

OpenGL textures... I don't get it.

Started by December 24, 2003 03:20 PM
6 comments, last by Hedos 21 years, 2 months ago
Hey, I''m having troubles understanding how does the textures work in openGL... I mean, by using glTexCoord2f to apply textures to my polygon. I don''t understand the logic behind this.. I just always use the same value for drawing my textures with polygons, like that:

glTexCoord2f(0.0f,1.0f); glVertex2f(0,0);
glTexCoord2f(1.0f,1.0f); glVertex2f(m_Textures->width,0);
glTexCoord2f(1.0f,0.0f); glVertex2f(m_Textures->width,m_Textures->height);
glTexCoord2f(0.0f,0.0f); glVertex2f( 0,m_Textures->height);
This always work fine.. But since I started to write a Tile class, wich is supposed to modify how do glTexCoord2f is used so it will just display a specific part of the texture ( a tile ) But as I don''t understand the texture logic, I''m not able to do it... By the way, if someone know a method that looks better for using Tiles in OpenGL, let me know. Thanks
Think of your texture this way:

0,0 = top left, 1,1 = bottom right.

The point in the middle would be 0.5f, 0.5f. So, say you wanted to draw only the top 1/4 of the texture, you would use
glTexCoord2f(0.0f,0.5f); glVertex2f(0,0);
glTexCoord2f(0.5f,0.5f); glVertex2f(m_Textures->width,0);
glTexCoord2f(0.5f,0.0f); glVertex2f(m_Textures->width,m_Textures->height);
glTexCoord2f(0.0f,0.0f); glVertex2f( 0,m_Textures->height);

If your texture were split into 4 sections, this would be the top left section (0-0.5 in the X direction, 0-0.5 in the Y direction). If you wanted to draw the top right section, you''d do:

glTexCoord2f(0.5f,0.5f); glVertex2f(0,0);
glTexCoord2f(1.0f,0.5f); glVertex2f(m_Textures->width,0);
glTexCoord2f(1.0f,0.0f); glVertex2f(m_Textures->width,m_Textures->height);
glTexCoord2f(0.5f,0.0f); glVertex2f( 0,m_Textures->height);


Just think of your entire texture as 0,0 -> 1,1.
Advertisement
Ohh.. that makes sense now
Thanks a lot
I, myself is facing some difficulty with texture in OpenGL. I understand the concept however.

My Question:
How comes when I draw a textured polygon all alone in a 3D scene it renders perfectly. But when I add more object to the screen to render, like a 3D grid, my textured polygon is messed up(colorwise). I know that the texture takes the same color of the polygon its resting on, but my polygon is white......shouldn''t the texture be fine then? When I go back to my OpenGL application, I don''t see the texture....instead, I see my white polygon(texture-less basically). Whats going on here?

--------
"Do you believe in Ghost?"
--------"Do you believe in Ghost?"
Btw, someone know any tutorial or maybe have some code that would give me an exemple of tile-texturing?
Forget it, my tiles work fine now, yippie
Advertisement
I posted this in a thread about textuirng a while ago. it may help

plus I think it's funny my cat attacking my friend

picky

| - Project-X - | - adDeath - | - my windows XP theme - | - email me - |

[edited by - RipTorn on December 25, 2003 5:51:32 AM]
quote:
Original post by Ready4Dis
0,0 = top left, 1,1 = bottom right.


0,0 is lower left in OpenGL, it''s upper left in D3D.

This topic is closed to new replies.

Advertisement