在GLKit中分配属性

时间:2016-03-07 09:25:52

标签: ios opengl-es attributes glkit

过去几天我一直在使用OpenGL ES 2.0和GLKit进行IOS开发,我似乎抓住了大部分概念,但我似乎无法为着色器分配新的属性。我在顶点着色器中添加了一个名为NormalStuff的新vec3属性,但是当我尝试运行以下代码时,它会在第三行崩溃。

    self.positionSlot = GLuint(glGetAttribLocation(programHandle, "Position"))
    self.colorSlot =    GLuint(glGetAttribLocation(programHandle, "SourceColor"))
    self.normalSlot =   GLuint(glGetAttribLocation(programHandle, "NormalStuff"))
    glEnableVertexAttribArray(self.positionSlot)
    glEnableVertexAttribArray(self.colorSlot)
    glEnableVertexAttribArray(self.normalSlot)

这是我的顶点着色器:

attribute vec4 Position;
attribute vec4 SourceColor;
attribute vec3 NormalStuff;

varying vec4 DestinationColor;

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

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

任何人都可以解释为什么这不起作用或如何修复它?

另外,我认为这不重要,但这里有一些我的代码:

  let positionSlotFirstComponent = UnsafePointer<Int>(bitPattern: 0)
  glEnableVertexAttribArray(positionSlot)
  glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), positionSlotFirstComponent)

  glEnableVertexAttribArray(colorSlot)
  let colorSlotFirstComponent = UnsafePointer<Int>(bitPattern: sizeof(CFloat) * 3)
  glVertexAttribPointer(colorSlot, 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), colorSlotFirstComponent)


  glEnableVertexAttribArray(normalSlot)
  let normalSlotFirstComponent = UnsafePointer<Int>(bitPattern: sizeof(CFloat) * 7)
  glVertexAttribPointer(normalSlot, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), normalSlotFirstComponent)

0 个答案:

没有答案