0

以前、OpenGL-ES 1.0 のチュートリアルを行いました。参考までに、これはここにあります: SpaceInvaders (ドイツ語ですが)

私の目標は、ゲームを OpenGL-ES 2.0 に移植することです。これまでのところ、メッシュとテクスチャを読み込んでレンダリングすることができました。

ここで、背景として単純な長方形をテクスチャ付きにしたいと考えています。これは、オルソ パースペクティブでレンダリングする必要があります。次に、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 に答える