How to draw a snake in opengl? - 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.

Related

OpenGL: Drawing 3D curves with varying width

I am trying to draw a curve in 3D in which the width may vary. I am doing something similar to what described in this tutorial.
Meaning, i create quads (and triangles) with the desired width instead of using OpenGL width. The problem is that the triangles are actually in 2D - they are drawn in a specific plane. The width will look different, of course, from different angles.
Am i tackling this problem in the correct way? Is there a good solution for my problem?
This is a tough question that i have recently come across while developing a game myself.
They way i handled it instead of drawing a line i would draw boxes as segments of the line. You could potential make cylinders but if your using lighting you could cheat boxes to look like cylinders.
You could also cross two quads to make an X of sorts along the length of the string.

how to use gl_polygon to draw 3d cylinder

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

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

Why is there no circle or ellipse primitive in OpenGL?

Circles are one of the basics geometric entities. Yet there is no primitives defined in OpenGL for this, like lines or polygons. Why so? It's a little annoying to include custom headers for this all the time!
Any specific reason to omit it?
While circles may be basic shapes they aren't as basic as points, lines or triangles when it comes to rasterisation. The first graphic cards with 3D acceleration were designed to do one thing very well, rasterise triangles (and lines and points because they were trivial to add). Adding any more complex shapes would have made the card a lot more expensive while adding only little functionality.
But there's another reason for not including circles/ellipses. They don't connect. You can't build a 3D model out of them and you can't connect triangles to them without adding gaps or overlapping parts. So for circles to be useful you also need other shapes like curves and other more advanced surfaces (e.g. NURBS). Circles alone are only useful as "big points" which can also be done with a quad and a circle shaped texture, or triangles.
If you are using "custom headers" for circles you should be aware that those probably create a triangle model that form your "circles".
Because historically, video cards have rendered points, lines, and triangles.
You calculate curves using short enough lines so the video card doesn't have to.
Because graphic cards operate on 3-dimensional points, lines and triangles. A circle requires curves or splines. It cannot be perfectly represented by a "normal" 3D primitive, only approximated as an N-gon (so it will look like a circle at a certain distance). If you want a circle, write the routine yourself (it isn't hard to do). Either draw it as an N-gon, or make a square (2 triangles) and cut a circle out of it it using fragment shader (you can get a perfect circle this way).
You could always use gluSphere (if a three-dimensional shape is what you're looking for).
If you want to draw a two-dimensional circle you're stuck with custom methods. I'd go with a triangle fan.
The primitives are called primitives for a reason :)

OpenGL, remove sections from a shape

In OpenGL, how can one cut a triangle shaped hole from a square? making the hole transparent.
I'm also using SDL, maybe it can be achieved with an SDL surface?
While doing it on a texture is truly the easier way out, if you need it to be a real shape, you might try using the GLUtesselator from GLU toolkit. See a tutorial for it here.
General usage is that you create a tesselator object, create two contours (the outer and the inner in a reverse direction) and the tesselator translates that into pure OpenGL commands. Of course if it's efficiency you're seeking you should implement or find some higher order system that operates on vertex buffers.
You can use a texture and alpha blending: the texture would contain a transparent triangle. See this tutorial on blending.
EDIT: Of course alpha blending doesn't change the geometry. For that you need to perform treatments that are more complicated. See this tutorial on realtime CSG.
Reference: Constructive Solid Geometry