Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect vertex shader code? OpenGL ES 2.0
    primarykey
    data
    text
    <p><strong>Edit</strong> Code added, please see below</p> <p><strong>Edit 2</strong> - Screenshots from device included at bottom along with explanation</p> <p><strong>Edit 3</strong> - New code added</p> <p>I have 2 classes, a rendered and a custom 'quad' class.</p> <p>I have these declared at class level in my renderer class:</p> <pre><code>final float[] mMVPMatrix = new float[16]; final float[] mProjMatrix = new float[16]; final float[] mVMatrix = new float[16]; </code></pre> <p>And in my onSurfaceChanged method I have:</p> <p>@Override public void onSurfaceChanged(GL10 gl, int width, int height) {</p> <pre><code> GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); } </code></pre> <p>and....</p> <pre><code> 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); } </code></pre> <p>And finally from this class, onDrawFrame:</p> <pre><code>@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 } </code></pre> <p>Then, in my quad class:</p> <p>This declared at class level:</p> <pre><code> 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);" + "}"; </code></pre> <p>Then method for setting texture (don't think this is relevant to this problem though!!)</p> <pre><code> 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); } </code></pre> <p>And finally, my 'rotateQuad' method (which currently is supposed to draw and rotate the quad).</p> <pre><code>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); } </code></pre> <p>for Edit 2.</p> <p>This is my quad as drawn in the center of the screen. No rotation.</p> <p><img src="https://i.stack.imgur.com/dMKbH.png" alt="enter image description here"></p> <p>This is the same quad rotated at +45 Degrees with the code "gl_Position = a_position * uMVPMatrix;" + in my vertex shader (it's from a different project now so the shader variable is a_position and not vPosition), it's looks correct!</p> <p><img src="https://i.stack.imgur.com/LJXL0.png" alt="enter image description here"></p> <p>However, this is the same quad rotated at +45 Degrees with the 2 shader variables switched (so they read "gl_Position = uMVPMatrix * a_position;" - as you can see, it's not quite right.</p> <p>Also just a side note, you can't see it here as the quare is symetrical, but each method also rotates in the opposite direction to the other....</p> <p><img src="https://i.stack.imgur.com/p94Jv.png" alt="enter image description here"></p> <p>Any help appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload