- Forward


OpenGL Basics
with Examples


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Introduction
Back SMYC Forward
  • What is OpenGL?
    • An API for describing 3D points, lines and polygons
    • An API for performing 3D transformations
    • An API for 3D viewing and hidden object removal
    • An API for 3D lighting, shading, rasterization
  • What isn't OpenGL?
    • A windowing system [though the Graphics Library Utility Toolkit (GLUT) does provide some of this functionality]
    • A GUI toolkit [though GLUI does provide some of this functionality]
    • A 3D modeling system [though the OpenGL Utility Library (GLU) does provide some of this functionality]
OpenGL Concepts
Back SMYC Forward
  • Some Data Types:
    • GLbyte, GLshort, GLint
    • GLfloat, GLdouble
  • States:
    • Many function calls change the state of OpenGL (e.g., the current color, the current projection)
    • A "capability" can be enabled/disabled with the void glEnable(GLenum) and void glDisable(GLenum) functions
    • The state of a "capability" can be checked with the GLboolean glIsEnabled(GLenum)
    • Other state information can be retrieved with void glGetBooleanv(GLenum, GLboolean*), void glGetIntegerv(GLenum, GLint*), void glGetFloatv(GLenum, GLfloat*), and void glGetDoublev(GLenum, GLdouble*)
Clearing Buffers
Back SMYC Forward
  • Setting the "Clear" Color:
    • glClearColor(r, g, b, alpha)
    • The parameters must be in the interval [0, 1]
  • Setting the "Clear" Depth:
    • glClearDepth(depth)
  • Clearing:
    • glClear(bitflags)
    • The parameter is created using a bitwise OR of constants like GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT
Vertices
Back SMYC Forward
  • Types:
    • 2D, 3D, and 4D
    • short, int, float, double
  • Functions:
    • void glVertex2s(GLshort, GLshort), void glVertex3s(GLshort, GLshort, GLshort), void glVertex4s(GLshort, GLshort, GLshort, GLshort)
    • void glVertex2i(GLint, GLint), void glVertex3i(GLint, GLint, GLint), void glVertex4i(GLint, GLint, GLint, GLint)
    • void glVertex2f(GLfloat, GLfloat), void glVertex3f(GLfloat, GLfloat, GLfloat), void glVertex4f(GLfloat, GLfloat, GLfloat, GLfloat)
    • void glVertex2d(GLdouble, GLdouble), void glVertex3d(GLdouble, GLdouble, GLdouble), void glVertex4d(GLdouble, GLdouble, GLdouble, GLdouble)
  • Notation:
    • It is convenient to refer to a non-specific element as glVertex*()
Properties of Vertices
Back SMYC Forward
  • Color:
    • glColor*()
    • glIndex*()
    • glSecondaryColor*()
  • Normal:
    • glNormal*()
  • Material Properties:
    • glMaterial*()
Points, Lines and Polygons
Back SMYC Forward
  • Some Constants:
    • GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_POLYGON
  • Beginning a List:
    • void glBegin(GLenum)
  • Ending a List:
    • void glEnd()
Points, Lines and Polygons (cont.)
Back SMYC Forward
  • A Simple Example:
    • glBegin(GL_POLYGON); glVertex2i(0, 0); glVertex2i(1, 0); glVertex2i(1, 1); glEnd();
  • An Example with Colors and Normals:
    • glBegin(GL_POLYGON); glColor3f(1.0, 0.0, 0.0); glNormal3i(0, 0, 1); glVertex3i(0, 0, 0); glColor3f(0.0, 1.0, 0.0); glNormal3i(0, 0, 1); glVertex3i(1, 0, 0); glColor3f(0.0, 0.0, 1.0); glNormal3i(0, 0, 1); glVertex3i(1, 1, 0); glEnd();
Controlling the Rendering Process
Back SMYC Forward
  • Point Details:
    • void glPointSize(GLfloat)
  • Line Details:
    • void glLineWidth(GLfloat)
  • Polygon Details:
    • void glPolygonMode(GLenum face, GLenum mode)
    • face is either GL_FRONT or GL_BACK
    • mode is either GL_FILL or GL_LINE
Vertex Arrays
Back SMYC Forward
  1. Enable Arrays
  2. Specify the Data
  3. Dereference
Data for an Example
openglexamples/basics/vertex-arrays.c (Fragment: data)
 
Vertex Arrays (cont.)
Back SMYC Forward
  • Enabling:
    • Call void glEnableClientState(GLenum) passing GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_NORMAL_ARRAY or another acceptable value
  • Example:
openglexamples/basics/vertex-arrays.c (Fragment: enableArrays)
 
Vertex Arrays (cont.)
Back SMYC Forward
  • Specifiying Data:
    • void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer)
    • void glColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer)
    • void glNormalPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer)
  • Example:
openglexamples/basics/vertex-arrays.c (Fragment: specifyData)
 
Vertex Arrays (cont.)
Back SMYC Forward
  • Dereferencing:
    • Call void glArrayElement(GLint) or void glDrawElements(GLenum mode, GLsizei count, GLenum type, void* indices)
  • Example:
openglexamples/basics/vertex-arrays.c (Fragment: dereferenceArrayElements)
 
Vertex Arrays (cont.)
Back SMYC Forward

void glDrawElements(GLenum mode, GLsizei count, GLenum type, void* indices)

openglexamples/basics/vertex-arrays.c (Fragment: dereferenceDrawElements)
 
Viewing
Back SMYC Forward
  1. ModelView Matrix:
    • From object coordinates to eye coordinates
  2. Projection Matrix
    • From eye coordinates to clip coordinates
  3. Perspective Division:
    • From clip coordinates to normalized device coordinates (NDC)
  4. Viewport Transformation
    • From NDC to window coordinates
Viewing (cont.)
Back SMYC Forward
  • void glMatrixMode(GLenum mode)
    • Specifies which mode is current
    • mode can be GL_MODELVIEW, GL_PROJECTION, or GL_TEXTURE
  • void glLoadIdentity()
    • Sets the current matrix to the identity matrix
  • void glLoadMatrix*(const type* m)
    • Sets the current matrix to the matrix m
ModelView Matrix
Back SMYC Forward
  • The Viewing Transform:
    • Positions the viewing volume in the world
    • Usually set using gluLookAt()
  • The Modeling Transform
    • "Positions" the model in the world
    • Usually set using glTranslate*(), glScale*(), glRotate*()
The Projection
Back SMYC Forward
  • Orthographic:
    • glOrtho(left, right, bottom, top, near, far)
  • Perspective:
    • One Way: glFrustum(left, right, bottom, top, near, far)
    • Another Way:gluPerspective(fovy, aspect, near, far)
    • where fovy is the field of view (in degrees) in the \(y\) direction, aspect is the aspect ration that determines the FOV in the \(x\) direction, near is the positive distance from the viewer to the near clipping plane, and far is the positive distance from the viewer to the far clipping plane
Viewing (cont.)
Back SMYC Forward

An Example

openglexamples/basics/projection.c
 
There's Always More to Learn
Back -