Count vertex array elements

Started by
10 comments, last by Brother Bob 11 years, 6 months ago
I have a problem with counting the elements in a vertex array.

I have a vertex array like this

Vertex temp = {
{ x,y,z,norm,u,v,color}
---||----

Here i can use sizeof to get the right number of elements to create my vertexbuffer.
But when i declare my xertex array like this:

Vertex* temp;
Temp = new vertex[count];
Foreach
.....

Then i cant use size of?

Any tips?
Advertisement

size = count*sizeof(vertex)

for(int i=0; i<count<i++)
{
Temp.x = 1337;
}
Thanks, but i need to solve count from an array of vertex. I have the array but cant get the count of it.
Maybe you could use std::vector instead of array.
You need to either manually keep track of the array size or use sonething like std::vector. Is there any reason why you aren't using the latter?
Dont know how i can sent data to my buffer otherwise?
this is the geometry i want to calculate count on.

VERTEX OurVertices[] =
{
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 0.0f}, // side 1
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 0.0f}, // side 2
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 0.0f}, // side 3
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 1.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 0.0f}, // side 4
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 5
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 6
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
};
DWORD OurIndices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
[source lang="cpp"][/source]
If you're hard coding your geometry like that then you know how many vertices and indices you have, unless I'm missing something?
I think, that this would be a method:


const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX)

I think, that this would be a method:


const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX)


Consider replacing the calculation with the following function:

template<typename T, size_t N>
size_t number_of_array_elements(T (&)[N])
{
return N;
}

It will return the number of elements for an array, and will error out at compile time if you don't pass it an array. Your code, for example, will fail silently and calculate an incorrect size value if you try to use it with a pointer instead of an array.
const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX) = perfect

Thank you!

This topic is closed to new replies.

Advertisement