Note that there are some explanatory texts on larger screens.

plurals
  1. POAll of my matrix functions are not working? OpenGL ES 2.0
    text
    copied!<p>I am trying to make a projection matrix scaling the screen and making a coordination system. For some reason I don't think any of my matrix calling is working... the 3 function I am using are</p> <pre><code>Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0, 1000, -1, 10); Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f); Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); </code></pre> <p>Are they canceling each other out? anything wrong with it? (the full rendering class code is at the end) </p> <p>My main goal in doing this is eventually getting to a situation where when I make a square, I could provide coordinates such as <code>(200, 100,0) //x, y, z</code> which are not only between -1 and 1.</p> <p>Here is my full rendering class:</p> <pre><code>public class MyRenderer implements Renderer { private static final String TAG = "MyRenderer"; Square square; private final float[] mMVPMatrix = new float[16]; private final float[] mProjMatrix = new float[16]; private final float[] mVMatrix = new float[16]; private final float[] mRotationMatrix = new float[16]; private int camWidth,camHeight; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f); camWidth=480;camHeight=320; // initialize a square square = new Square(); } @Override public void onDrawFrame(GL10 nope) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); //set camera position GLES20.glViewport(0, 0, camWidth, camHeight); Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0, 1000, -10, 999999); Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f); Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); square.draw(mMVPMatrix); } @Override public void onSurfaceChanged(GL10 nope, int width, int height) { GLES20.glViewport(0, 0, camWidth, camHeight); Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0, 1000, -10, 999999); Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f); } public static int loadShader(int type, String shaderCode) { // create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) int shader = GLES20.glCreateShader(type); // add the source code to the shader and compile it GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; } } </code></pre> <p>My Square class (wasn't sure if needed or not, but just to be safe :) ) - </p> <pre><code>public class Square { private final String vertexShaderCode = "attribute vec4 vPosition;" + "void main() {" + " gl_Position = vPosition;" + "}"; private final String fragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}"; static final int COORDS_PER_VERTEX = 3; static float triangleCoords[] = { // in counterclockwise order: -0.5f, 0.5f, 0.0f, // top left -0.5f, -0.5f, 0.0f, // bottom left 0.5f, -0.5f, 0.0f, // bottom right 0.5f, 0.5f, 0.0f }; private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX; private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex // red-green-blue-alpha float color[] = { 0.63f, 0.76f, 0.22f, 1.0f }; private final int mProgram; private int mPositionHandle; private int mColorHandle; private int mMVPMatrixHandle; private FloatBuffer vertexBuffer; private ShortBuffer drawListBuffer; public Square() { ByteBuffer bb = ByteBuffer.allocateDirect( // # of coords values * 4 bytes per float triangleCoords.length * 4); // use native byte order bb.order(ByteOrder.nativeOrder()); // create a floating point buffer from the ByteBuffer vertexBuffer = bb.asFloatBuffer(); // add coordination to FloatBuffer vertexBuffer.put(triangleCoords); // set the buffer to read first coordinate vertexBuffer.position(0); ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); int vertexShader = ChizRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = ChizRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(mProgram, vertexShader); GLES20.glAttachShader(mProgram, fragmentShader); GLES20.glLinkProgram(mProgram); } public void draw(float[] mvpMatrix) { // Add program to OpenGL ES environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // Enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // Prepare the triangle coordinate data GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // set color for drawing the triangle GLES20.glUniform4fv(mColorHandle, 1, color, 0); // get handle to shape's transformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // apply the projection and view transformation GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Draw the triangle GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); // dispable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); } } </code></pre> <p>And lastly, just so you could have some visuals: </p> <p><img src="https://i.stack.imgur.com/GSzNU.png" alt="with and without metrix functions"></p> <p>This is how it looks on my phone with and without the three metrix functions mentioned before, it also seems that the only thing that did make any change with the width and height was <code>GLES20.glViewport(0, 0, camWidth, camHeight);</code></p> <p>It seems as if the metrix is doing nothing.</p>
 

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