# Create cylinder using OpenGL-ES [closed]

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();
}
``````
-

## closed as too localized by Byte56♦, bummzack, Sean Middleditch, Trevor Powell, Josh Petrie♦Mar 28 '13 at 15:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

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/… – Shivan Dragon Mar 20 '13 at 12:54
I think this is a "can I have the code" question. – Byte56 Mar 20 '13 at 13:52

## 1 Answer

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

-
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 – DattatrayD Mar 20 '13 at 13:04
Which line causes the error? Or is that whenever you use glBegin it pops up something? – AB. Mar 20 '13 at 14:23
Btw check this answer : stackoverflow.com/a/11453019/2173834 And more precisely answer provided by user1401452. Is that your problem? – AB. Mar 20 '13 at 15:04
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) – Sparksis Mar 20 '13 at 17:33
geBegin and glEnd Pops message symbol not found, i am using this code in cocos3d environment – DattatrayD Mar 21 '13 at 6:55