我正在尝试在 OpenGL ES 2.0 iOS 中绘制 3D 立方体。我制作了一个结构,试图通过它来容纳与立方体相关的各种信息(位置坐标、纹理映射、法线)。现在我需要告诉 OpenGL 在哪里可以找到这个结构中的法线。一切都正确编译和呈现,但我在下面显示的最后一行给了我这个警告:
将“unsigned long”传递给类型为“const GLvoid *”(又名“const void *”)的参数的指针转换不兼容
为什么我的代码返回一个 unsigned long?我不知道如何修改它以获得该行要求的指针。谁能看到我做错了什么?谢谢。
结构如下:
typedef struct {
GLKVector3 positionCoordinates;
GLKVector2 textureCoordinates;
GLKVector3 normalCoordinates;
} VertexData;
坐标数据:
VertexData cameraVertices[] = {
//{ position x, position y, position z}, {texture}, {normalX, normalY, normalZ}
{ { 0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, { 1.0f, 0.0f, 0.0f} }, // rightward facing (+X)
{ { 0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f}, { 0.0f, 1.0f, 0.0f} }, // upward facing (+Y)
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 1.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 1.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 1.0f, 0.0f} },
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 1.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f}, { 0.0f, 1.0f, 0.0f} },
{ {-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} }, // leftward facing (-X)
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, { 0.0f, -1.0f, 0.0f} }, // downward facing (-Y)
{ { 0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, -1.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, -1.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, -1.0f, 0.0f} },
{ { 0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, -1.0f, 0.0f} },
{ { 0.5f, -0.5f, 0.5f}, {1.0f, 1.0f}, { 0.0f, -1.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} }, // forward facing (+Z)
{ {-0.5f, 0.5f, 0.5f}, {1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 0.0f, 1.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 0.0f, 1.0f} },
{ {-0.5f, 0.5f, 0.5f}, {1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} },
{ {-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f} },
{ { 0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f} }, // rear facing (-Z)
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f} },
{ { 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f} },
{ { 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f} },
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f} },
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f}, { 0.0f, 0.0f, -1.0f} },
};
以及相关的代码行(基本上在 viewDidLoad 中):
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), offsetof(VertexData, positionCoordinates));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), offsetof(VertexData, normalCoordinates));
最佳答案
嗯,这里的根本问题是 offsetof
宏不返回指针!它以 size_t
的形式返回偏移量。由于 size_t
的大小和 GLvoid *
的大小可能不同,所以不要这样使用 offsetof(...)
的结果。
你应该做的是:(GLubyte *)0 + offsetof (...)
。
关于ios - 在结构中传递法线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692233/