簡體   English   中英

iphone openGL ES 2翻譯對象

[英]iphone openGL ES 2 translating object

希望通過矩陣平移在場景中移動對象,但無法粘貼任何東西。 我可以移動投影,但我不能移動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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM