I think the problem is in how you call the function :
float vertices[60];
unsigned int indices[60];
icosahedron(radius, vertices);
subdivide(vertices, indices, 1, sizeof(vertices), sizeof(indices), radius);
The
sizeof
operator returns the number of bytes in an item, not the number of array elements so the value passed to subdivide is 240, not 60. There is a
_countof
macro available in many (most?) compilers that will do that. If it is not available in yours, here is how it is implemented :
#ifndef _countof
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#endif
Another option is to use a constant value like this :
const int VertexCount=60;
float vertices[ VertexCount ];
unsigned int indices[ VertexCount ];
subdivide( vertices, indices, 1, VertexCount, VertexCount, radius );
I always tell the compiler to use warning level 4 with my code and it would tell me that in the function :
void subdivide(float* vertices, unsigned int* indices, int subdivision_amount, size_t vertices_arr, size_t indices_arr, int radius)
the arguments
vertices_arr
and
indices_arr
are not used. On second thought, that means this is not the problem. Not directly, that is.
As has been mentioned, I think your best is to use a debugger or, at least, to do some printf-style debugging and display every index used in the code.
ETA: I was curious so I had a go at this and I found a few problems. One is the indices array was not initialized so it had invalid values. Another is null pointers were passed to add_indices. I adjusted the code to this and it runs but I'm not sure it works correctly :
void subdivide( float* vertices
, unsigned int* indices
, int subdivision_amount
, int radius
, int vertCount
)
{
float new_v1[3], new_v2[3], new_v3[3];
float *v1, *v2, *v3;
unsigned int index;
for( int i = 1; i <= subdivision_amount; ++i )
{
index = 0;
for( int j = 0; j < vertCount; j += 3 )
{
v1 = & vertices[ indices[ j ] * 3 ];
v2 = & vertices[ indices[ j + 1 ] * 3 ];
v3 = & vertices[ indices[ j + 2 ] * 3 ];
compute_half_vertex( v1, v2, new_v1, radius );
compute_half_vertex( v2, v3, new_v2, radius );
compute_half_vertex( v1, v3, new_v3, radius );
add_vertices( v1, new_v1, new_v3, vertices );
add_vertices( new_v1, v2, new_v2, vertices );
add_vertices( new_v1, new_v2, new_v3, vertices );
add_vertices( new_v3, new_v2, v3, vertices );
add_indices( index, index + 1, index + 2, indices );
add_indices( index + 3, index + 4, index + 5, indices );
add_indices( index + 6, index + 7, index + 8, indices );
add_indices( index + 9, index + 10, index + 11, indices );
index += 12;
}
}
}
void TestSubdivide()
{
const int VertCount = 60;
int radius = 1;
float vertices[ VertCount ] = { 0 };
unsigned int indices[ VertCount ] = { 0 };
icosahedron( radius, vertices );
subdivide( vertices, indices, 1, radius, VertCount );
}
The
indices
array never gets set to anything other than zeros but the
vertices
array has some non-zero values in it. I am not sure if this is what you want or not. Also, I don't see what effect the
subdivision_amount
parameter will have.