GLUGG
OpenGL Utilities for Geometry Generation
API only. Also usually in the downladed archive.
Programming interface
void gluggBegin(int mode);
Start recording vertices using the selected mode. Modes include:
GLUGG_TRIANGLES: Enter triangles one vertex at a time.
GLUGG_QUADS: Enter quads of four vertices;
GLUGG_TRIANGLE_FAN: For every vertex, a new triangle is made from the new, the previous and the first.
GLUGG_TRIANGLE_STRIP: For every vertex, a new triangle is made from the new and two previous ones.
void gluggMode(int newMode);
Change the mode while recording a shape.
void gluggVertex(float x, float y, float z);
void gluggVertexv(Point3D p);
Enter a new vertex.
void gluggNormal(float x, float y, float z);
void gluggNormalv(Point3D n);
Enter a normal vector for the next vertex. Note that this should be called before gluggVertex/gluggVertexv!
void gluggTexCoord(float s, float t);
Enter a texture coordinates for the next vertex. Note that this should be called before gluggVertex/gluggVertexv!
void gluggColor(float r, float g, float b);
void gluggColorv(Point3D n);
Enter a color in RGB format for the next vertex. Note that this should be called before gluggVertex/gluggVertexv!
void gluggBuildBezier(Point3D *vertices, int *indices, float step);
Calls that encapsulate all model data in a struct called gluggModel. This simplifies the code somewhat and also makes it possible to dispose of the model:
gluggModel gluggBuildModel(char optimize);
Builds a gluggModel struct which can be drawn by gluggDrawModel.
void gluggDrawModel(gluggModel m, GLuint program)
Draws the model with the specified shader.
void gluggDisposeModel(gluggModel m);
Disposes all buffers for a model.
Calls for building with Bézier patches:
Builds a Bézier patch using gluggVertex etc.
gluggModel gluggBuildBezierPatchModel(vec3 *vertices, int *indices, int startPatch, int endPatch, float step)
Builds an entire model from a set of Bézier patches.
Old or special-purpose calls:
The call gluggEnd builds the model and returns a GLuint and an int, all you need to draw it with glDrawArrays or glDrawElements! The advantage of this over gluggBuildModel and gluggDrawModel is that we are calling straight into OpenGL with no padding in between. This is good, but that costs three things: (1) We must use two variables instead of one. (2) It is hard-wired to use one specific shader. (3) The references to the buffers are lost so it is not possible to modify it or dispose it later.
GLuint gluggEnd(int *count, GLuint program, char optimize);
Ends the recording and clears all internal variables. Uploads all data to the GPU, returning a VAO and the number of vertices, optionally adapting the data to use an index list instead.
Note that gluggEnd assumes your shader variables to be named exactly "in_Position”, "in_Normal” and "in_TexCoord”! (This can be cutomized.)
Also note that the shader program must be initialized!
void gluggDisposeData();
Dispose internal variables without calling gluggEnd or gluggBuildModel. This should only be used when you need to access the data directly in order to do something user defined afterwards.
Shader variables
By default, the shader attributes used by gluggEnd are "in_Position” for vertices, "in_Normal" for normal vectors, "in_TexCoord" for texture coordinates and “in_Color” for colors. However, you can change these using the following calls:
void gluggSetPositionName(char *name)
void gluggSetNormalName(char *name)
void gluggSetColorName(char *name)
void gluggSetTexCoordName(char *name)
Getting raw data from GLUGG:
Below follow the calls for inspection of internal variables. Important! This is where you may get you data before you call gluggEnd.
GLfloat *gluggGetVertexArray();
GLfloat *gluggGetNormalsArray();
GLfloat *gluggGetColorsArray();
GLfloat *gluggGetTexCoordArray();
GLfloat *gluggGetIndexArray();
int gluggGetNumVertices();
int gluggGetNumIndices();
void gluggArraysToElements(); // Needed before gluggGetNumIndices, corresponds to the “optimize” option in gluggEnd.
I hope these are self-explanatory. They are only needed when you need to have access to the actual data on the CPU after creating them.
Matrix stack support
The matrix stack support is another case of making it easier to port old code, but also useful for combining existing shapes, something I want to add more support for. These are highly similar to old-style OpenGL, but are, obviously, intended for intitialization only, after which rendering is fast. (Added for the “snowman” demo where four spheres and a cone are combined to one shape.)
void gluggPushMatrix();
void gluggPullMatrix();
void gluggPopMatrix();
These calls will extend or shorten the matrix stack, so you can save an old matrix for later. gluggPushPatrix will duplicate the top item.
void gluggTranslate(GLfloat x, GLfloat y, GLfloat z);
void gluggRotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
void gluggScale(GLfloat x, GLfloat y, GLfloat z);
These calls create the desired matrix (using VectorUtils) and multiply it onto the current matrix (top of stack).
mat4 gluggCurrentMatrix();
void gluggSetMatrix(mat4 m);
Get or set the matrix on top of the stack.
void gluggMultMatrix(mat4 m);
Multiply a custom matrix on the top of the stack.
gluggShapes programming interface
void gluggCube(GLfloat size);
void gluggDoughnut(GLfloat r, GLfloat R, GLint nsides, GLint rings);
void gluggDodecahedron(float size);
void gluggOctahedron(GLfloat size);
void gluggIcosahedron(GLfloat size);
void gluggTetrahedron(GLfloat size);
void gluggEllipsoid(int aSlices, int hSlices, vec3 dimensions);
void gluggSphere(int aSlices, int hSlices, float radius);
void gluggCylinder(int aSlices, float height, float width);
void gluggCone(int aSlices, float height, float width);
void gluggCylinderAlt(int aSlices, float height, float topwidth, float bottomwidth);
Each call generates the shape in question. They do not upload the model but must be within a gluggBegin/gluggEnd recording. The advantage is that several models can be combined into one, typically using the matrix calls between to place them.
Demos
gluggTriangle
This folder holds not one but three simple demos:
gluggTriangle
gluggColorTriangle
gluggModes
Every package like this should have an example as simple as possible. Rendering a single triangle, all white or with color gradients, is as simple as it gets, and is also as close you can get to the usual first examples of old OpenGL. The gluggModes demo expands this to showing the four kinds of modes in the simplest possible way.
gluggGears
Since the Gears demo was the origin of GLUGG, it should be included, even though it is pretty similar to my other modernization of the program. Here the actual upload to the GPU is somewhat simplified but that is a relatively small part of the program.
boing
Another old classic demo, modernised to a more current style, the famous Amiga demo “boing”. This has gone through some iterations before coming here and even the original GLboing is nowhere to be found, but as far as I know this version is the only one that leaves OpenGL 2 behind (which is true for “gears” as well).
gluggShapesDemo
This demo is based on the glut_shapes file in FreeGLUT. MicroGlut has no such default shapes built-in so here you can generate most of them with GLUGG, which is in the separate gluggShapes.c file. The file generates many different shapes: Tetrahedron, cube, torus (doughnut), octahedron, dodecahedron, icosahedron.
gluggMoreShapes
This demo covers a few shapes not in glut_shapes, now found in glutShapes.c.
gluggSphere
A neat generation of a sphere from a tetrahedron, based on an example for which the source is lost.
gluggSweeper
Generation of shapes from a set of points, i.e. vases.
surface
A simple Bézier patch demo, where four patches are controlled by an invisible “joystick” that ensures continuity over the edges of the patches.
simplesnowman
A demo based on the snowman from lighthouse3d. The point is to use the matrix stack to build a combination of multiple shapes with branching dependencies. It also uses gluggShapes for generating the shapes.
teaset
These demos generate the entire Utah Teaset, from which the famous Utah Teapot comes, using the Bézier functions in GLUGG. “teaset3glugg” (where the 3 implies OpenGL 3) generates and renders the shapes, while “saveteaset” also saves some shapes to disc. The file saveobj.c is included and can be used separately.