iPhone openGL ES 2 翻译对象



希望通过矩阵翻译在场景中移动对象,但无法粘住任何东西。我可以让投影移动,但我不能让 2d 正方形移动。

在精灵类中渲染代码:

glGenBuffers(1, &self->_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(self.shader.positionSlot);
glVertexAttribPointer(self.shader.positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(self.shader.colorSlot);
glVertexAttribPointer(self.shader.colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)(sizeof(float) * 3));
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
float matrix[16]; // trying to manipulate matrix here 
matrixSetIdentity(matrix);
float translateMatrix[16];
matrixSetTranslation(translateMatrix, 0, 0, 0);
float result[16];
matrixMultiply(matrix, translateMatrix, result);
glUniformMatrix4fv(self.shader.positionSlot, 1, 0, result); //this doesn't work, the object disappears?
glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertices)/sizeof(Vertices[0]));
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

下面是着色器代码:

attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;

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

现在令人沮丧的部分是这有效:

float h = 4.0f * self.frame.size.height / self.frame.size.width;
float projection[16];
[self.matrix projection:projection Left:-2 Right:2 Top:h/2 Bottom:-h/2 near:4 far:20];
glUniformMatrix4fv(self.shader.projectionUniform, 1, 0, projection);
float modelView[16];
matrixSetTranslation(modelView, 0.0, sin(CACurrentMediaTime()), -10.0);
glUniformMatrix4fv(self.shader.modelViewUniform, 1, 0, modelView);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);

不知道为什么顶部代码似乎不起作用,但底部代码却有效。任何意见将不胜感激。

投影

矩阵正在由一个值转换,该值会改变每一帧(CACurrentMediaTime())。模型的转换矩阵正在按相同的值进行转换。

该值是 0 x、0 y 和 0 z 单位的平移(假设 matrixSetTranslation 的行为类似于 GLKMatrixMakeTranslation (https://developer.apple.com/library/ios/documentation/GLkit/Reference/GLKMatrix4/Reference/reference.html#//apple_ref/c/func/GLKMatrix4MakeTranslation)),即单位矩阵,再次乘以单位矩阵。这为您提供了单位矩阵,相当于将常规数字乘以 1。