我知道以下問題已經在不同的論壇上被多次提出過,但是在查看了不同的解決方案之後,它仍然沒有解決我的問題,或者我沒有得到他們的意思
I我試圖在iPhone上使用OpenGL ES 2.0對金字塔建模。
我想允許用戶使用觸摸屏旋轉此金字塔,以便他可以查看形狀的每個面。
我使用我自己的矩陣工具,靈感來自OpenGL ES 2.0 Programming Guide書中給出的庫。
我有一個簡單的頂點着色器,需要2個制服和2個屬性。
這裏是我的頂點着色器:OpenGL ES 2.0:對象旋轉問題
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec4 vertex;
attribute vec4 color;
varying vec4 colorVarying;
void main()
{
colorVarying = color;
gl_Position = projectionMatrix * modelViewMatrix * vertex;
}
所以,我的渲染過程中,我創建2個矩陣:模型視圖和投影矩陣。
這裏是我的投影矩陣如何初始化:
[pMatrix frustumWithAngleOfView:70.0f
ratio:(float)backingWidth/(float)backingHeight
zNear:1.0f
zFar:100.0f];
[program use];
glUniformMatrix4fv(unif_projectionMatrix, 1, GL_FALSE, pMatrix.vertices);
[program unuse];
我的金字塔的頂點設置如下:
GLfloat pyramid[] = {
//Base
-1.0, -1.0, 1.0, 1.0,
-1.0, -1.0, -1.0, 1.0,
1.0, -1.0, -1.0, 1.0,
1.0, -1.0, 1.0, 1.0,
//Front face
0.0, 1.0, 0.0, 1.0,
-1.0, -1.0, 1.0, 1.0,
1.0, -1.0, 1.0, 1.0,
//Right face
0.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0,
1.0, -1.0, -1.0, 1.0,
//Back face
0.0, 1.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0,
-1.0, -1.0, -1.0, 1.0,
//Left face
0.0, 1.0, 0.0, 1.0,
-1.0, -1.0, -1.0, 1.0,
-1.0, -1.0, 1.0, 1.0
};
最後,這裏是我的繪製代碼:
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use shader program
[program use];
// Apply transformations on the ModelView matrix
[mvMatrix identity];
[mvMatrix translateX:0 Y:0 Z:-6.0];
[mvMatrix rotateX:rotatex Y:rotatey Z:rotatez];
// Set the ModelView matrix uniform in the v-shader
glUniformMatrix4fv(unif_modelViewMatrix, 1, GL_FALSE, mvMatrix.vertices);
// Set the vertex attribute in the v-shader
glVertexAttribPointer(attr_vertex, 4, GL_FLOAT, GL_FALSE, 0, pyramid);
glEnableVertexAttribArray(attr_vertex);
// Draw
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDrawArrays(GL_TRIANGLES, 4, 3);
glDrawArrays(GL_TRIANGLES, 7, 3);
glDrawArrays(GL_TRIANGLES, 10, 3);
glDrawArrays(GL_TRIANGLES, 13, 3);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
一切似乎都很好,我的金字塔在這裏,在Z軸上向後翻譯。
當我開始旋轉形狀時,問題就出現了,它看起來像是在用戶周圍旋轉,而不是在自身周圍旋轉。
例如,當我從左到右地觸摸屏幕時,我看到金字塔從屏幕右邊緣出來,然後從左邊緣返回屏幕,就好像它已經圍繞着一個完整的圓圈我:第。
我非常確定我的矩陣管理代碼,所以我決定不要在這裏發佈它以避免在這個問題中存在太多的代碼。如果有必要,我可以給它。
你知道我做錯了什麼嗎?
非常感謝!
湯姆
確實!問題恰恰在於我的矩陣乘法代碼,它給了我相同的結果,而不依賴於操作的順序。 – 2010-06-04 08:07:44