Reputation: 5503
As for OpenGL ES 2 I have understood that there no longer are any Matrices (matrix stack) in it. So I have to create my own matrices.
What I want to do is just draw some simple 2D graphics, like a couple of rectangles.
Lots of code I find use OpenGL ES 1 or older OpenGL where there still was a matrix stack, so I can't use it directly in 2.0.
I believe I want code that does something like this
public void onSurfaceCreated(GL10 unused, EGLConfig eglConfig) {
// Set the background frame color
GLES20.glClearColor(0.1f, 0.3f, 0.5f, 1.0f);
// Set 2D drawing mode
GLES20.glViewport(0, 0, windowWidth, windowHeight);
GLES20.glMatrixMode(GL_PROJECTION);
GLES20.glLoadIdentity();
GLES20.glOrtho(0, windowWidth, windowHeight, 0, -1, 1);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
}
but there are no longer any methods glMatrixMode, glLoadIdentity, glOrtho.
How would I translate this into OpenGL ES 2 to set it up for 2D drawing? I believe I can use the Matrix class procvided by android, but I am not sure how.
Upvotes: 2
Views: 7979
Reputation: 589
Basically, you don't "set" any matrices with OpenGL ES 2.0 (as you setup other things, like the viewport, disable GL_DEPTH_TEST, etc). Instead, you create and manage the matrices yourself, passing them to your shaders on each frame render.
You can just create an orthographic projection matrix, then pass it to your shader as a uniform (eg: glUniformMatrix4fv).
I cannot comment on exactly how to do that with Android, but if you have a Matrix class, it should have functions to create an orthographic projection matrix. Then you would just pass a pointer to the data (ie: the 16 floats - 4x4 matrix) to glUniformMatrix4fv before calling glDrawArrays/glDrawElements/etc.
So, your above setup function would be much smaller..
public void onSurfaceCreated(GL10 unused, EGLConfig eglConfig) {
// Set the background frame color
GLES20.glClearColor(0.1f, 0.3f, 0.5f, 1.0f);
// Set 2D drawing mode
GLES20.glViewport(0, 0, windowWidth, windowHeight);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
}
But your render functions would look different (you could still create your ortho projection matrix above... just making sure to update it if necessary.. ie: screen resizing/moving/etc).
This page covers it pretty well for Android:
http://www.learnopengles.com/android-lesson-one-getting-started/
Upvotes: 2