How can that be achieved? I need somehting similar to strlen, but for any array. Can I use strlen for that matter?
Printable View
How can that be achieved? I need somehting similar to strlen, but for any array. Can I use strlen for that matter?
That didn't work, it returned 1, it should be 24 according to what I counted.
My array is type of int*, here is the code:
Code:int *indices=NULL;
...
...
int ind[] = {1,2,3,4,
2,4,2,1,
.........
};
//Then I do
indices = ind;
//(is this correct?)
strlen() only works for null terminated char array (ie, strings).
Arrays must have a constant size throughout a program. However, not every available spot in memory needs to be occupied. For example:
int array[3] will hold up to 3 ints. However:
array[0] = 0;
array[1] = 1;
works fine, even if array[2] isn't used. To keep track of how many items are actually in the array I usually use a counting variable. Then the fun starts because the maximal size is often called the capacity and the actual size is called size, which is actually a counting variable.
int array[10];
int size = sizeof(array)/sizeof(array[0]);
I believe will give you size = 10
but why bother? You already know what the size is, one way or the other. I suspect you want to know how many values are actually in the array, not what the maximal number of elements could be.
That is what I do. Well, I think I should better post the whole class code, it is not that big.
Then, I use PassVertices with 2 arrays, one my type, Vec3 which consists of x,y,z floats and the int one, which describes the indices. This is something:Code://class:
class RT3DAPI CRTRenderer
{
public:
CRTRenderer()
{
vertices=NULL;
indices=NULL;
}
void PassVertices(void* vertices, int* indices);
void Render(rtPrimType PT);
private:
void* vertices;
int* indices;
};
//cpp file: code:
void CRTRenderer::PassVertices(void* vertices, int* indices)
{
this->vertices = vertices;
this->indices = indices;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, this->vertices);
}
void CRTRenderer::Render(rtPrimType PT)
{
//find how many items are in the array:
int size=0;
size = sizeof(this->indices) / sizeof(this->indices[0]);
char test[100];
sprintf(test, "%i", size);
MessageBox(0,test,"",0);
glDrawElements(PT, size, GL_UNSIGNED_INT, this->indices);
}
Thats it.Code:Vec3* g_cube = new Vec3[8];
g_cube[0] = Vec3(-1,1,1);
g_cube[1] = Vec3(1,1,1);
g_cube[2] = Vec3(1,-1,1);
g_cube[3] = Vec3(-1,-1,1);
g_cube[4] = Vec3(-1,1,-1);
g_cube[5] = Vec3(1,1,-1);
g_cube[6] = Vec3(1,-1,-1);
g_cube[7] = Vec3(-1,-1,-1);
int g_ind[] = {0,1,2,3,
4,5,1,0,
3,2,6,7,
5,4,7,6,
1,5,6,2,
4,0,3,7};
renderer.PassVertices(g_cube, g_ind);
renderer.Render(RTPT_QUADS);
to see if it works how about placing code aftet the declaration of g_ind[]?
int g_ind[] = {0,1,2,3,4,5,1,0,3,2,6,7,5,4,7,6,1,5,6,2,4,0,3,7};
int size = sizeof(g_ind)/sizeof(g_ind[0]);
cout << size << endl;
if it works there but it doen't work in:
void CRTRenderer::Render(rtPrimType PT)
then I would try changing this:
void CRTRenderer::PassVertices(void* vertices, int* indices)
{
this->vertices = vertices;
this->indices = indices;
to this:
void CRTRenderer::PassVertices(void* vertices, int* _indices)
{
this->vertices = vertices;
this->indices = new int(sizeof( _indices)/sizeof( _indices[0]);
The one you said worked, but as you said it didnt work at the class using the int*.
I changed it to what you said, using new, but I still have the same result. Here is my changed class code:
Maybe there is a mistake in the Render method, calculating the size? I dont know.. But we are vreally close... Why wouldnt it work with an int* and it would work as the g_ind is defined? I dont get that.Code:void CRTRenderer::PassVertices(void* vertices, int* indices)
{
this->vertices = vertices;
this->indices = new int(sizeof(indices)/sizeof(indices[0]));//indices;
this->indices = indices;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, this->vertices);
}
void CRTRenderer::Render(rtPrimType PT)
{
//find how many items are in the array:
int size=0;
size = sizeof(this->indices) / sizeof(this->indices[0]);
char test[100];
sprintf(test, "%i", size);
MessageBox(0,test,"",0);
....
}
don't know for sure. suspect compiler having problems separated the array you pass in called indices and the member variable you call indices. That's why I changed the name of the passed in array called indices to _indices. I'd place the underscores as in my code in your code to see if it makes a difference explicitly telling the compiler which "indices" it should use.
I did that after posting this reply, but it had the same result. Any other method of finding the num of items in the array?
passing the size is a time honored technique. Adjust the declaration and definition of Render to include an int parameter, calculate size of g_ind before passing it to Render, and pass the size as a parameter.
int g_ind[] = {0,1,2,3,4,5,1,0, 3,2,6,7,5,4,7,6,1,5,6,2,4,0,3,7};
int size = sizeof(g_ind)/sizeof(g_ind[0]);
renderer.PassVertices(g_cube, g_ind);
renderer.Render(RTPT_QUADS, size);
Yea, I believe I will do it like that if I can't find the solution.
I wanted the Renderer to automatically get the size, however, passing the size as a parameter could be beneficial in the end :) Anyway, thanks everyone for your help! I might come back to this matter at another topic.