If you have a regular figure, like the lizard, which is constructed completely of triangles, you can use vertex lists in openGL.
The data must be stored in a traditional (contiguous) array.
The data you register is transfered to the server side, this
will probably lead to a speed up.
You may also store other data such as normals, edges, colors and textures in this manner.
It will at least minimize calls.
Step 1, enable arrays
glEnableClientState(array type id)
GL_VERTEX_ARRAY
GL_COLOR_ARRAY
GL_INDEX_ARRAY
GL_NORMAL_ARRAY
GL_TEXTURE_COORD_ARRAY
GL_EDGE_FLAG_ARRAY
glDisableClientState(type of array) disables this mode.
Step 2, Specify the arrays
glVertexPointer(size, type, stride, *array)
size is the number of values per vertex, (2,3,4)
type is the openGL type of the data (FL_FLOAT, ...)
stride is the offset between verticies, 0 is normal here, but not required.
array is the base address of the array that holds the data.
This is different for the different types of arrays. s
glIndexPointer type, stride, * array)
glColorPointer
glNormalPointer
glCoordPointer
glEdgeFlagPointer
Step 3, Dereference and render.
glArrayElement(element number)
Is equivalent to a glVertex* call with those points.
glDrawElements(mode, count, type, * indicies)
Mode is one of the glBegin() modes. (GL_LINES, ...)
count is the number of elements
type is the type of indicies.
indicies is an array of idicies.
This will draw the shape specified.
This can also be used with arrays of mixed data types.
Transformations
As we have discussed all transformations can be accomplished
via a matrix vector multiply.
If you with to perform multiple transformations, you just
continue to multiply the individual transformation matricies
and this gives you a single transformation matrix.
As we have discussed, the order of the transformation is important.
In openGL, the last specified transformation is applied first.
glScale()
glRotate()
glTranslate()
draw_object
Will result is a translation, then a rotation, then a scaling.
Translation
The entire plane (in a 2-D case) or volume (in 3D) of space is
shifted by the amount specified by a translation vector.
This does not depend on the origin, only the scale of
the translation vector.
This does not deform or change the shape of objects.
glTranslate*(x,y,z)
Scaling
This will scale the entire plane/volume, by the scaling vector.
This is with respect to the origin.
If the scaling vector is not uniform (x=y=z) then the objects
will be deformed.
glScale*(x,y,z)
If we wish to scale about an arbitrary point (x,y,z)
Translate that point to the origin.
Perform the scaling
Translate the origin back to the point.
Reflection
This is scaling by a negative value.
glScalef(-1,1,1)
All positive x coordinates become negative, all negative become positive.
We will produce a mirror image of the object
Sheer
There are many other transformations we can do.
Sheer is one of these.
The equations are y=y, z=z, x=x+y cot(Θ);
This is equivalent to grabbing an object in its upper right and lower left extremes and stretching it by an angle Θ
No openGL call, we will have to do a
glMultMatrix*() to accomplish this.
Note glMultMatrix wants an array in column major order.
Rotation
Rotation in two space is about a point.
Rotation in three space is about a line.
Rotation about an axis (x,y,z) is somewhat easy, and we
have been doing that for a while.
The openGL rotation call, is for rotation about a line through the origin.
The call takes an angle, and a vector to specify the line.
To rotate about an arbitrary line, translate a point on the line to the origin, rotate, translate it back.
If we don't have rotation about an arbitrary line, we can derive it.
Project the vector into the xy plane by a rotation about
the y axis.
Project the new vector (which is in the xy plane onto the x axis by a rotation about the z axis)
Rotate by amount about the x axis.
Rotate back into the xy plane by a negative of the previous second rotation about the z axis.
Rotate back to the original vector by a negative of the first rotation about the y axis.