• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #ActualYann ALET

Posted 10 September 2013 - 06:52 PM

Well they all have access to the full atlas which contains all possible textures for a cube, but i still need set UV coordinates for each vertex of my cube.

I'll make things easier (because i am terrible at explaining things) by submitting my atlas and my code.

Here is the old atlas i was using :

And i initialized vertices of my unique cube type like such :

float oneQuarter = (float)1/3;
float twoQuarter = (float)2/3;
Vertex::Basic32 vertices[] =
{
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 1) },     // 0 - front face  - bottom left
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },            // 1 - left face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 0) },            // 2 - bottom face - top left -> clone

{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 0) },     // 3 - front face  - top left
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },            // 4 - left face   - top right
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },     // 5 - top face    - bottom left

{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 0) },            // 6 - front face  - top right
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },     // 7 - right face  - top left
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },     // 8 - top face    - bottom right

{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 1) },			  // 9 - front face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 10 - right face  - bottom left
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 11 - bottom face - top right

{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 12 - left face   - bottom left
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 1) },           // 13 - back face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 1) },           // 14 - bottom face - bottom left

{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 15 - left face   - top left
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 16 - top face    - top left  -> clone
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 0) },			  // 17 - back face   - top right

{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },           // 18 - right face  - top right
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 19 - top face    - top right -> clone
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 0) },    // 20 - back face   - top left

{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },           // 21 - right face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },    // 22 - bottom face - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 1) },    // 23 - back face   - bottom left
};


Now, i have added a new texture to my atlas :

So, i changed the above code to calculate UV based on texture index in the atlas (maybe that's not the right approach, i don't know :s).

void GeometryBuilder::MakeCube(Geometry& box, int bottomTextureIndex, int topTextureIndex, int sideTextureIndex)
{
int numberOfTextures = 4;
float textureWidth = 1.0f / numberOfTextures;

Vertex::Basic32 vertices[] =
{
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 1) },     // 0 - front face  - bottom left
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },            // 1 - left face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 0) },            // 2 - bottom face - top left -> clone

{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 0) },     // 3 - front face  - top left
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 4 - left face   - top right
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 1) },     // 5 - top face    - bottom left

{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 6 - front face  - top right
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },     // 7 - right face  - top left
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 1) },     // 8 - top face    - bottom right

{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },			  // 9 - front face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 10 - right face  - bottom left
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 0) },    // 11 - bottom face - top right

{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 12 - left face   - bottom left
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 13 - back face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 1) },           // 14 - bottom face - bottom left

{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 15 - left face   - top left
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 0) },    // 16 - top face    - top left  -> clone
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },			  // 17 - back face   - top right

{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },           // 18 - right face  - top right
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 0) },    // 19 - top face    - top right -> clone
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 20 - back face   - top left

{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 21 - right face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 1) },    // 22 - bottom face - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 23 - back face   - bottom left
};

// other stuff...
}


This works, but forces me to call MakeCube for each type of cubes, and call DrawIndexedInstanced for each one of those.

### #1Yann ALET

Posted 10 September 2013 - 06:50 PM

Well they all have access to the full atlas which contains all possible textures for a cube, but i still need set UV coordinates for each vertex of my cube.

I'll make things easier (because i am terrible at explaining things) by submitting my atlas and my code.

Here is the old atlas i was using :

And i initialized vertices of my unique cube type like such :

float oneQuarter = (float)1/3;
float twoQuarter = (float)2/3;
Vertex::Basic32 vertices[] =
{
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 1) },     // 0 - front face  - bottom left
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },            // 1 - left face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 0) },            // 2 - bottom face - top left -> clone

{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 0) },     // 3 - front face  - top left
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },            // 4 - left face   - top right
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },     // 5 - top face    - bottom left

{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 0) },            // 6 - front face  - top right
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },     // 7 - right face  - top left
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },     // 8 - top face    - bottom right

{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 1) },			  // 9 - front face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 10 - right face  - bottom left
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 11 - bottom face - top right

{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 12 - left face   - bottom left
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 1) },           // 13 - back face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 1) },           // 14 - bottom face - bottom left

{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 15 - left face   - top left
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 16 - top face    - top left  -> clone
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 0) },			  // 17 - back face   - top right

{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },           // 18 - right face  - top right
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 19 - top face    - top right -> clone
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 0) },    // 20 - back face   - top left

{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },           // 21 - right face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },    // 22 - bottom face - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 1) },    // 23 - back face   - bottom left
};


Now, i have added a new texture to my atlas :

So, i changed the above code to calculate UV based on texture index in the atlas (maybe that's not the right approach, i don't know :s).

void GeometryBuilder::MakeCube(Geometry& box, int bottomTextureIndex, int topTextureIndex, int sideTextureIndex)
{
int numberOfTextures = 4;
float textureWidth = 1.0f / numberOfTextures;

Vertex::Basic32 vertices[] =
{
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 1) },     // 0 - front face  - bottom left
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },            // 1 - left face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 0) },            // 2 - bottom face - top left -> clone

{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 0) },     // 3 - front face  - top left
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 4 - left face   - top right
{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 1) },     // 5 - top face    - bottom left

{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 6 - front face  - top right
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },     // 7 - right face  - top left
{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 1) },     // 8 - top face    - bottom right

{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },			  // 9 - front face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 10 - right face  - bottom left
{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 0) },    // 11 - bottom face - top right

{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 12 - left face   - bottom left
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 13 - back face   - bottom right
{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 1) },           // 14 - bottom face - bottom left

{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 15 - left face   - top left
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 0) },    // 16 - top face    - top left  -> clone
{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },			  // 17 - back face   - top right

{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },           // 18 - right face  - top right
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 0) },    // 19 - top face    - top right -> clone
{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 20 - back face   - top left

{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 21 - right face  - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 1) },    // 22 - bottom face - bottom right
{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 23 - back face   - bottom left
};

// other stuff...
}


This works, but forces me to call MakeCube for each type of cubes, and call DrawIndexedInstanced for one of those.

PARTNERS