I have been referring to this post Drawing Sphere in OpenGL without using gluSphere()? which has helped me quite a lot but i'm stumped now.
I have an octahedron in my scene and I would now like to recursively subdivide the triangles to create a sphere. I found this block of code which is supposed to carry out the subdivision for me but I don't understand it fully
void subdivide(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth)
{
GLfloat v12[3], v23[3], v31[3]; int i;
if (depth == 0) {
drawTriangle(v1, v2, v3);
return;
}
for (i = 0; i < 3; i++)
{
v12[i] = (v1[i]+v2[i])/2.0;
v23[i] = (v2[i]+v3[i])/2.0;
v31[i] = (v3[i]+v1[i])/2.0;
}
I set up a struct to hold a position, normal and a color
// simple vertex container
struct SimpleVertex
{
vec3 pos; // Position
vec3 normal // Normal
vec4 colour; // Colour
};
and this is where I set up my vertices
/*
*
* This is just one triangle form my octahedron
*
*/
static void createVertexBuffer()
{
// Create some vertices to put in our VBO.
// Create vertex buffer
SimpleVertex vertices[] =
{
// Side 1 Front
{ vec3(0.0f, 1.0f, 1.0f), vec3(0.0f, 0.0f, 1.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f) },
{ vec3(-1.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f) },
{ vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f) },
}
}
If anyone could explain how I could use the method above to subdivide my triangles, i'd appreciate it.