Public Group

# Texture Mapping on Square

This topic is 3852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I was looking at the NeHe tutorials (http://nehe.gamedev.net ) and got to Lesson 6. The lesson shows you how to apply a texture map to all faces of a cube; the thing is, I want it on a square so I can use it as a background. This is the code for the cube.
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer

glTranslatef(0.0f,0.0f,-5.0f);              // move 5 units into the screen.

glRotatef(xrot,1.0f,0.0f,0.0f);		// Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f);		// Rotate On The Y Axis
glRotatef(zrot,0.0f,0.0f,1.0f);		// Rotate On The Z Axis

glBindTexture(GL_TEXTURE_2D, texture[0]);   // choose the texture to use.

glBegin(GL_QUADS);		                // begin drawing a cube

// Front Face (note that the texture's corners have to match the quad's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad

// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad

// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad

// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad

glEnd();                                    // done with the polygon.

xrot+=15.0f;		                // X Axis Rotation
yrot+=15.0f;		                // Y Axis Rotation
zrot+=15.0f;		                // Z Axis Rotation

// since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers();
}

Can some one change that so it will display the square for me? Whenever I try to edit it, I always just get a white square. Thanks.

##### Share on other sites
Just use the front face geometry from what you posted:

// Front Face (note that the texture's corners have to match the quad's corners)glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and QuadglTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and QuadglTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and QuadglTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad

##### Share on other sites
You might find that a better way to approach programming help forums like this one would be to include the code you have (post editing) and ask "what am I doing wrong" rather than "will someone fix this for me." Others are more likely to help you if they believe you'll understand the solution and won't come back to ask the same or similar questions.

As for your question, simply keeping the "front" face and deleting the calls that draw the other five faces of the cube would be sufficient in this case, but you would probably also want to adjust the "origin" of your quad later so it rotates about its center rather than the center of an imaginary cube.*

If you're still seeing a white square, ensure that your texture is accessible by your program, and check any output from the program for error messages. Unless you changed the "glTexture2f" calls, then the texture mapping shouldn't be a problem.

* Alternatively you could simply remove the rotation code as long as you ensure that you keep the face that's facing the camera.

##### Share on other sites
I understand it, I think I just had something wrong in my code.
I have just started over and used the front face and now it is all working.
(Sorry I didn't post my code, I am sort of a noob at programming forums)
Thanks a lot for your help. It works wonderfully! :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015445
×