1
\$\begingroup\$

I want to draw cone using OpenGL ES I have gone through many links to draw cylinder but I don't have any idea or approach to draw the cylinder. I've seen the gluCylinder function for OpenGL, but no equivalent function in OpenGL-ES.

i tried the below code

float radius, halfLength;
int slices;
for(int i=0; i<slices; i++) {
    float theta = ((float)i)*2.0*M_PI;
    float nextTheta = ((float)i+1)*2.0*M_PI;
    glBegin(GL_TRIANGLE_STRIP);
    /*vertex at middle of end */ glVertex3f(0.0, halfLength, 0.0);
    /*vertices at edges of circle*/ glVertex3f(radius*cos(theta), 
    halfLength, radius*sin(theta));
    glVertex3f (radius*cos(nextTheta), halfLength, radius*sin(nextTheta));
    /* the same vertices at the bottom of the cylinder*/
    glVertex3f (radius*cos(nextTheta), -halfLength, radius*sin(nextTheta));
    glVertex3f(radius*cos(theta), -halfLength, radius*sin(theta));
    glVertex3f(0.0, -halfLength, 0.0);
    glEnd();
}
\$\endgroup\$
2
  • \$\begingroup\$ Aron Boguta's answer is excellent. I would just want to comment that if you're trying to do this for practical reasons (you will want to have more complex geometry in Open GL), it's best if you at least find a library that lets you load meshes made with specialized software (like 3ds Max or Blender). Usually Obj+Mat or fbx formats are supported by many such libraries (and the 3d softwares as well). Example: stackoverflow.com/questions/9452533/opengl-obj-loader-for-ios stackoverflow.com/questions/6691927/… \$\endgroup\$ Mar 20, 2013 at 12:54
  • \$\begingroup\$ I think this is a "can I have the code" question. \$\endgroup\$
    – House
    Mar 20, 2013 at 13:52

1 Answer 1

2
\$\begingroup\$

Basically the formula would be as follows :

(assuming Y points up)

you need to declare "slices", "HEIGHT", r

x- origin of the bottem circle on X axis

y- origin of the bottem circle on Y axis

z- origin of the bottem circle on Z axis

glBegin(GL_TRIANGLE_FAN);
// bottom circle
glVertex3f(x,y,z);
for(int i=0, float alpha=0; i<slices; i++) glVertex3f(x + cos((float)i/slices * 2 *PI)*r,
y, z+sin((float)i/slices * 2 * PI)*r);
glEnd();

glBegin(GL_TRIANGLE_FAN);
//top circle
glVertex3f(x,y + HEIGHT,z);
for(int i=0, float alpha=0; i<slices; i++) glVertex3f(x + cos((float)i/slices * 2 *PI)*r,
y+ HEIGHT, z+sin((float)i/slices * 2 * PI)*r);
glEnd();

// the rest
glBegin(GL_TRIANGLE_STRIP);
for(int i=0, float alpha=0; i<slices; i++) 
{
   glVertex3f(x + cos((float)i/slices * 2 *PI)*r,y, z+sin((float)i/slices * 2 * PI)*r);
   glVertex3f(x + cos((float)i/slices * 2 *PI)*r,y+ HEIGHT, z+sin((float)i/slices * 2 *      PI)*r);
}
glEnd();

This should be just fine

\$\endgroup\$
5
  • \$\begingroup\$ this code is not working in opengles 1.1 , it gives error on glBegin and glEnd Error: Implicit declaration of function 'glBegin' is invalid in C99 \$\endgroup\$ Mar 20, 2013 at 13:04
  • \$\begingroup\$ Which line causes the error? Or is that whenever you use glBegin it pops up something? \$\endgroup\$
    – AB.
    Mar 20, 2013 at 14:23
  • \$\begingroup\$ Btw check this answer : stackoverflow.com/a/11453019/2173834 And more precisely answer provided by user1401452. Is that your problem? \$\endgroup\$
    – AB.
    Mar 20, 2013 at 15:04
  • 1
    \$\begingroup\$ One of the limitations of opengl ES is that you are not permitted to use certain functions that computationally expensive. What you could do is precompute your cylinder (which is the recommended method) \$\endgroup\$
    – Colton
    Mar 20, 2013 at 17:33
  • \$\begingroup\$ geBegin and glEnd Pops message symbol not found, i am using this code in cocos3d environment \$\endgroup\$ Mar 21, 2013 at 6:55

Not the answer you're looking for? Browse other questions tagged .