Sign in to follow this  

Texturing a Simple Cube

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

It just occured to me that I have not yet been able to successfully texture a cube (not the mesh one). I have the following segment of code that sets up the cubes vertex information.

// Set Up Vertices
gpCube->AddPosition(stCoords(-1.0f,-1.0f,-1.0f));
gpCube->AddPosition(stCoords(-1.0f, 1.0f,-1.0f));
gpCube->AddPosition(stCoords( 1.0f, 1.0f,-1.0f));
gpCube->AddPosition(stCoords( 1.0f,-1.0f,-1.0f));
gpCube->AddPosition(stCoords(-1.0f,-1.0f, 1.0f));
gpCube->AddPosition(stCoords(-1.0f, 1.0f, 1.0f));
gpCube->AddPosition(stCoords( 1.0f, 1.0f, 1.0f));
gpCube->AddPosition(stCoords( 1.0f,-1.0f, 1.0f));

// Set Up Indices for Vertices
gpCube->AddIndex(0);	gpCube->AddIndex(1);	gpCube->AddIndex(2);
gpCube->AddIndex(0);	gpCube->AddIndex(2);	gpCube->AddIndex(3);
gpCube->AddIndex(4);	gpCube->AddIndex(6);	gpCube->AddIndex(5);
gpCube->AddIndex(4);	gpCube->AddIndex(7);	gpCube->AddIndex(6);
gpCube->AddIndex(4);	gpCube->AddIndex(5);	gpCube->AddIndex(1);
gpCube->AddIndex(4);	gpCube->AddIndex(1);	gpCube->AddIndex(0);
gpCube->AddIndex(3);	gpCube->AddIndex(2);	gpCube->AddIndex(6);
gpCube->AddIndex(3);	gpCube->AddIndex(6);	gpCube->AddIndex(7);
gpCube->AddIndex(1);	gpCube->AddIndex(5);	gpCube->AddIndex(6);
gpCube->AddIndex(1);	gpCube->AddIndex(6);	gpCube->AddIndex(2);
gpCube->AddIndex(4);	gpCube->AddIndex(0);	gpCube->AddIndex(3);
gpCube->AddIndex(4);	gpCube->AddIndex(3);	gpCube->AddIndex(7);

// Set Up Texture Co-ordinates
for (int i=0; i < 36; i+=4)
{
	gpCube->AddTexture(stTextures(0.0f,0.0f));
	gpCube->AddTexture(stTextures(1.0f,0.0f));
	gpCube->AddTexture(stTextures(1.0f,1.0f));
	gpCube->AddTexture(stTextures(0.0f,1.0f));
}


I'm sure there is something else that needs to be done but all that displays with the texture is the front face and back face and the sides and top and bottom are ignored. Its not really that important but it would be good to know what I am doing wrong as its annoying me no end that regardless of what I do it refuses to work. I am suspecting the problem is due to using 8 points and 36 indices to match them up as a single point would have different texture coords depending on which face it is on. If someone can confirm this I can at least move on with the next stage of my sample project and forget about this slight problem.

Share this post


Link to post
Share on other sites
Yes, a vertex is more than just position, it's the entire vertex structure, including normals, and UV coordinates. A cube has unique normals on each cube-face, and often unique UVs, so no vertices can be shared between different faces of the cube. A single cube-face can share vertices between it's triangles though. Each cube-face needs 4 vertices, and there are 6 cube-faces, so you need a minimum of 24 vertices to texture or light the cube properly.

Share this post


Link to post
Share on other sites
Too slow!! [headshake]

Quote:
Original post by Xrystal
I am suspecting the problem is due to using 8 points and 36 indices to match them up as a single point would have different texture coords depending on which face it is on. If someone can confirm this I can at least move on with the next stage of my sample project and forget about this slight problem.
Yeah, you've basically got it. Indices allow you to re-use IDENTICAL vertices only and the ones you're trying to define are only SIMILAR. You actually have 24 unique vertices if you want a different texture per cube face.

You can reduce it down further by unwrapping it like the cube-mesh that you might have made in school as a kid, but its not really worth the hassle [wink]

hth
Jack

Share this post


Link to post
Share on other sites

This topic is 3846 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.

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