ios - 在结构中传递法线

标签 ios struct opengl-es-2.0

我正在尝试在 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/

相关文章:

objective-c - NSBundle 文件引用在应用程序重新安装后丢失

ios - 当应用程序进入前台时如何重新加载 UITableView 数据? (Xcode 8.3/swift 3)

c - 将 qsort 与定义结构一起使用

c++ - 数组与结构 C++

iphone - OpenGL 不显示 iPhone 上辅助线程的结果

opengl-es - 写入 gl_FragColor 会导致 Android 上的 INVALID_OPERATION

ios - merge 两个 git 仓库(不小心从 github 下载的 .zip,需要重新 merge 进去)

ios - 为什么自定义类不能成功调用自己的类方法? iOS

c - 通过结构别名数组

iOS 5.0 GLKit GLKTextureLoader- glBindTexture 发生在哪里?