I am working on an OpenGL 2D game with sprite graphics. I was recently advised that I should use OpenGL ES calls as it is a subset of OpenGL and would allow me to port it more easily to mobile platforms. The majority of the code is just calls to a draw_image function, which is defined so:
void draw_img(float x, float y, float w, float h, GLuint tex,float r=1,float g=1, float b=1) {
glColor3f(r,g,b);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f( x, y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(w+x, y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f( w+x, h+y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f( x, h+y);
glEnd();
}
What do I need to change to make this OpenGL ES compatible? Also, the reason I am using fixed-function rather than shaders is that I am developing on a machine which doesn’t support GLSL.
In OpenGL ES 1.1 use the
glVertexPointer(),glColorPointer(),glTexCoordPointer()andglDrawArrays()functions to draw a quad. In contrast to your OpenGL implementation, you will have to describe the structures (vectors, colors, texture coordinates) that your quad consists of instead of just using the built-inglTexCoord2f,glVertex2fandglColor3fmethods.Here is some example code that should do what you want. (I have used the argument names you used in your function definition, so it should be simple to port your code from the example.)
First, you need to define a structure for one vertex of your quad. This will hold the quad vertex positions, colors and texture coordinates.
Then, you should define a structure describing the whole quad consisting of four vertices:
Now, instantiate your quad and assign quad vertex information (positions, colors, texture coordinates):
Now tell OpenGL how to draw the quad. The calls to
gl...Pointerprovide OpenGL with the right offsets and sizes to your vertex structure’s values, so it can later use that information for drawing the quad.Finally, assign the texture and draw the quad.
glDrawArraystells OpenGL to use the previously defined offsets together with the values contained in yourQuadobject to draw the shape defined by4vertices.Please also note that it is perfectly OK to use OpenGL ES 1 if you don’t need shaders. The main difference between ES1 and ES2 is that, in ES2, there is no fixed pipeline, so you would need to implement a matrix stack plus shaders for the basic rendering on your own. If you are fine with the functionality offered by the fixed pipeline, just use OpenGL ES 1.