1

这有什么明显的错误吗?我的正方形渲染正常,但是当我尝试向左/右/上/下/移动 9it 时,它的边缘会伸展到无限远,就好像使用了一些超高 FOV 一样……这不是……

private float[] mMVPMatrix = new float[16];
private float[] mProjMatrix = new float[16];
private float[] mVMatrix = new float[16];

@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) 
{
    // Set the OpenGL viewport to the same size as the surface.
    GLES20.glViewport(0, 0, width, height);

      float ratio = (float) width / height;

      float PI = 3.1428f;
      float near = 1.0f;
      float far  = 100.0f;
      float fov = 45; // degrees, try also 45, or different number if you like
      float top = (float)(Math.tan(fov * PI / 360.0f) * near);
      float bottom = -top;
      float left = ratio * bottom;
      float right = ratio * top;

      Matrix.frustumM(mProjMatrix, 0, left*0.1f, right*0.1f, top*0.1f, bottom*0.1f, near, far);
      //Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 0.5f, 4);

      Matrix.setLookAtM(mVMatrix, 0,
              0f, 0f,-150f,
              0f, 0f, 0f,
              0f, 1.0f, 0.0f);
}   

@Override
public void onDrawFrame(GL10 glUnused) 
{
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);                    

    // Do a complete rotation every 10 seconds.
    long time = SystemClock.uptimeMillis() % 10000L;
    float angleInDegrees = (360.0f / 10000.0f) * ((int) time);

    if(m_nPoints.size()>0)
    {
        m_fX = m_nPoints.get(0).x;
        m_fY = m_nPoints.get(0).y;
        m_nPoints.remove(0);
    }

    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
    Matrix.translateM(mModelMatrix,0,m_fX,m_fY, -12.0f);

    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix,0,m_fX,m_fY, 0.0f);

    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mModelMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

    m_nSquare.draw(mMVPMatrix);//mModelMatrix);
}   



public void draw(float[] mvpMatrix) {

// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");    // get handle to vertex shader's vPosition member
GLES20.glEnableVertexAttribArray(mPositionHandle);    //Enable a handle to the triangle vertices
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,GLES20.GL_FLOAT, false,vertexStride, vertexBuffer);    // Prepare the triangle coordinate data

textureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");
GLES20.glVertexAttribPointer(textureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
GLES20.glEnableVertexAttribArray(textureCoordinateHandle);

textureUniformHandle = GLES20.glGetUniformLocation(mProgram, "u_Texture");
OpenGLESRenderer.checkGlError("glGetUniformLocation");
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle);
GLES20.glUniform1i(textureUniformHandle, 0);      

mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");//get handle to shape's transformation matrix
OpenGLESRenderer.checkGlError("glGetUniformLocation");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);//Apply the projection and view transformation
OpenGLESRenderer.checkGlError("glUniformMatrix4fv");
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,GLES20.GL_UNSIGNED_SHORT, drawListBuffer);//Draw the square
GLES20.glDisableVertexAttribArray(mPositionHandle);// Disable vertex array
}



private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +

"attribute vec4 vPosition;" +
"attribute vec2 a_TexCoordinate;" +

"varying vec2 v_TexCoordinate;" +

"void main() {" +
// the matrix must be included as a modifier of gl_Position
"  gl_Position = vPosition * uMVPMatrix;" +
"  v_TexCoordinate = a_TexCoordinate;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +

"uniform sampler2D u_Texture;" +

"varying vec2 v_TexCoordinate;" +

"void main() {" +
"  gl_FragColor = texture2D(u_Texture, v_TexCoordinate);" +
"}";
4

0 回答 0