Correct vertex shader code? OpenGL ES 2.0

Edit Added code, see below

Change 2 . Screenshots from the device below, along with an explanation

Edit 3 - added new code

I have 2 classes, rendering and custom class "quad".

I declared this at the class level in the renderer class:

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

And in my onSurfaceChanged method, I have:

@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);

}

and....

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

}

And finally, from this class, 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

}

Then in my quad class:

This is declared at the class level:

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

Then a texture adjustment method (don't think this applies to this problem!)

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

    }

And finally, my rotateQuad method (which currently needs to draw and rotate the quad).

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 transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");
   // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    // get handle to shape 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);
}

for Edit 2.

, . .

enter image description here

, +45 "gl_Position = a_position * uMVPMatrix;" + ( , - a_position, vPosition), !

enter image description here

, , +45 , ( "gl_Position = uMVPMatrix * a_position;" - , .

, , , ....

enter image description here

.

+1
3

, , .

OpenGL - , , vPosition , uMVPMatrix , , .

, , .

, , , , .

+1

MVP,

gl_Position = uMVPMatrix * vPosition;

, ...

0

.

( ). , .

:

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

, , .

, , mvpMatrix2, . .

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

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

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

After applying this, there is no distortion (and also regarding my other question here Using the matrix. Turn in OpenGL ES 2.0 I can translate the center of the ATV). I say “for the most part” because when I rotate it, it rotates backward (so if I say “rotate” +45 degrees (clockwise), it actually rotates the quad by -45 degrees (Anita-po clockwise).

But hopefully this will help anyone who has a similar problem in the future.

0
source

All Articles