There is this typedef for openGL
typedef struct
{
float Position[3];
float Color[4];
} Vertex;
The example hard-codes the postions and colors which works:
Vertex Vertices[] =
{
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {1, 0, 0, 1}},
{{-1, 1, 0}, {0, 1, 0, 1}},
{{-1, -1, 0}, {0, 1, 0, 1}},
{{1, -1, -1}, {1, 0, 0, 1}},
{{1, 1, -1}, {1, 0, 0, 1}},
{{-1, 1, -1}, {0, 1, 0, 1}},
{{-1, -1, -1}, {0, 1, 0, 1}}
};
I want to NOT hard-code my colors and positions so that I can create a class with a constructor for my OpenGL drawing data.
I was wondering why constructing my data as below doesn't work and what the correct way to do it would be.
Vertices = malloc(sizeof(Vertex)*8); //make 8 vertice pointers
Vertices[0].Position[0] = 1; Vertices[0].Position[1] = -1; Vertices[0].Position[2] = 0;
Vertices[0].Color[0] = 1; Vertices[0].Color[1] = 0; Vertices[0].Color[2] = 0; Vertices[0].Color[3]= 1;
Vertices[1].Position[0] = 1; Vertices[1].Position[1] = 1; Vertices[1].Position[2] = 0;
Vertices[1].Color[0] = 1; Vertices[1].Color[1] = 0; Vertices[1].Color[2] = 0; Vertices[1].Color[3]= 1;
Vertices[2].Position[0] = -1; Vertices[2].Position[1] = 1; Vertices[2].Position[2] = 0;
Vertices[2].Color[0] = 0; Vertices[2].Color[1] = 1; Vertices[2].Color[2] = 0; Vertices[2].Color[3]= 1;
Vertices[3].Position[0] = -1; Vertices[3].Position[1] = -1; Vertices[3].Position[2] = 0;
Vertices[3].Color[0] = 0; Vertices[3].Color[1] = 1; Vertices[3].Color[2] = 0; Vertices[3].Color[3]= 1;
Vertices[4].Position[0] = 1; Vertices[4].Position[1] = -1; Vertices[4].Position[2] = -1;
Vertices[4].Color[0] = 1; Vertices[4].Color[1] = 0; Vertices[4].Color[2] = 0; Vertices[4].Color[3]= 1;
Vertices[5].Position[0] = 1; Vertices[5].Position[1] = 1; Vertices[5].Position[2] = -1;
Vertices[5].Color[0] = 1; Vertices[5].Color[1] = 0; Vertices[5].Color[2] = 0; Vertices[5].Color[3]= 1;
Vertices[6].Position[0] = -1; Vertices[6].Position[1] = 1; Vertices[6].Position[2] = -1;
Vertices[6].Color[0] = 0; Vertices[6].Color[1] = 1; Vertices[6].Color[2] = 0; Vertices[6].Color[3]= 1;
Vertices[7].Position[0] = -1; Vertices[7].Position[1] = -1; Vertices[7].Position[2] = -1;
Vertices[7].Color[0] = 0; Vertices[7].Color[1] = 1; Vertices[7].Color[2] = 0; Vertices[7].Color[3]= 1;
Although printing out both data structures look identical with
NSLog(@"print it");
for(unsigned int i = 0; i < 8; i ++)
{
printf("i: %f\t%f\t%f\t%f\t%f\t%f\t%f\n", Vertices[i].Position[0], Vertices[i].Position[1], Vertices[i].Position[2], Vertices[i].Color[0], Vertices[i].Color[1], Vertices[i].Color[2], Vertices[i].Color[3]);
}
my open GL cube does not draw