在没有硬编码的情况下构造typedef

时间:2015-10-11 05:05:27

标签: ios opengl vertex

openGL有这个typedef

typedef struct
{
float Position[3];
float Color[4];
} Vertex;

该示例对有效的位置和颜色进行硬编码:

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}}
};

我不想硬编码我的颜色和位置,这样我就可以用OpenGL绘图数据的构造函数创建一个类。

我想知道为什么如下构建我的数据并不起作用以及正确的方法是什么。

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;

虽然打印出两个数据结构看起来与

相同
    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]);
}

我的开放式GL立方体不会绘制

3 个答案:

答案 0 :(得分:3)

由于该问题的作者在Sami Kuhmonen的问题评论中发布了用于源代码的链接,因此可以识别该问题:

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

您无法通过Vertex Vertices[8]交换Vertex* Vertices,并希望这仍然有用。在C / C ++中,数组和指针不是相同的东西。数组上的sizeof将以字节为单位返回整个数组的大小(在本例中为8*sizeof(Vertex),而指针上的sizeof返回指针的大小(通常为4或8,在一个非正常的平台上。)所以你目前只将前1或2个浮点数加载到缓冲区中。

答案 1 :(得分:2)

首先,不要使用sizeof(int)来获取指针的大小。使用适当的类型,在本例中为sizeof(Vertex*)

但主要问题在于内存分配。你没有指针数组,你有一个数组。所以你应该根据它分配内存。如果你想要八个顶点并假设你有Vertex* Vertices,那么

Vertices = malloc(sizeof(Vertex) * 8);

这会分配足够的内存来将它们存储在一个块中。

答案 2 :(得分:0)

作为对来自Sami和Derhas的所有重要反馈的回复,当我执行以下“不硬编码”顶点或颜色从一开始时,代码对我有效(所以我可以稍后将此在一个立方体类中,它将位置和颜色作为我的构造函数的输入)

Vertex Vertices[8];

.....

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;


....

- (void)setupVBOs
{
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

//works with Vertices[]
//glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices)*8, Vertices, GL_STATIC_DRAW);

GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}