Circles, Ellipses, and Regular Polygons in OpenGL ES


The iPhone's use of OpenGL ES indicates that a lot of games and other applications can be ported relatively swiftly. Unfortunately, the iPhone uses a relatively old edition of OpenGL ES (1.1), and by the very characteristics of an included visual powerplant, a lot of the niceties of the large OpenGL have been eliminated, such as features to attract a lot of normal old fashioned designs.

Lines and pieces are very simple, of course, but what about more complicated shapes? Well, you're on your own for those, so unless you're relaxed with doing a little numbers, you're going to have issues with OpenGL ES. Here's one example - how do you attract an ellipse or range in OpenGL? I'm not dealing with a area (we'll look at that in a later article, maybe), but a two-dimensional circle? Or a triangle?

These two features attract (respectively) an ellipse and a range (which is just an ellipse with equivalent level and width) at a given area, either packed, or discussed. But, they actually do more then that. You can specify how specific you want the ellipse to be using the messages debate. If you specify, three, for example, it will attract a three-sided polygon (aka a triangle). If you specify 360, you'll get a 360-sided normal polygon, which is going to look like a range on all but the greatest features. For most reasons on the iPhone, a value above 12 will look like a range. A value less than 3 won't operate, but the problem examining is your liability.


void GLDrawEllipse (int segments, CGFloat width, CGFloat height, CGPoint center, bool filled)
{
glTranslatef(center.x, center.y, 0.0);
GLfloat vertices[segments*2];
int count=0;
for (GLfloat i = 0; i < 360.0f; i+=(360.0f/segments))
{
vertices[count++] = (cos(degreesToRadian(i))*width);
vertices[count++] = (sin(degreesToRadian(i))*height);
}
glVertexPointer (2, GL_FLOAT , 0, vertices);
glDrawArrays ((filled) ? GL_TRIANGLE_FAN : GL_LINE_LOOP, 0, segments);
}
void GLDrawCircle (int circleSegments, CGFloat circleSize, CGPoint center, bool filled)
{
GLDrawEllipse(circleSegments, circleSize, circleSize, center, filled);
}

0 comments:

Post a Comment

Powered by Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More