Android OpenGL ES 2, Drawing squares

放肆的年华 提交于 2019-12-03 22:14:46
vertexCount = squareCoords.length/COORDS_PER_VERTEX; //Vertex count is the array divided by the size of the vertex ex. (x,y) or (x,y,z) 
vertexStride = COORDS_PER_VERTEX * 4;                //4 are how many bytes in a float

Let me know if that worked out for you, good luck.

I think your also missing the ModelViewProjection Matrix used to convert 3D space to 2D screen space. mvpMatrix should be passed in by the draw function draw(float[] mvpMatrix) Forgot to mention you also should use DrawElements(...) (used in example) if you do there's no need for the count or stride, just the length of an idicies array and a drawing buffer.

    // 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 square
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
                          GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

The tutorial is missing some steps: the final code for the square is here.

The example was intended to use glDrawElements instead of glDrawArrays which is indicated by the presence of the line: private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices.

This array specifies the desired vertices of 2 triangles. 0, 1 and 2 for the first. Then 0, 2 and 3 for the second. GL_TRANGLE_FAN just happens to work because it will draw the next triangle using the first vertex in the buffer, the last vertex used in previous triangle and the next vertex. For the second triangle this is 0, 2 and 3. Then 0, 3 and 4, etc. Had vertex 2 been 5, 5 and vertex 3 been -5, 5, the resulting fan would not have been a square.

Replace these lines:

// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

With these:

// Draw the square
GLES20.glDrawElements(
    GLES20.GL_TRIANGLES, drawOrder.length,
    GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!