Advertisement

How to draw indexed cube with texture on every side ?

Started by November 09, 2024 03:31 PM
5 comments, last by Sekt4nt 1 month, 2 weeks ago

Hello everyone! When I draw my cubes now, there is a bug with texture



Here is my vert and ind

TVertexPT vertices[] = {
{{-1.0f,-1.0f,-1.0f}, {0.0f, 1.0f}},
{{1.0f,-1.0f,-1.0f}, {1.0f, 1.0f}},
{{-1.0f,1.0f,-1.0f}, {0.0f, 0.0f}},
{{1.0f,1.0f,-1.0f}, {1.0f, 0.0f}},
{{-1.0f,-1.0f,1.0f}, {1.0f, 0.0f}},
{{1.0f,-1.0f,1.0f}, {0.0f, 0.0f}},
{{-1.0f,1.0f,1.0f}, {1.0f, 1.0f}},
{{1.0f,1.0f,1.0f}, {0.0f, 1.0f}},

{{-4.0f,-4.0f,-4.0f}, {0.0f, 1.0f}},
{{-2.0f,-4.0f,-4.0f}, {1.0f, 1.0f}},
{{-4.0f,-2.0f,-4.0f}, {0.0f, 0.0f}},
{{-2.0f,-2.0f,-4.0f}, {1.0f, 0.0f}},
{{-4.0f,-4.0f,-2.0f}, {1.0f, 0.0f}},
{{-2.0f,-4.0f,-2.0f}, {0.0f, 0.0f}},
{{-4.0f,-2.0f,-2.0f}, {1.0f, 1.0f}},
{{-2.0f,-2.0f,-2.0f}, {0.0f, 1.0f}},
};

unsigned short indices[] = {
0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4,

0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4
};

I need exactly indexed cube.

None

Moving to For Beginners.

What have you tried? What didn't work? What did you do about texture coordinates? Be more specific than “there is a bug with texture”.

Advertisement

@frob I think its need more vertices and indices. Because thats cube works fine if it doesnt need texture, but when we reuse vertex with texture coordinates they start laying incorrect. Maybe you have vertices and indices for textured cube ?

None

How did you reach that? Obviously you need texture information but you have not told us anything about it.

Repeating: What have you tried? What didn't work? What did you do about texture coordinates?

Sekt4nt said:
Maybe you have vertices and indices for textured cube ?

It helps you more to figure out what's the problem with yours, and then fixing it.

Seems you have two cubes in the data, both made from 8 vertices.
Each of the 8 vertices has one 3D position, and one 2D UV coordinate.

This can not represent a cube with the texturing you expect.
The reason is: You can not wrap a piece of paper (the texture, with a topology like a disc) around the cube (topology of a sphere) without having folds in the paper.
To make it fit well, you may take a scissor to cut the paper, so you get a shape like this for example:

You can wrap this piece of paper around the cube, and you get the desired result of texturing without distortion or stretching.

Now look closely at the cube.
Some edges (those at the bounmdary of our cross shaped cut) show a texture seam, where the image on one face does not continue to the image of the other face.
And this also means that at some corners (again those at the boundary) have different UV coords on those corners, depending on to which face they belong.

If you can imagine and follow til here, it becomes obvious what you need to change:
You need to duplicate the vertices, so each duplicate can have it's own UV coord.
(Due to technical reasons, we usually duplicate the whole vertex, so the 3D coordinate as well, to keep things simple.)
So the typical layout of a textured cube is 4 unique vertices for each cube face, giving 24 vertices in total.
Notice this is not only due to texturing, but we have the same problem with normals as well: Corner vertices have the same position but different normals depending on their face.

We can ofc. optimize to bring this number down, and thus you will likely find different examples of cube data if you google for it.

@JoeJ exactly, now I found correct solution

TVertexPT vertices[] = {
{{-1.0f, -1.0f, 1.0f}, {0.0f, 1.0f}},
{{ 1.0f, -1.0f, 1.0f}, {1.0f, 1.0f}},
{{ 1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},

{{ 1.0f, -1.0f, -1.0f}, {0.0f, 1.0f}},
{{-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f}},
{{-1.0f, 1.0f, -1.0f}, {1.0f, 0.0f}},
{{ 1.0f, 1.0f, -1.0f}, {0.0f, 0.0f}},

{{-1.0f, -1.0f, -1.0f}, {0.0f, 1.0f}},
{{-1.0f, -1.0f, 1.0f}, {1.0f, 1.0f}},
{{-1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-1.0f, 1.0f, -1.0f}, {0.0f, 0.0f}},

{{ 1.0f, -1.0f, 1.0f}, {0.0f, 1.0f}},
{{ 1.0f, -1.0f, -1.0f}, {1.0f, 1.0f}},
{{ 1.0f, 1.0f, -1.0f}, {1.0f, 0.0f}},
{{ 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},

{{-1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
{{ 1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{ 1.0f, 1.0f, -1.0f}, {1.0f, 0.0f}},
{{-1.0f, 1.0f, -1.0f}, {0.0f, 0.0f}},

{{-1.0f, -1.0f, -1.0f}, {0.0f, 1.0f}},
{{ 1.0f, -1.0f, -1.0f}, {1.0f, 1.0f}},
{{ 1.0f, -1.0f, 1.0f}, {1.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, 0.0f}}
};

unsigned short indices[] = {
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23
};

None

This topic is closed to new replies.

Advertisement