Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OpenGL ES 1.0/2.0中的正交/投影_Android_Opengl Es 2.0 - Fatal编程技术网

Android OpenGL ES 1.0/2.0中的正交/投影

Android OpenGL ES 1.0/2.0中的正交/投影,android,opengl-es-2.0,Android,Opengl Es 2.0,之前我做了一个关于OpenGLES1.0的教程。供参考,可在此处找到: (不过是德语) 我现在的目标是将游戏移植到OpenGLES2.0。到目前为止,我能够加载网格和纹理并渲染它们 现在我想有一个简单的矩形作为背景,上面有纹理。这应该在正交透视中渲染。然后我切换到投影透视图并绘制一个简单的方框。当我现在调用setLookAtM(…)时,我得到一个空白的sceen。代码如下: public void onDrawFrame(GL10 gl) { long currentFrameStart

之前我做了一个关于OpenGLES1.0的教程。供参考,可在此处找到: (不过是德语)

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

现在我想有一个简单的矩形作为背景,上面有纹理。这应该在正交透视中渲染。然后我切换到投影透视图并绘制一个简单的方框。当我现在调用setLookAtM(…)时,我得到一个空白的sceen。代码如下:

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();
}
此处为牵引杆接地方法:

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");
 }

我不确定我在正交和投影透视方面遗漏了什么。非常感谢您的帮助。

您正在将投影矩阵作为绘图块的一部分进行擦拭。如果你想用已经计算过的透视投影来绘制它,我认为你不想这样做

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

blockMesh.vertexBuffer.position(0);
...
。。。

矩阵setIdentityM(mProjectionMatrix,0);谢谢。有一段时间我对矩阵有点困惑。