android - 正确的顶点着色器代码? OpenGL ES 2.0

标签 android rotation opengl-es-2.0 translate-animation skew

编辑 代码添加,请看下面

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

编辑 3 - 添加了新代码

我有 2 个类,一个渲染类和一个自定义“四边形”类。

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

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

在我的 onSurfaceChanged 方法中我有:

@覆盖 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。

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

enter image description here

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

enter image description here

但是,这是旋转了 +45 度并切换了 2 个着色器变量的同一个四边形(因此它们显示为“gl_Position = uMVPMatrix * a_position;”- 如您所见,它不太正确。

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

enter image description here

感谢任何帮助。

最佳答案

这真的很难说,因为我们不知道你传递给这两个变量的是什么。

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

如果这不在您的着色器中,而是在您的程序代码中,则说明信息不足。

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

关于android - 正确的顶点着色器代码? OpenGL ES 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836688/

相关文章:

java - Libgdx:不支持的格式组合

opengl - 片段着色器并为纹理着色

android - isEnabled() 与适配器 Android

android - 阻止不可避免的回声的最有效方法

iphone - iOS 方向更改不起作用也未收到 UIDeviceOrientationDidChangeNotification

c++ - 获取网格中心的屏幕位置

android - OpenGL ES 2 投影切换前景和背景

java - Android 保存相机图片到本地存储

android - 在android中绘制 ImageView

javascript - 旋转图标并同时切换内容 onclick