PDA

View Full Version : Trouble porting OpenGL code to iPhone's OpenGL ES



vamshivakul
04-15-2009, 10:53 AM
Hi friends,
I am new to OpenGL..

Here, is the sample code which I am using. This code is in OpenGL. I want to convert it to OpenGL ES. glArrayElement() is not supported in OpenGL ES. Instead, it was recommended to use glDrawElements(), glDrawArrays(). I am not able to convert this. Can you please help me out to convert the code.

glTexCoordPointer(2,GL_FLOAT,sizeof(struct texcoord),tex);
glColorPointer(4,GL_UNSIGNED_BYTE,4,col);
glNormalPointer(GL_FLOAT,sizeof(vector),wet->n);
glVertexPointer(2,GL_FLOAT,sizeof(struct vertexcoord),vert);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glLockArraysEXT(0,wet->w*wet->h);
i=0;
for(int y=0;y<wet->h-1;y++)
{
glBegin(GL_TRIANGLE_STRIP);
for(int x=0;x<wet->w;x++)
{
glArrayElement(i);
glArrayElement(i+wet->w);
i++;
}
glEnd();
}
glUnlockArraysEXT();

And one more question is do we have any replacement for glEnable(GL_TEXTURE_RECTANGLE_EXT) in OpenGL ES.

Thanks in advance
Vamshi

Xmas
04-16-2009, 06:40 AM
You simply need to create an index array which contains the index values you pass to glArrayElement, then pass that index array as a whole to glDrawElements. Of course as long as your vertex order stays constant you need to create this index array only once. In addition, you can stitch triangle strips together with degenerate triangles in order to reduce the number of draw calls since they have some overhead.


// Initialize index buffer only once
int indexCount = 2 * (wet->w + 1) * (wet->h - 1) - 2;
GLushort* indices = new GLushort[indexCount];
GLushort* ptr = indices;
for (int y=0; y<wet->h-1; y++)
{
for(int x=0;x<wet->w;x++)
{
*ptr++ = y * wet->w + x;
*ptr++ = (y+1) * wet->w + x;
}
if (y != wet->h - 2)
{
// insert degenerate triangles to stitch strips together
*ptr++ = (y+2) * wet->w - 1;
*ptr++ = (y+1) * wet->w;
}
}

// in your render loop: set up client arrays, then just call
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_SHORT, indices);


And one more question is do we have any replacement for glEnable(GL_TEXTURE_RECTANGLE_EXT) in OpenGL ES.
Textures need to have power-of-two dimensions and use normalized texture coordinates. You could pad your rectangle textures to the next higher power of two and scale the texture coordinates accordingly.

vamshivakul
04-16-2009, 12:37 PM
Thanks alot. The code which you have sent is working fine. Thank you very much...

Can you please help me out in my other question. Iam still strucked with glEnable(GL_TEXTURE_RECTANGLE_EXT).
I am using glEnable(GL_TEXTURE_2D) instead of glEnable(GL_TEXTURE_RECTANGLE_EXT) for texture image. Iam not getting the same image. I am not able to fix this...

The OpenGL code is:
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_RECTANGLE_EXT);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT,backtex);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
glMatrixMode(GL_TEXTURE);

I have converted the above code to OpenGL ES in this way :
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,backtex);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
glMatrixMode(GL_TEXTURE);

Note: "backtex" contains the texture image.

Can you please help me out.

Thanks in advance,
Vamshi

Ifer
10-26-2011, 01:27 AM
Thank you guys for your help and patience. Are there any basic explanations about how to port certain OpenGL 1.3 code to OpenGL ES 1.0?