Jump to content
  • Advertisement
Sign in to follow this  
tgashby

OpenGL Texture coords aren't what they should be, what am I doing wrong?

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

Hey everyone,

I am by no means any sort of expert on OpenGL, I just started learning it recently and I've run into a little issue.
I am trying to draw only portions of a texture that is loaded in.

After some research I've come to use glScale() which seems to be the way to do what I want, but I am not getting the right texture section.
Here's a short code snippet:
void Texture::drawSection(GLfloat xPos, GLfloat yPos, int sectX, int sectY, int sectWidth, int sectHeight)
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_TEXTURE);

glLoadIdentity();

glScalef(1/(GLfloat)width, 1/(GLfloat)height, 1); // "width" is the width of the entire texture
// "height" is the height of the entire texture

glPushMatrix();

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);

glTexCoord2f((GLfloat)sectX, (GLfloat)sectY); // sectX and sectY are the top-left corner points
glVertex2f(xPos, yPos);

glTexCoord2f((GLfloat)sectWidth, (GLfloat)sectY); // sectWidth is the width of the individual section
glVertex2f(xPos + sectWidth, yPos);

glTexCoord2f((GLfloat)sectWidth, (GLfloat)sectHeight); // sectHeight is the height of the individual section
glVertex2f(xPos + sectWidth, yPos + sectHeight);

glTexCoord2f((GLfloat)sectX, (GLfloat)sectHeight);
glVertex2f(xPos, yPos + sectHeight);

glEnd();

glPopMatrix();
}



This works if I start the texture section at the top-left corner of the whole texture and choose any section branching off of that.

Forgive me if the answer is obvious... OGL is still a bit of a mystery.

Thanks!
~Tag

Share this post


Link to post
Share on other sites
Advertisement
You have to be aware that OpenGL has no concept of absolute directions of your texture other than how you pass the data to it. What you think is upper-left is most likely the lower-left, or vice versa, due to the way you pass the image data to OpenGL.

All OpenGL knows is that the pointer passed to glTexImage corresponds to texture coordinate (0,0), and the texture coordinate increases from there. So if you load the image and store it top-down, the image will point to the upper-left corner, and so (0,0) corresponds to the upper-left corner of your image, and you have to specify your texture coordinates with that as a reference. You have to know how the image data is loaded, and then can you start defining your texture coordinates based on where the origin of the image really is.

edit: Reading the post a second time, perhaps the orientation isn't the problem. Do you mean you can only select the proper slice if the start of the slice is at the origin? If so, perhaps you just need to select the slice from (sectX, sectY) to (sectX+sectWidth, sectY+sectHeight) instead.

Share this post


Link to post
Share on other sites

edit: Reading the post a second time, perhaps the orientation isn't the problem. Do you mean you can only select the proper slice if the start of the slice is at the origin? If so, perhaps you just need to select the slice from (sectX, sectY) to (sectX+sectWidth, sectY+sectHeight) instead.


Yes, that's what I mean.

Thank you!

I'll play around with that... I think I know what you mean...

Share this post


Link to post
Share on other sites
Yes!

That's exactly what I needed, and that makes way more sense too.

Thank you so much.

This was like the biggest and most frustrating hurdle I'd been trying to deal with.

You rock, Brother Bob! smile.gif

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!