how to use gl_polygon to draw 3d cylinder - c++

i am new opengl learner. i know there is build in function to draw cylinder in glut something like
GLUquadricObj *quadratic;
quadratic=gluNewQuadric();
gluCylinder(quadratic,0.6f,0.6f,1.5f,20,20);
However, is there any other way to draw cylinder using glBegin(GL_POLYGON) and glvertex3d, i am not quite clear about the algorithm behind, please help.

Use sin/cos to trace around a circle in segments. Extend those segments upwards with 2 triangles. Then connect them to close the ends.
You may be able to find the source online for freeglut. Here's some Java code I found: http://massapi.com/source/lwjgl-source-2.7.1/src/java/org/lwjgl/util/glu/Cylinder.java.html

Related

Change the position of a sphere without recreating it

I am writing a code in OpenGL, in c++ on Linux, where i need to draw a several sphere in the 3D space, for drawing it I use the glutSolidSphere( GLdouble(radius), GLint(slices), GLint(stacks) ) method, everytime, in the draw function, the glutSolidSphere is called a lot of times and after the sphere is traslated in the right position.
But I have noticed that when the program draw several spheres there is a framerate problem, so i was thinking if there was a method that allow me to "store" the model of the sphere without recreate it everytime and just change position.
I am not an OpenGL expert, sorry if i have committed english language errors.
The answer of #datenwolf in this topic is perfect, he said :
In OpenGL you don't create objects, you just draw them. Once they are drawn, OpenGL no longer cares about what geometry you sent it.
glutSolidSphere is just sending drawing commands to OpenGL.
So if you have performance issues you will need to look for others ways to improve performance like multithreading or maybe look to implement your own sphere draw function (google or stackoverflow research).
I heard that GluSolidSphere call glBegin(…) / glEnd() and these can be slow so maybe draw all your spheres with a loop so you have only one call of glBegin(…) / glEnd()

3D spheres and adding textures in OpenGL

I have been asked to do 3D sphere and adding textures to it so that it looks like different planets in the Solar System. However 3ds max was not mentioned as mandatory.
So, how can I make 3D spheres using OpenGL and add textures to it? using glutsphere or am I suppose to do it some other method and how to textures ?
The obvious route would be gluSphere (note, it's glu, not glut) with gluQuadricTexture to get the texturing done.
I am not sure if glutSolidSphere has texture coordinates (as far as I can remeber they were not correct, or not existant). I remember that this was a great resource to get me started on the subject though:
http://paulbourke.net/texture_colour/texturemap/
EDIT:
I just remembered that subdividing an icosahedron gives a better sphere. Also texture coordinates are easier to implement that way:
see here:
http://www.gamedev.net/topic/116312-request-for-help-texture-mapping-a-subdivided-icosahedron/
and
http://www.sulaco.co.za/drawing_icosahedron_tutorial.htm
and
http://student.ulb.ac.be/~claugero/sphere/

Drawing a simple anti-aliased stickman in OpenGL

I would like to draw a simple 2D stickman on the screen. I also want it to be anti-aliased.
The problem is that I want to use a bones system, which will be written after I would know how to draw the stickman itself based on the joints positions. This means I can't use sprites - I want my stickman to be fully controlable in the code.
It would be great if it will be possible to draw curves too.
Drawing a 3D stickman using a model would also be great if not better. The camera will be positioned like it's 2D, but I would still have depth. The problem is that I only have experience in Maya, and exporting and vertex weighting of the model in OpenGL seems like a mess...
I tried to find libraries for 2D anti-aliased drawing or enable multi-sampling and draw normally, but I had no luck. I also tried to use OpenGL's native anti-aliasing but it seems deprecated and the line joins are bad...
I don't want it to be too complicated because, well, it shouldn't be - it's just the first part of my program, and it's drawing a stickman...
I hope you guys can help me, I'm sure you know better than me :)
You could enable GL_SMOOTH. To check if you device supports your required line width for smooth lines, you can use glGet(GL_SMOOTH_LINE_WIDTH_RANGE);
If you want your code to be generic, you can also use antialiased textures.
Take a look at this link
http://www.opengl.org/resources/code/samples/advanced/advanced97/notes/node62.html
The only way to get antialiasing is use GL library which knows how to get antialiased GL context, for example, SDL. As of stickman, you can draw him with colored polygons.

How to draw a snake in opengl?

I am doing a snake and ladder game in VC++ using opengl.
I think i can draw a snake using arc and line. But i don't have an idea of how to do it in opengl. Can anyone give me some guidance?
Arc must be broken into small line segments manually. GL can render points, lines, triangles, quads and polygons only.
You might want to take a look at spline interpolation, e.g. Bezier splines, B-splines etc.

Most efficient way to draw circles for polygon outlines

I'm using OpenGL and was told I should draw circles at each vertex of my outline to get smoothness. I tried this and it works great. The problem is speed. It crippled my application to draw a circle at each vertex. I'm not sure how else to fix the anomaly of my outlines other than circles, but using display lists and trying with vertex array both were brutally slow. Thanks
see: Edges on polygon outlines not always correct
One (perhaps too fancy) alternative is to draw a single polygon that bounds the circle (say, a quad), and then use a fragment program to discard the fragments. This would not be entirely trivial to write, but I would bet it's the fastest way.
You would simply pass the circle parameters to the fragment program and discard the fragment if the distance from the fragment center to the center of the circle is bigger than the desired radius.
Have you seen this article?
..or if you have access to the GL utility library, you could use gluDisk