Sign in to follow this  
Medo Mex

3D Box Texture Coordinates

Recommended Posts

I have 3D box with the following vertices:

CUSTOM_VERTEX vertices[] = 
{
    {  D3DXVECTOR3(-1.0f, 1.0f, -1.0f), Texcoord[0], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(1.0f, 1.0f, -1.0f), Texcoord[1], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(1.0f, 1.0f, 1.0f), Texcoord[2], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(-1.0f, 1.0f, 1.0f), Texcoord[3], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(-1.0f, -1.0f, -1.0f), Texcoord[4], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(1.0f, -1.0f, -1.0f), Texcoord[5], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(1.0f, -1.0f, 1.0f), Texcoord[6], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
    {  D3DXVECTOR3(-1.0f, -1.0f, 1.0f), Texcoord[7], D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
}

 

and indices as the following:

DWORD indices[] =
{
3,1,0,
2,1,3,

0,5,4,
1,5,0,

3,4,7,
0,4,3,

1,6,5,
2,6,1,

2,7,6,
3,7,2,

6,4,5,
7,4,6,
};

Now, what values should I put in the array Texcoord[] so I can have a valid texture coordinate for the box?

 

I tried to put different texture coordinates but I can't get the box to appear as expected.

 

According to what I know the texture coordinates for the box should be as the following:

[attachment=25660:TextureCoordSys.jpg]

 

Which means the following should form one side of the box:

D3DXVECTOR2(0.0f, 0.0f) // Vertex: 1
D3DXVECTOR2(1.0f, 0.0f) // Vertex: 2
D3DXVECTOR2(0.0f, 1.0f) // Vertex: 3
D3DXVECTOR2(1.0f, 1.0f) // Vertex: 4
If I'm correct, what texture coordinates values should I put for Vertex 5, 6, 7, 8?

Share this post


Link to post
Share on other sites

I tried to put different texture coordinates but I can't get the box to appear as expected.

 

 

If, by "expected," you mean the texture appearing correctly on each of the six faces - alas, it can't be done with just 8 vertices, each with a unique set of tex coords.

 

Consider, for the texture to appear correctly, the tex coords (0,0) and (1,1) must be assigned to vertices on one of the diagonals of the face.

 

In the image below, you see the "front" face with tex coords assigned to the four corners of the face. That assignment forces the tex coords on the face to the right to be as shown. I.e., (0, 0) must be on the diagonal from (1,1). So far, so good. The front face texture will appear left-to-right, and, on the face to the right, right-to-left. If the texture is symmetrical, you're still good.

 

But.. the top face (3 of 4 vertices shown) is already incorrect. Two of the diagonal vertices have the same tex coords ( 0, 1 ). Also, if you can envision the bottom face, there are already 2 vertices with tex coords ( 0, 0 ).

 

[attachment=25661:box_tcs.png]

 

IF you want to be able to orient the texture on each face, the simplest solution is to use 4 vertices per face (24 vertices). You can then assign tex coords to each face as desired. Also, in future, if you want to use vertex lighting, you can assign the correct normal to each vertex.

 

If you're willing to have the texture reversed left-to-right on some faces, you can use fewer vertices - duplicate vertex positions where needed, but with tex coords to orient the texture correctly. I've not done that as 1) I can't see a clear advantage over the simpler approach, and 2) I'm too lazy to figure out the indexing.

 

EDIT: Just by inspection, and assuming you can accept left-to-right and right-to-left orientations of the texture, it appears you can use 12 vertices - 8 for the "box" without a top or bottom; 2 new vertices + 2 of the original 8 vertices for the top, and the same for the bottom. 8 "box" vertices, and 2 "extra" vertices each for the top and bottom. If you ever decide you want to use a texture that requires a specific orientation (i.e., contains text which doesn't mirror well), it's simpler to just stick with 24.

Edited by Buckeye

Share this post


Link to post
Share on other sites

There is a very good example of how to do this from Introduction to 3D Game Programming with Direct3D 11.0.  You can download the source code from there (it's the Source Code Set 1 link).  In the zipfile open up Common/GeometryGenerator.cpp and look at the CreateBox function.

 

This uses the 24-vertex approach that Buckeye mentions.  It also has functions for generating vertices and indices for spheres, cylinders and geospheres as well.  

Share this post


Link to post
Share on other sites

Something weird is happening, the box is getting another shape if I moved the camera to look at the box from certain angels:

[attachment=25665:4848.png]

 

The same box from another camera angel:

[attachment=25666:12641.png]

Edited by Medo3337

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