This lab is an introduction to Fundamental OpenGL Functions
After the lab lecture, you have approximately two weeks of time to:
B. Why is OpenGL used?
C. What are the basic steps to use OpenGL in a Windows Program?
F. Brief Introduction to Pixel
Format Data Structure
Pixel Format Function Description ------------------------------------------------------------------ ChoosePixelFormat() Obtains a DC's pixel format that's the closet match to a pixel format you've provided. SetPixelFormat() Sets a DC's current pixel format to the pixel format index specified. GetPixelFormat() Returns the pixel format index of a DC's current pixel format. DescribePixelFormat() Given a DC and a pixel format index, fills a PIXELFORMATDESCRIPTOR data structure with the pixel format's properties.
The main properties of pixel format include:
The following structure can be found in the Windows include file WINGDI.H
typedef struct tagPIXELFORMATDISCRIPTOR { WORD nsize; WORD nVersion; BYTE dwFlags; BYTE iPixelType; BYTE cColoerBits; BYTE cRedBits; BYTE cRedShift; BYTE cGreenBits; BYTE cGreenShift; BYTE cBlueBits; BYTE cBlueShift; BYTE cAlphaBits; BYTE cAlphaShift; BYTE cAccumBits; BYTE cAccumRedBits; BYTE cAccumBlueBits; BYTE cAccumAlphaBits; BYTE cDepthBits; BYTE cStencilBits; BYTE cAuxBuffers; BYTE iLayerType; BYTE bReserved; DWORD dwLayerMask; DWORD dwVisisbleMask; DWORD dwDamageMask; } PIXELFORMATDISCRIPTOR, *PPIXELFORMATDISCRIPTOR, FAR *LPPIXELFORMATDISCRIPTOR;
glBegin(GL_POINTS) glVertex2f(0.0f, 2.0f); //note 2D form glVertex2f(1.0f, 2.0f); glVertex2f(0.0f, -2.0f); glVertex2f(-1.0f, 0.0f); glEnd(); There are also other types of points. 3D vertex can be similarly drawn. They are listed below: glVertex2d, glVertex2f, glVertex2i, glVertex2s, glVertex3d, glVertex3f, glVertex3i, glVertex3s, glVertex4d, glVertex4f, glVertex4i, glVertex4s, glVertex2dv, glVertex2fv, glVertex2iv, glVertex2sv, glVertex3dv, glVertex3fv, glVertex3iv, glVertex3sv, glVertex4dv, glVertex4fv, glVertex4iv, glVertex4sv The postfix specifies the format of parameters used by each function: - 2 means a 2D point x, y. - 3 means a 3D point x, y, and z. - 4 means a 3D point in homogeneous coordinates x, y, z, and w. [The homoheneous coordinates will be discussed in the lecture on Chapter 5.] - d means double type. - f means float type. - i means integer type. - s means short type. - v means vector type. Example: using vector type parameter. double P1[2], P2[2], P[3]; /* set values to P1, P2, P3. */ P1[0] = 1.5; P1[1] = -0.3; ...... glBegin(GL_POINTS) glVertex2dv(P1); glVertex2dv(P2); glVertex2dv(&P3[0]); /* This is equivalent. */ glEND(); Refer to online manual for details.
Three different line primitives can be created:
draws a line segment for each pair of vertices.
draws a connected group of line segments from vertex v0 to vn connecting a line between each vertex and the next in the order given.
similar to GL_LINES_STRIP, except it closes the line from vn to v0, defining a loop. glBegin(GL_LINES_LOOPS) //make it a connected close line segment glVertex2f(0.0f, 2.0f); //note 2D form glVertex2f(1.0f, 2.0f); glVertex2f(0.0f, -2.0f); glVertex2f(-1.0f, 0.0f); glEnd();
1. Front faces, back faces and rendering modes
2. Polygon Types
You may refer to Page 59 of the reference book to view the ten OpenGL primitive types
The following code constructs a filled in parallelogram on the x-y plane:
glBegin(GL_POLYGON) glVertex2f(0.0f, 2.0f); //note 2D form glVertex2f(1.0f, 2.0f); glVertex2f(0.0f, -2.0f); glVertex2f(-1.0f, 0.0f); glEnd();There are also other types of points. 3D vertex can be similarly drawn.
Refer to online manual.
The following draws a rectangle:
glRectf(0f, 0f, 1f, 1f); // x0, y0, x1, y1: two opposite corners // of the rectangle.it is the same as:
glBegin(GL_QUADS); glVertex2f(0.0f, 0.0f); //note 2D form glVertex2f(1.0f, 0.0f); glVertex2f(1.0f, 1.0f); glVertex2f(0.0f, 1.0f); glEnd();3. Specifying a color
glcolor*( ). For example, glcolor3f() function takes three floating-point values for the red, green and blue color to select. A value of 0 means zero intensity; a value of 1.0 is full intensity, and any value in between is a partial intensity.
4. Calculating normal vectors
// pass in three points, and a vector to be filled void NormalVector(GLdouble p1[3], GLdouble p2[3], GLdouble p3[3], GLdouble n[3]) { GLdouble v1[3], v2[3], d; // calculate two vectors, using the middle point // as the common origin v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2]; v2[0] = p2[0] - p3[0]; v2[1] = p2[1] - p3[1]; v2[2] = p2[2] - p3[2]; // calculate the cross-product of the two vectors n[0] = v1[1]*v2[2] - v2[1]*v1[2]; n[1] = v1[2]*v2[0] - v2[2]*v1[0]; n[2] = v1[0]*v2[1] - v2[0]*v1[1]; // normalize the vector d = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]n[2]); n[0] /= d; n[1] /= d; n[2] /= d; } // end of NormalVector
5. Clearing the rendering window
glClearColor(0.0f, 0.0f, 0.0f); glClearDepth(1.0f); // once the clear color and clear depth values have been // set, both buffers can be cleared, the following command // is usually issued just before you begin to render a // scene, usually as the first rendering step to a // WM_PAINT message // clear both buffers in the mean time glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
H. Assignment
|y | | | | x_______| / / / / z
glPushMatrix( ), glPopMatrix, gluQuadricDrawstyle( ), glTranslated( ), glRotated( ), gluDisk( ), gluCylinder( ), gluDisk( ), auxSolidSphere( )