Jump to content

  • Log In with Google      Sign In   
  • Create Account


Count vertex array elements


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 06 October 2012 - 02:37 PM

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?

Sponsor:

#2 Tispe   Members   -  Reputation: 978

Like
0Likes
Like

Posted 06 October 2012 - 03:31 PM


size = count*sizeof(vertex)



for(int i=0; i<count<i++)

{

Temp[i].x = 1337;

}


Edited by Tispe, 06 October 2012 - 03:33 PM.


#3 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 07 October 2012 - 01:12 AM

Thanks, but i need to solve count from an array of vertex. I have the array but cant get the count of it.

#4 kubera   Members   -  Reputation: 858

Like
0Likes
Like

Posted 07 October 2012 - 01:29 AM

Maybe you could use std::vector instead of array.

#5 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
0Likes
Like

Posted 07 October 2012 - 02:23 AM

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?

#6 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 07 October 2012 - 07:20 AM

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]

#7 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
0Likes
Like

Posted 07 October 2012 - 08:41 AM

If you're hard coding your geometry like that then you know how many vertices and indices you have, unless I'm missing something?

#8 kubera   Members   -  Reputation: 858

Like
0Likes
Like

Posted 07 October 2012 - 09:04 AM

I think, that this would be a method:

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


#9 Brother Bob   Moderators   -  Reputation: 7745

Like
2Likes
Like

Posted 07 October 2012 - 09:15 AM

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.

#10 KurtO   Members   -  Reputation: 210

Like
0Likes
Like

Posted 08 October 2012 - 02:10 AM

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

Thank you!

#11 kubera   Members   -  Reputation: 858

Like
0Likes
Like

Posted 08 October 2012 - 04:54 AM

template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

Could you explain, please, how your template works?
(or add an example)

#12 Brother Bob   Moderators   -  Reputation: 7745

Like
2Likes
Like

Posted 08 October 2012 - 06:05 AM


template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

Could you explain, please, how your template works?
(or add an example)

I can interpret your question in two ways:
  • you want to know how to use it, or
  • you want to know how it works and how it is able to determine the size of an array.
I'll just go ahead and answer both, so pick your answer :)

For 1, it is just a function where you pass the array and it returns its size.
int foo[42];
int size = number_of_array_elements(foo);
The variable size is assigned the value 42, because the array foo contains 42 elements.

For 2, you need to know a bit about references to arrays. Normally when you pass an array to a function, you pass a pointer to its the first value in the array along with the size of the array. This way, the function can take any array, but since the array is decayed into a pointer, the size information about the array is also lost which is why you also have to pass its size to the function. Such a function prototype typically looks like this:
void myFunction(int *p, int n);
where p is the pointer and n is the size. The function now knows how many elements that is pointed to by p. You can pass any pointer to any array of any size to this function, but you have to manually track the size of the array.

Another way to pass an array to a function is by reference. The syntax is a little strange if you're not used to it:
void myFunction(int (&a)[42]);
The function now takes a reference to an array of 42 elements. This way, you can only pass an array of 42 elements, but the type of the array (which includes its size) is preserved entirely. In the previous function, the type of the array is partially lost: the type of the elements is implied by the pointer's type, but the array's size is lost in the pointer decay.

Now, a template is introduced to allow the type to vary so that you can pass an array of any type. Furthermore, the size of the array can also be made a template parameter. So replace the type (int in this case) and the size (42 in this case) with generic template parameters.
template<typename T, size_t N>
void myFunction(T (&a)[N]);

Next step is to ask: what do we want the function to do with the array? In this case, we don't want to do anything with the array itself, we just want the function to return the size of the array.
template<typename T, size_t N>
void myFunction(T (&a)[N])
{ return N; }

Last step to match the function I presented above is to remove the name of the parameter. Since we don't use the parameter a anywhere in the function, there is no need to give it a name so we can leave it as an unnamed parameter.
template<typename T, size_t N>
void myFunction(T (&)[N])
{ return N; }
And there it is, the function number_of_array_elements.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS