在结构中传递法线

时间:2013-10-30 19:31:54

标签: ios struct opengl-es-2.0

我正在尝试在OpenGL ES 2.0 iOS中绘制3D立方体。我做了一个结构,通过它我试图容纳与立方体相关的各种信息(位置坐标,纹理映射,法线)。现在我需要告诉OpenGL在这个结构中找到法线的位置。一切都正确编译和渲染,但我在下面显示的最后一行给了我这个警告:

Incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'const GLvoid *' (aka '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));

2 个答案:

答案 0 :(得分:1)

嗯,这里的根本问题是offsetof宏没有返回指针!它将偏移量返回为size_t。由于size_t的大小和GLvoid *的大小可能不同,因此请勿以此方式使用offsetof (...)的结果。

你应该做的是:(GLubyte *)0 + offsetof (...)

答案 1 :(得分:0)

实际上,我找到了一种更简单的方法。我需要做的就是将其转化为空白*。

glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void *)offsetof(VertexData, normalCoordinates));
相关问题