1

添加了编辑代码,请参见下文

编辑 2 - 底部包含设备的屏幕截图以及说明

编辑 3 - 添加了新代码

我有 2 个类,一个渲染的和一个自定义的“quad”类。

我在渲染器类的类级别声明了这些:

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

在我的 onSurfaceChanged 方法中,我有:

@Override public void onSurfaceChanged(GL10 gl, int width, int height) {

    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

}

和....

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub

    myBitmap = BitmapFactory.decodeResource(curView.getResources(), R.drawable.box);

        //Create new Dot objects

        dot1 = new Quad();
        dot1.setTexture(curView, myBitmap);
        dot1.setSize(300,187);    //These numbers are the size but are redundant/not used at the moment.

        myBitmap.recycle();


           //Set colour to black
           GLES20.glClearColor(0, 0, 0, 1);

}

最后来自这个类,onDrawFrame:

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub

    //Paint the screen the colour defined in onSurfaceCreated
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

     // Set the camera position (View matrix) so looking from the front
   Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Combine
   Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    dot1.rotateQuad(0,0,45, mMVPMatrix); //x,y,angle and matrix passed in

}

然后,在我的四人班:

这在类级别声明:

        private float[] mRotationMatrix = new float[16];        
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjMatrix = new float[16];
    private final float[] mVMatrix = new float[16];
    private int mMVPMatrixHandle;
    private int mPositionHandle;
    private int mRotationHandle;
//Create our vertex shader
    String strVShader =  
              "uniform mat4 uMVPMatrix;" +
              "uniform mat4 uRotate;" +
              "attribute vec4 a_position;\n"+
              "attribute vec2 a_texCoords;" +
              "varying vec2 v_texCoords;" +
              "void main()\n" +
              "{\n" +
         //     "gl_Position = a_position * uRotate;\n"+
         //          "gl_Position = uRotate * a_position;\n"+
               "gl_Position = a_position * uMVPMatrix;\n"+  
        //        "gl_Position = uMVPMatrix * a_position;\n"+  
                  "v_texCoords = a_texCoords;" +
              "}";

    //Fragment shader

    String strFShader =
        "precision mediump float;" +
        "varying vec2 v_texCoords;" +
        "uniform sampler2D u_baseMap;" +
        "void main()" +
        "{" +
        "gl_FragColor = texture2D(u_baseMap, v_texCoords);" +
        "}";

然后设置纹理的方法(但不要认为这与这个问题有关!!)

    public void setTexture(GLSurfaceView view, Bitmap imgTexture){
        this.imgTexture=imgTexture;

        iProgId = Utils.LoadProgram(strVShader, strFShader);
        iBaseMap = GLES20.glGetUniformLocation(iProgId, "u_baseMap");
                iPosition = GLES20.glGetAttribLocation(iProgId, "a_position");
        iTexCoords = GLES20.glGetAttribLocation(iProgId, "a_texCoords");
        texID = Utils.LoadTexture(view, imgTexture);

    }

最后,我的“rotateQuad”方法(目前应该绘制和旋转四边形)。

public void rotateQuad(float x, float y, int angle, float[] mvpMatrix){

Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, 0.1f);

//  Matrix.translateM(mRotationMatrix, 0, 0, 0, 0);  //Removed temporarily

// Combine the rotation matrix with the projection and camera view
   Matrix.multiplyMM(mvpMatrix, 0, mRotationMatrix, 0, mvpMatrix, 0);

float[] vertices = {

        -.5f,.5f,0, 0,0,
        .5f,.5f,0, 1,0,
        -.5f,-.5f,0, 0,1,
        .5f,-.5f,0, 1,1

        };

 vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
 vertexBuf.put(vertices).position(0);

//Bind the correct texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);      
//Use program
GLES20.glUseProgram(iProgId);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");
   // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    // get handle to shape's rotation matrix
mRotationHandle = GLES20.glGetUniformLocation(iProgId, "uRotate");
  // Apply the projection and view transformation
  GLES20.glUniformMatrix4fv(mRotationHandle, 1, false, mRotationMatrix, 0);

//Set starting position for vertices
vertexBuf.position(0);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for position
GLES20.glEnableVertexAttribArray(iPosition);
//Set starting position for texture
vertexBuf.position(3);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for texture
GLES20.glEnableVertexAttribArray(iTexCoords);
//Draw it
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}

对于编辑 2。

这是我在屏幕中央绘制的四边形。没有旋转。

在此处输入图像描述

这是以 +45 度旋转的同一个四边形,代码为“gl_Position = a_position * uMVPMatrix;” + 在我的顶点着色器中(它现在来自不同的项目,所以着色器变量是 a_position 而不是 vPosition),它看起来是正确的!

在此处输入图像描述

但是,这是在 2 个着色器变量切换的情况下以 +45 度旋转的同一个四边形(因此它们读取“gl_Position = uMVPMatrix * a_position;” - 如您所见,它不太正确。

也只是一个旁注,你在这里看不到它,因为四边形是对称的,但每种方法也会以与另一种相反的方向旋转......

在此处输入图像描述

任何帮助表示赞赏。

4

3 回答 3

1

真的无法分辨,因为我们不知道您传递给这两个变量的内容。

OpenGL是列优先格式,所以如果vPosition实际上是一个向量,并且uMVPMatrix是一个矩阵,那么第一个选项是正确的,如果这是在你的着色器中。

如果这不是在您的着色器中而是在您的程序代码中,那么没有足够的信息。

如果您使用第一个选项但得到意外结果,您可能没有正确计算矩阵或没有传递正确的顶点。

于 2013-04-05T15:12:01.923 回答
0

通常在顶点着色器中,你应该通过 MVP 来倍增位置,即

gl_Position = uMVPMatrix *vPosition;

当您更改订单时,这应该可以...

于 2013-04-05T16:33:25.233 回答
0

感谢大家的帮助。

我设法找到了问题(大部分)。我会展示我所做的。

这是以下行:

Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, mRotationMatrix, 0);

如您所见,我将矩阵相乘并将它们存储回我在乘法中使用的矩阵。

所以我创建了一个名为 mvpMatrix2 的新矩阵并将结果存储在其中。然后将其传递给我的顶点着色器。

//Multiply matrices
Matrix.multiplyMM(mvpMatrix2, 0, mvpMatrix, 0, mRotationMatrix, 0);

//get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");

//Give to vertex shader variable
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix2, 0);

应用此之后,没有失真(而且,关于我在此处使用矩阵的另一个问题。在 OpenGL ES 2.0 中旋转我能够平移四边形的中心)。我说“大部分”是因为当我旋转它时,它会向后旋转(所以如果我说旋转 +45 度(顺时针),它实际上会将四边形旋转 -45 度(逆时针)。

但希望这将帮助将来遇到类似问题的任何人。

于 2013-04-06T16:16:37.887 回答