Sign in to follow this  
zerocide

Texture mapped onto quads appears tilted

Recommended Posts

zerocide    122
Hi all, When I try mapping a texture onto a quad, it appears to be tilted (At approximately a 45 degree angle). Does anyone know how I can fix it? Any unrelated suggestions are also welcome. Thank you. Screenshot at: http://www.zerocide.net/pavel/screen.jpg Coordinates for the wall we're looking at right now: X Y Z U V -500 -200 -500 1 0 -500 800 -500 1 1 500 800 -500 0 1 500 -200 -500 0 0 ** U and V are X and Y texture coordinates, respectively. And here's the drawing code: ==================================== int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix glRotatef(lookupdown, 1.0f, 0.0f, 0.0f); glRotatef(-heading, 0.0f, 1.0f, 0.0f); glTranslatef(-x, -y, -z); float cx, cy, cz; for(int objectIndex = 0; objectIndex < world1.numobjects; objectIndex++) { glNormal3f(world1.objects[objectIndex].normal_x, world1.objects[objectIndex].normal_y, world1.objects[objectIndex].normal_z); if(world1.objects[objectIndex].texture == -1) glColor3ub(world1.objects[objectIndex].color_r, world1.objects[objectIndex].color_g, world1.objects[objectIndex].color_b); else { glColor4f(1, 1, 1, 0.0f); glBindTexture(GL_TEXTURE_2D, texture[world1.objects[objectIndex].texture]); } glBegin(GL_QUADS); for(int vertexIndex = 0; vertexIndex < 4; vertexIndex++) { cx = (float)world1.objects[objectIndex].vertex[vertexIndex].x; cy = (float)world1.objects[objectIndex].vertex[vertexIndex].y; cz = (float)world1.objects[objectIndex].vertex[vertexIndex].z; cx /= 100.0f; cy /= 100.0f; cz /= 100.0f; if(world1.objects[objectIndex].texture != -1) { glTexCoord2f(world1.objects[objectIndex].vertex[vertexIndex].tx, world1.objects[objectIndex].vertex[vertexIndex].ty); } glVertex3f(cx, cy, cz); } glEnd(); } return TRUE; } ===============================

Share this post


Link to post
Share on other sites
izzo    437
Hm it looks okay to me. Are you sure you're loading in the coordinates correctly? I'd check that they are what you expect in your call to glTexCoord. You might also want to check if there are any errors using glGetError.

cheers
sam.

Share this post


Link to post
Share on other sites
zerocide    122
Quote:
Original post by izzo
Hm it looks okay to me. Are you sure you're loading in the coordinates correctly? I'd check that they are what you expect in your call to glTexCoord. You might also want to check if there are any errors using glGetError.

cheers
sam.


Thanks for your reply :)

The coordinates seem to be loading correctly, and there are no errors with glGetError() at the end of InitGL().
I've tried replacing my drawing code with that from NeHe's site after loading the texture:
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Point 1 (Front)
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Point 2 (Front)
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Front)
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Point 4 (Front)

glEnd();

... and it's still tilted when the actual .BMP isn't.

I've posted all the code at:
http://www.zerocide.net/pavel/code.txt

Share this post


Link to post
Share on other sites
Drew_Benton    1861
What happens if you take out:
glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer


I've never seen that called in that fashion before, but then again, I don't do that much OpenGL programming with Normals

Share this post


Link to post
Share on other sites
zerocide    122
Seems like it was one of those "The last place you look" problems... something was wrong with the texture image that I was using... When I tried loading it using NeHe's tutorial #7, it didn't load *at all*. Some other images which I've tested my program with worked perfectly.

Are there any specific restrictions for images to be valid texture images?

Share this post


Link to post
Share on other sites
izzo    437
The glNormal call is perfectly normal (haha!). All he's doing is specifying a normal per quad. In fact the initial value of the normal is (0, 0, 1). Also the normals won't affect his texturing.

Hm why are you calling glEnable(GL_TEXTURE_2D) *after* drawing your quad? That should come before you draw the quad.

I see you call glGenTextures near the start of SetupWorld and store the texture name in the first element of your texture array. Are you sure the first element is being used when you call glBindTexture in your LoadTexture function? Also maybe try taking out the call to gluBuild2DMipmaps.

edit: doh it looks like while I was posting you solved it :)

The thing is, gluBuild2DMipmaps will re-scale the images if they're not power of two, so there must be something else wrong with your images..

sam.

Share this post


Link to post
Share on other sites
zerocide    122
Quote:
Original post by izzo
Hm why are you calling glEnable(GL_TEXTURE_2D) *after* drawing your quad? That should come before you draw the quad.


The code below disables GL_TEXTURE_2D so that I can use a color instead of a texture. I have to enable it after so that textures can be loaded in the future. I agree that it's a little messy, however.. I'll try cleaning up my code later.
if(world1.objects[objectIndex].texture == -1)
{
glDisable(GL_TEXTURE_2D);
glColor3ub(world1.objects[objectIndex].color_r, world1.objects[objectIndex].color_g, world1.objects[objectIndex].color_b);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this