0

以前我做过一个关于 OpenGL-ES 1.0 的教程。作为参考,可以在这里找到: SpaceInvaders(虽然它是德语)

我现在的目标是将游戏移植到 OpenGL-ES 2.0。到目前为止,我能够加载网格和纹理并渲染它们。

现在我想有一个简单的矩形作为我的背景,上面有纹理。这应该在 Ortho-Perspective 中呈现。然后我更改为 Projection-Perspective 并绘制一个简单的框。当我现在调用setLookAtM(...)时,我得到一个空白画面。这是代码:

public void onDrawFrame(GL10 gl) {
   long currentFrameStart = System.nanoTime();
   deltaTime = (currentFrameStart - lastFrameStart) / 1000000000.0f;
   lastFrameStart = currentFrameStart;

   GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
   GLES20.glUseProgram(programHandle);
   checkGlError("glUseProgram");

   mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVPMatrix");
   mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "uMVMatrix");
   mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "uLightPos");
   mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "uTexture");

   mPositionHandle = GLES20.glGetAttribLocation(programHandle, "aPosition");
   mColorHandle = GLES20.glGetAttribLocation(programHandle, "aColor");
   mNormalHandle = GLES20.glGetAttribLocation(programHandle, "aNormal");
   mTextureCoordinateHandle = GLES20.glGetAttribLocation
      (programHandle,"aTexCoordinate");

   Matrix.setIdentityM(mLightModelMatrix, 0);
   Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0,    
      mLightPosInModelSpace, 0);
   Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);

   Matrix.orthoM(mProjectionMatrix, 0, -width / 2, width / 2, -height / 2, height / 2, 
      0, 100);
   drawBackground();

   GLES20.glEnable(GLES20.GL_CULL_FACE);

   Matrix.setIdentityM(mProjectionMatrix, 0);
   final float ratio = (float) width / height;
   final float left = -ratio;
   final float right = ratio;
   final float bottom = -1.0f;
   final float top = 1.0f;
   final float near = 1.0f;
   final float far = 100.0f;
   Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);

   Matrix.setLookAtM(mViewMatrix, 0, 0, 6, 2, 0, 0, -4, 0, 1, 0);

   drawBlock();
}

这里的drawBackground方法:

private void drawBackground() {
   GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
   GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, backgroundTextureHandle);
   GLES20.glUniform1i(mTextureUniformHandle, 0);

   Matrix.setIdentityM(mModelMatrix, 0);
   Matrix.setIdentityM(mProjectionMatrix, 0);

   mBackgroundPositions.position(0);
   GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, 
      false, 0, mBackgroundPositions);
   GLES20.glEnableVertexAttribArray(mPositionHandle);

   mBackgroundColors.position(0);
   GLES20.glVertexAttribPointer(mColorHandle, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false, 
      0, mBackgroundColors);
   GLES20.glEnableVertexAttribArray(mColorHandle);

   mBackgroundNormals.position(0);
   GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT, 
      false, 0, mBackgroundNormals);
   GLES20.glEnableVertexAttribArray(mNormalHandle);

   mBackgroundTextureCoordinates.position(0);
   GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORD_DATA_SIZE,   
      GLES20.GL_FLOAT, false, 0, mBackgroundTextureCoordinates);
   GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

   // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
   // (which currently contains model * view).
   Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

   // Pass in the modelview matrix.
   GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);

   // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
   // (which now contains model * view * projection).
   Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

   // Pass in the combined matrix.
   GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

   // Pass in the light position in eye space.
   GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], 
      mLightPosInEyeSpace[2]);

   ShortBuffer buf = 
      ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asShortBuffer();
   buf.put(new short[] {0, 1, 2, 0, 2, 3});
   buf.position(0);

   // Draw the rectangle.
   GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, buf);
   checkGlError("glDrawArrays");
 }

最后是 drawBlock 方法:

private void drawBlocks() {
   GLES20.glUseProgram(colorProgramHandle);
   checkGlError("glUseProgram");

   mMVPMatrixHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uMVPMatrix");
   mMVMatrixHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uMVMatrix");
   mLightPosHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uLightPos");
   mTextureUniformHandle = GLES20.glGetUniformLocation(colorProgramHandle, "uTexture");

   mPositionHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aPosition");
   mColorHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aColor");
   mNormalHandle = GLES20.glGetAttribLocation(colorProgramHandle, "aNormal");
   mTextureCoordinateHandle = GLES20.glGetAttribLocation(colorProgramHandle, 
       "aTexCoordinate");

   Matrix.setIdentityM(mProjectionMatrix, 0);
   Matrix.setIdentityM(mModelMatrix, 0);

   blockMesh.vertexBuffer.position(0);
   GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, 
      false, 0, blockMesh.vertexBuffer);
   GLES20.glEnableVertexAttribArray(mPositionHandle);

   blockMesh.colorBuffer.position(0);
   GLES20.glVertexAttribPointer(mColorHandle, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false, 
      0, blockMesh.colorBuffer);
   GLES20.glEnableVertexAttribArray(mColorHandle);

   blockMesh.normalBuffer.position(0);
   GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT, 
      false, 0, blockMesh.normalBuffer);
   GLES20.glEnableVertexAttribArray(mNormalHandle);

   // This multiplies the view matrix by the model matrix, and stores the result in the 
   MVP matrix
   // (which currently contains model * view).
   Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

   // Pass in the modelview matrix.
   GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);

   // This multiplies the modelview matrix by the projection matrix, and stores the 
   result in the MVP matrix
   // (which now contains model * view * projection).
   Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

   // Pass in the combined matrix.
   GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

   // Pass in the light position in eye space.
   GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], 
      mLightPosInEyeSpace[2]);

   // Draw the cube.
   GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, 
      blockMesh.indexBuffer);
   checkGlError("glDrawElements");
 }

我不确定我在正交和投影透视方面缺少什么。任何帮助表示赞赏。

4

1 回答 1

0

您正在擦除投影矩阵作为 drawBlocks 的一部分。如果您想使用已经计算的透视投影来绘制它,我认为您不想这样做。

...
Matrix.setIdentityM(mProjectionMatrix, 0);   <-----
Matrix.setIdentityM(mModelMatrix, 0);

blockMesh.vertexBuffer.position(0);
...
于 2012-06-17T08:19:12.327 回答