21

I have OpenGL code like the following that I'd like to port to OpenGL ES 2.0:

for (surfnum=0;surfnum < surftotal;surfnum++){
    for (i=0;i<triNum[surfnum];i++){
        glBegin(GL_POLYGON);
        glNormal3fv(triArray[surfnum][i].normpt1);
        glVertex3fv(triArray[surfnum][i].pt1);
        glNormal3fv(triArray[surfnum][i].normpt2);
        glVertex3fv(triArray[surfnum][i].pt2);
        glNormal3fv(triArray[surfnum][i].normpt3);
        glVertex3fv(triArray[surfnum][i].pt3);
        glEnd();
        glFlush();
    }       
}

OpenGL ES 2.0 lacks GL_POLYGON, glNormal3fv, glVertex3fv, glEnd, glBegin, etc., so how do I replace these functions?

P.S.: I am doing this in Ubuntu 10.10 through an emulator.

badcc
  • 222
  • 4
  • 18
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74
  • I think some of your code was garbled when you copied it in. – Nicol Bolas Jun 20 '11 at 07:51
  • You realize, that your polygon is actually just a triangle? And GL_TIRANGLES is perfectly present in ES 2.0. But you will have to use vertex arrays/buffers. See datenwolf's answer for this. – Christian Rau Jun 20 '11 at 08:21

2 Answers2

11

You use Vertex Buffer Objects. Tutorial at NeHe: http://nehe.gamedev.net/tutorial/vertex_buffer_objects/22002/

The tutorial (the bulk text) is written for Windows. OpenGL-ES 2 on Android differs by that you don't have to load extensions manually and are given a properly prepared OpenGL context by the egl... functions.

Another readable tutorial is http://www.songho.ca/opengl/gl_vbo.html

GL_POLYGONS have been abandoned from OpenGL-3 and -ES since they're cumbersome to work with and almost never used. Also GL_POLYGON can be perfectly replaced by GL_TRIANGLE_FAN. Or you do the clean thing and tesselate polygonal geometry into triangles yourself.

Joey Foo
  • 5
  • 3
datenwolf
  • 159,371
  • 13
  • 185
  • 298
6

A basic example, to draw a triangle in OpenGL ES:

GLfloat glverts[9];
glVertexPointer(3, GL_FLOAT, 0, glverts);
glEnableClientState(GL_VERTEX_ARRAY);

//fill in vertex positions with your data
for (int i = 0; i < 3; i++) {
  glverts[i*3]   = ...;
  glverts[i*3+1] = ...;
  glverts[i*3+2] = ...;
}

glDrawArrays(GL_TRIANGLE_FAN, 0, 3);

EDIT: sorry, this is for OpenGL ES 1.1, not 2.0

iforce2d
  • 8,194
  • 3
  • 29
  • 40
  • 1
    What you are telling me is used in opengl es 1.1 but i want to know for opengl-es 2.0 ... – Sudhanshu Gupta Jun 22 '11 at 06:34
  • I see, my mistake. I don't know much about 2.0 but I found a good walkthrough in about 30 seconds on google: http://www.webreference.com/programming/opengl_es/ – iforce2d Jun 22 '11 at 10:14
  • Yaah i have already done the example for triangle in ubuntu with emulator but there is only vertexbufferarray and i want to use normals also . so how am gono do that.. – Sudhanshu Gupta Jun 23 '11 at 02:16
  • 1
    @Sudhanshu Gupta - This will work perfectly fine in OpenGL ES 2.0, just as it does in 1.1. You will of course need to have a shader program to handle the vertex and fragment processing around this in 2.0. – Brad Larson Jun 24 '11 at 22:37