views:

1756

answers:

3

How do you draw a cylinder with OpenGLES?

+1  A: 

You'll need to do it via object loading. You can't call on 3D shape primitives using Open GL ES.

Look through Jeff Lamarche's blog, there's lots of really good resources on how to object load there. link text

David Wong
Thanks for the response. However, I was told that you could draw a cylinder in OpenGLES by just using 'quads'. And I'd like to avoid using 3d-modeling software at this point. Could anyone give a more specific answer?
RexOnRoids
Quads aren't avaliable on OpenGL ES, as they were taken out to slim down the package. The easiest programmatic way to draw cylinders is to use GLUT but that isn't avaliable for OpenGL ES.
David Wong
I ended up downloading blender and installing someone's Objective-C export script to create/export a 3D model into an iPhone SDK OpenGL project. Took a little while but once everything was in place It worked fine. I am glad that I did not try to write a routine in Xcode manually to handle the cylinders for the 3D model since the 3D model ending up having about 500 vertices and around 700 faces LOL. Thanks
RexOnRoids
+1  A: 

You can indeed draw a cylinder in OpenGL ES by calculating the geometry of the object. The open source GLUT|ES project has geometry drawing routines for solids (cylinders, spheres, etc.) within its glutes_geometry.c source file. Unfortunately, these functions use the glBegin() and glEnd() calls, which aren't present in OpenGL ES.

Code for a partially working cylinder implementation for OpenGL ES can be found in the forum thread here.

Brad Larson
+1  A: 

First step is to write a subroutine that draws a triangle. I'll leave that up to you. Then just draw a series of triangles the make up the shape of a cylinder. The trick is to approximate a circle with a polygon with a large number of sides like 64. Here's some pseudo-code off the top of my head.

for (i = 0; i < 64; i++)
{
    angle = 360 * i / 63;  // Or perhaps 2 * PI * i / 63
    cx[i] = sin(angle);
    cy[i] = cos(angle);
}

for (i = 0; i < 63; i++)
{
    v0 = Vertex(cx[i], cy[i], 0);
    v1 = Vertex(cx[i + 1], cy[i + 1], 0);
    v2 = Vertex(cx[i], cy[i], 1);
    v3 = Vertex(cx[i + 1], cy[i + 1], 1);

    DrawTriangle(v0, v1, v2);
    DrawTriangle(v1, v3, v2);
    // If you have it:  DrawQuad(v0, v1, v3, v2);
}

There is almost certainly a mistake in the code. Most likely is that I've screwed up the winding order in the triangle draws so you could end up with only half the triangles apparently visible or a very odd case with only the back visible.

Performance will soon want you drawing triangle strips and fans for efficiency, but this should get you started.

George Phillips