您不能一次绑定 2 个顶点数组 (glBindVertexArrayOES)。您只需要创建 1 个,然后将所有数据放入其中。试试这个方法:
glGenVertexArraysOES(1, &_vertexArray); //create vertex array
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts) + sizeof(norms), NULL, GL_STATIC_DRAW); //create vertex buffer big enough for both verts and norms and pass NULL as data..
uint8_t *ptr = (uint8_t *)glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES); //map buffer to pass data to it
memcpy(ptr, verts, sizeof(verts)); //copy verts
memcpy(ptr+sizeof(verts), norms, sizeof(norms)); //copy norms to position after verts
glUnmapBufferOES(GL_ARRAY_BUFFER);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); //tell GL where verts are in buffer
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(verts))); //tell GL where norms are in buffer
glBindVertexArrayOES(0);
至于检查或计算法线:
您可以使用叉积计算和检查法线是否有锋利的边缘(这是您的情况)。对于由位置 A、B 和 C 定义的每个三角形:
s1 = B-A //s1 and s2 are just for easier reading
s2 = C-A
N = s1 x s2 //N is normal (not quite ready)
(cross product:)
N.x = s1.y*s2.z - s2.y*s1.z
N.y = s1.z*s2.x - s2.z*s1.x
N.z = s1.x*s2.y - s2.x*s1.y
由于 ABC 可以以任何方式定向,因此您可能需要以另一种方式转向正常。您可以使用点积来获取此信息。在您的情况下,取一个靠近形状中心且不位于其上的点定义它的任何三角形(向量 Center(0,0,0) 可能会这样做)。现在对于每个 A、B、C、N:
vector Average = (A+B+C)/3 //center of a triangle
if(dotProduct(N, Center-Average) > 0) N = N * -1
(dotProduct:)
dot(M,N) = M.x*N.x + M.y*N.y + M.z*N.z
之后,您应该对每个法线进行归一化(将每个分量除以法线的长度,导致每个法线的长度为 1)或者您可以将归一化标志设置为:glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_TRUE...