菜鸟教程小白 发表于 2022-12-12 16:07:01

ios - 在结构中传递法线


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

<p><code>不兼容的整数到指针转换将“unsigned long”传递给“const GLvoid *”类型的参数(又名“const void *”)</code></p>

<p>为什么我的代码返回一个无符号长整数?我不知道如何修改它以获取该行所要求的指针。谁能看到我做错了什么?谢谢。</p>

<p>这是结构:</p>

<pre><code>typedef struct {
    GLKVector3 positionCoordinates;
    GLKVector2 textureCoordinates;
    GLKVector3 normalCoordinates;
} VertexData;
</code></pre>

<p>坐标数据:</p>

<pre><code>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} },
};
</code></pre>

<p>以及相关的代码行(基本上在viewDidLoad中):</p>

<pre><code>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));
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>嗯,这里的根本问题是 <code>offsetof</code> 宏不返回指针!它以 <code>size_t</code> 形式返回偏移量。由于 <code>size_t</code> 的大小和 <code>GLvoid *</code> 的大小可能不同,所以不要这样使用 <code>offsetof (...)</code> 的结果。 </p>

<p>你应该做的是:<code>(GLubyte *)0 + offsetof (...)</code>.</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在结构中传递法线,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/19692233/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/19692233/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在结构中传递法线