gpt4 book ai didi

swift - GLKit 是否将我限制为两个属性?

转载 作者:行者123 更新时间:2023-11-30 13:37:38 25 4
gpt4 key购买 nike

过去几天我一直在使用一些具有颜色属性和位置属性的 GLKit 代码,但是当我尝试添加普通属性时,它每次都会崩溃。

顶点着色器:

 attribute vec4 SourceColor;
attribute vec4 aVertexPosition;
attribute vec4 aVertexNormal;

varying vec4 DestinationColor;


uniform mat4 uPMatrix; /* perspectiveView matrix */
uniform mat4 uVMatrix; /* view matrix */
uniform mat4 uOMatrix; /* object matrix */


uniform mat4 Projection;
uniform mat4 ModelView;
uniform float u_time;

void main(void) {
DestinationColor = SourceColor;
gl_Position = aVertexPosition * Projection;
}

代码:

    self.colorSlot =    glGetAttribLocation(programHandle, "SourceColor")
self.positionSlot = glGetAttribLocation(programHandle, "aVertexPosition")
self.normalSlot = glGetAttribLocation(programHandle, "aVertexNormal")
glEnableVertexAttribArray(GLuint(self.positionSlot))
glEnableVertexAttribArray(GLuint(self.colorSlot))
glEnableVertexAttribArray(GLuint(self.normalSlot))<-crashes here

最佳答案

通过评论发现,这次崩溃的原因是值 self.normalSlot-1,当 glGetAttribLocation 失败时返回该值查找具有指定名称的属性。然后使用 GLuint(self.normalSlot) 对值 -1 进行类型转换,这很可能会产生一个非常大的值,在启用顶点属性数组时不支持该值,从而导致崩溃。因此,在使用属性和制服的位置之前,您应该检查是否检索到该位置。有效位置为正值,因此应检查 location >= 0

该属性仍然存在于着色器源中,但未检索到位置。原因似乎是着色器中从未使用过该属性,因此它可能是编译器优化。在任何情况下,您都不能通过简单地在顶点着色器中声明该属性来强制该属性,您还需要使用它。似乎还有另一种方法可以强制它,那就是使用glBindAttribLocation。我不希望这能保证该属性的存在,因此您至少应该在使用它后检查 GL 错误以避免出现其他问题。

注意:如果您使用的是 glBindAttribLocation,请确保您完全理解其文档。很容易忘记这些索引,您应该有一个智能系统或个人标准来确定如何对属性进行索引。

关于swift - GLKit 是否将我限制为两个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com