以前我做了一个关于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");
}
我不确定我对正射和投影视角的缺失。任何帮助表示赞赏。
答案 0 :(得分:0)
您正在将投影矩阵作为drawBlocks的一部分进行擦除。如果您想使用已计算的透视投影绘制它,我认为您不想这样做。
...
Matrix.setIdentityM(mProjectionMatrix, 0); <-----
Matrix.setIdentityM(mModelMatrix, 0);
blockMesh.vertexBuffer.position(0);
...