[英]iphone openGL ES 2 translating object
looking to move objects in scene with matrix translation, but can't get anything to stick. 希望通过矩阵平移在场景中移动对象,但无法粘贴任何东西。 I can get the projection to move but i cant get the 2d square to move. 我可以移动投影,但我不能移动2D正方形。
Render code in sprite class: 精灵类中的渲染代码:
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);
heres the shader code: 继承人着色器代码:
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;
}
Now the frustrating part is that this works: 现在令人沮丧的部分是,它的工作原理是:
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);
not sure why the top code doesn't seem to work but the bottom does. 不知道为什么顶部代码似乎不起作用,而底部代码为什么起作用。 Any comments are greatly appreciated. 任何意见,不胜感激。
The projection matrix is being translated by a value which changes every frame (CACurrentMediaTime()). 投影矩阵正在通过每帧更改的值(CACurrentMediaTime())进行转换。 The model's transformation matrix is being translated by the same value. 该模型的转换矩阵正在转换相同的值。
That value is a translation of 0 x,0 y, and 0 z units (assuming that matrixSetTranslation behaves like GLKMatrixMakeTranslation ( https://developer.apple.com/library/ios/documentation/GLkit/Reference/GLKMatrix4/Reference/reference.html#//apple_ref/c/func/GLKMatrix4MakeTranslation )), which is the identity matrix, multiplied by the identity matrix again. 该值是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 )),它是单位矩阵,再乘以单位矩阵。 This give you the identity matrix, which is equivalent to multiplying a regular number by one. 这为您提供了单位矩阵,等效于将常规数字乘以1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.