My Other Notes
Java Dummy Unix Java Script HTML Latex Standard C
Homepage of openGL
Red
book online: OpenGL programming Guide
Blue
book online: OpenGL Reference Manual
Commands
in openGL v1.0 (gl, glx, glu)
NeHe Tutorial
Tutorial:
CPEN 461/661
GLUT
from openGL
GLUT
toturial
JPot: openGL with Java
Programming
openGL with VisualBasic
How to write/read Latex fileSpaces / Empty Line
My openGL code to display 3-D image: compare.c
1.GLU (openGL Utility Library): basicCommand Syntax:
#include <GL/gl.h>
#include <GL/glu.h>
2.GLX (X window system):
#include <X11/Xlib.h>
#include <GL/glx.h>
3. GLUT (openGL Utility Toolkit): include all above
#include <GL/glut.h> /* one include is enough to all */
1. commands prefix gl: glClearColor(0.0, 0.0, 0.0, 0.0) /* black color to clear the window , RGB+alpha*/
constants prefix GL_: glClear(GL_COLOR_BUFFER_BIT) /* clear which buffer: currently enabled for color writing*/
2. suffix: glColor3f(1.0,1.0,1.0) /*set white color to draw*/
3: command has 3 arguments
f: type of args are floating-point (32-bit, GLfloat, GLclampf).
b (signed char, 8bit, GLbyte) s(short, GLshort) i (int/long, 32bit, GLint, GLsizei)
ub (unsigned char, 8, GLubyte, GLboolean) d(double,64, GLdouble)
ui (unsigned int/long, 32, GLenum)
v: arg is a pointer to vector(array)
GLfloat color_array[] = {1.0, 0.0, 0.0}; glColor3fv(color_array);
same as: glColor3f(1.0, 0.0, 0.0);
GLUT: for window:
int main(int argc, char** argv) {
glutInit(&argc, argv); /* Initialize GLUT. (int *argc, char **argv) */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) /*single buffering, RGBA color and a depth buffer */
/*double buffering (smoothing but slow): GLUT_DOUBLE*/
glutInitWindowSize(250, 250); /* window size in pixels*/
glutInitWindowPosition(100, 100); /* location of the upper-left corner */
glutCreateWindow("hello"); /* return: int, a unique identifier for the new window. (char *string) */
/*But not displayed yet until glutMainLoop() called */glutDisplayFunc
glutReshapeFunc()
/*glutSetKeyRepeat(int repeatMode); */ /* keyboard repeat: GLUT_KEY_REPEAT_OFF, _ON, _DEFAULT*/
glutKeyboardFunc() /*for standard ASCII keys*/
glutSpecialFunc() /* for non ASCII keys : GLUT_KEY_F1, _LEFT, _UP, _INSERT, _PAGE_UP */
glutMouseFunc() /**/
glutMotionFuncglutIdleFunc(animation); /*the animation function, glutSwapBuffers() for double buffer*/
glutIdleFunc(NULL); /*stop animation*/glutMainLoop(); /*loop forever*/
return 0;
}
glClearColor(0.0, 0.0, 0.0, 0.0); /* black color to clear the window , RGB+alpha*/Geometric Drawing Primitives:
glClearDepth(1.0); /* only if GL_DEPTH_BUFFER_BIT selected */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* clear color buffer and depth buffer */
Four kinds of buffer: Color buffer (commonly used) Depth / Accumulation / Stencil buffers
Clearing is generally a slow operation, thus different buffers defined.
glBegin(GL_POINTS); /* only draw simple polygons in openGL. */Vertex Array: (from openGL v1.1) deal with arrays of vertex in three steps
/* GL_LINES (unconnected lines), GL_LINE_STRIP (connected), GL_LINE_LOOP (looped),
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_QUADS (unconnected), GL_POLYGON ... */
glColor3f(0.0, 1.0, 0.0); /*set green color */
glVertex2f(1.0, 0.5); /* glVertex{234}{sifd}[v], a point in graph */
..... (more vertices)
glEnd(); /* only if GL_DEPTH_BUFFER_BIT selected */Point size: glPointSize(GLfloat size); /* size in pixels */
Line width: glLineWidth(GLfloat width); /* in pixel */
Stippled lines (dotted or dashed):
glLineStipple (1, 0x3F07); /* (GLint factor, GLushort pattern) */
glEnable(GL_LINE_STIPPLE); /* enable a state. glDisable(GLenum cap); glIsEnabled(GLenum cap) */Polygon mode: as points, outlines or solids
glPolygonMode(face, mode);
A polygon has two sides (face): GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
The mode can be: GL_POINT (drawn as points), GL_LINE (outlines), GL_FILL(filled)
Default: both front and back face are drawn filled
glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE); /*front face filled, back face outlined*/
glEdgeFlag(GL_FALSE); /* marking polygon boundary edges */
glPolygonStipple(const GLubtye *mask); glEnable(GL_POLYGON_STIPPLE); /*stipple pattern for filled polygon*/
glNormal3fv(n0); glVertex3fv(v0); /* normal vector at the vertex v0 of a polygon*/
/* Normal vector is a direction that's perpendicular to a surface at the vertex. */
/* Normals (directions) can be of any length, but they are normalized before lighting operation */
/* By convention, the normal is the one pointing outside of the surface, not the other opposite one */
1. Enabling arrays:
glEnableClientState(GLenum array): six available arrays,
GL_VERTEX_ARRAY /*for vertices*/
GL_COLOR_ARRAY /*for RGB colors*/
GL_INDEX_ARRAY /*for index colors */
GL_NORMAL_ARRAY /*for normal vectors of vertices in glNormal */
GL_TEXTURE_COORD_ARRAY
GL_EDGE_FLAG_ARRAY /*for boundary edges in glEdgeFlag*/
glDisableClientState(GLenum array)2. Specifying data for the arrays
glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
size: number of coordinates per vertex. =2,3,4
type: GL_FLOAT, GL_INT, ...
glColorPointer() size = 3,4
glIndexPointer() size = 1
glNormalPointer() size = 3
glTexCoordPointer() size = 1,2,3,4
glEdgeFlagPointer() size = 1, type: GLboolean
Stride: the byte offset between consecutive vertexes. sizeof(GL_INT)For example, points[] saved data as coordinates (3) + RGB colors (3):
static GLfloat points[] = (10.0, 10.0, 0.0, 1.0, 0.0, 0.0, /*first vertex: coordinates + color*/
10.0, 0.0, 10.0, 0.0, 1.0, 0.0, /*second vertex*/
.......)
glEnableClientState(GL_VERTEX_ARRAY); /*active vertex array*/
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), point); /*stride*/
glColorPointer(3, GL_FLOAT, 6*sizeof(GLfloat), &point[3]); /*starts of the array*/or, just use interleaved arrays glInterleavedArrays(GLenum format, GLsizei stride, void *pointer) to
enable parts of the 6 arrays:
glInterleaveArray(GL_C3F_V3F, point);
/*has the same effect as above example*/3. Dereferenceing and Rendering
A. Dereference a single array element
glBegin(GL_POINTS);
glArrayElement(2); /*third element in vertex array defined as above example*/
glArrayElement(5); /*sixth*/
glEnd();B. a sequence of array elements
glDrawArrays(GLenum mode, GLint first, GLsizei count);
Drawing mode: GL_POLYGONS, GL_POINTS, ...
Cannot be used inbetween glBegin/End().
Array elements from first to first+count-1 are used.C. A list of array elements
glDrawElements(GLenum mode, GLsizei count, GLenum type, void *indices);
Defines a sequence of geometric primitives using count number of elements, whose indices are stored in the array indices.
type is the data type of the indices array: GL_UNSIGNED_INT, GL_UNSIGNED_BYTE, or GL_UNSIGNED_SHORT
Example to draw a cube:
static GLint vertexes = {0, 0, 0, 1, 0, 0, 1, 0, 1, ...} /*eight vertices of the cube, with 3-D*/
static GLubyte allIndices = {4, 5, 6, 7, 1, 2, 6, 5, 0, 1, 5, 4, 0, 3, 2, 1, 0, 4, 7, 3, 2, 3, 7, 6};
/*6 QUAD surfaces with vertices indices*/
glEnableClientState(GL_VERTEX_ARRAY); /*step 1: enbale vertex array */
glVertexPointer(3, GLint, 0, vertexes); /*step 2: specify vertex data array */
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices); /*draw cube */
glMatrixMode(GLenum mode);ModelView Transformation: glMatrixMode(GL_MODELVIEW)
mode: GL_MODELVIEW /*default value. For both viewing and modeling transformation */
GL_PROJECTION
GL_TEXTUREglLoadIdentity() /*clear the current matrix C (4x4) to identity matrix I: C=I*/
/* need to be called every time a new transformation matrix generated */glLoadMatrix{fd}(const TYPE *M) /*load the 16 values of M to the current matrix : C=M*/
glMultMatrix{fd}(const TYPE *M) /*mutiple the matrix M by current matrix C and update C: C=CM*/
/*last matrix to be used in code, actually is the first matrix to perform transformation on vertex v = (x,y,z,w)T */
/* CNMv = C(N(Mv))) */
Modeling Transformation: Moving/rotating the objectglTranslate{fd}(TYPE x, y, z); /*moves the object by (x,y,z) value: C=CT*/Viewing Transformation: Moving/rotating the eye position
glRotate{fd}(TYPE angle, x, y, z); /*rotate the object around (x,y,z) by angle in a counterclockwise direction : C=CR*/
glScale{fd}(TYPE x, y, z); /*stretch factor on each direction: C=CS */gluLookAt(GLdouble eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
/*(eyex, eyey, eyez): eye position. default at 0 */
/*(centerx, y, z): looking direction, focusing point. default is negative-z axis */
/*(upx, y, z): oritation of your head, rotating your eye. default is positive-y axis */
Projection Tansformation glMatrixMode(GL_PROJECTION)
Projection defines a viewing volume:
Determing how an object projected on the screen
- perspective or orthographic projection
Defining which portions of the object are clipped
out of the image if not inside the viewing volume.
Perspective Projection: similar to what eye sees, further the object, smaller it is.
Viewport Where to draw the projected graph in the window. More than one viewport can be drawn on one window.glFrustum(GLdouble left, right, bottom, top, near, far);Orthographic Projection: parallel projection as in blueprints. Viewing volume is a rectangular box.
Two clipping planes, one is at a distance of nearto the eye, the other is at far. Both positive value.
near clipping plane's lower-left and upper-right corner: (left, bottom, -near) and (right, top, -near)gluPerspective(GLdouble fovy, aspect, near, far); /*same effect as glFrustum(), but use view angle*/
fovy: view angle in the x-z plane
aspect: width/height of the clipping plane. No image distortion if the same ratio as in glViewport().glOrtho(GLdouble left, right, bottom, top, near, far);
near clipping plane's lower-left and upper-right corner: (left, bottom, -near) and (right, top, -near)glOrtho2D(GLdouble left, right, bottom, top); /*2-D graph*/
glViewport(GLint x, y GLsizei width, height);
Draw the graph at a reactangle place with lower-left corner at (x,y) and width/height.
Default: (0, 0, winWidth, winHeight)
width/height should be the same as aspect value from gluPerspective() to avoid distortion.