OpenGL Basics
with Examples |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
GLbyte
, GLshort
, GLint
GLfloat
, GLdouble
void glEnable(GLenum)
and
void glDisable(GLenum)
functions
GLboolean glIsEnabled(GLenum)
void glGetBooleanv(GLenum, GLboolean*)
,
void glGetIntegerv(GLenum, GLint*)
,
void glGetFloatv(GLenum, GLfloat*)
, and
void glGetDoublev(GLenum, GLdouble*)
glClearColor(r, g, b, alpha)
glClearDepth(depth)
glClear(bitflags)
GL_COLOR_BUFFER_BIT
and
GL_DEPTH_BUFFER_BIT
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)
glVertex*()
glColor*()
glIndex*()
glSecondaryColor*()
glNormal*()
glMaterial*()
GL_POINTS
, GL_LINES
,
GL_TRIANGLES
, GL_QUADS
,
GL_POLYGON
void glBegin(GLenum)
void glEnd()
glBegin(GL_POLYGON); glVertex2i(0, 0); glVertex2i(1, 0); glVertex2i(1, 1); glEnd();
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();
void glPointSize(GLfloat)
void glLineWidth(GLfloat)
void glPolygonMode(GLenum face, GLenum mode)
face
is either GL_FRONT
or GL_BACK
mode
is either GL_FILL
or GL_LINE
/** * Data */ static GLfloat vertices[] = { -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0 }; static GLfloat colors[] = { 0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.5, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 1.0, 0.5, 1.0, 0.0, 1.0, 0.5, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0, 1.0, 0.5 };
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)
// Each color has 4 elements glColorPointer(4, GL_FLOAT, 0, colors); // Each vertex has 3 elements glVertexPointer(3, GL_FLOAT, 0, vertices);
void glArrayElement(GLint)
or
void glDrawElements(GLenum mode, GLsizei count, GLenum type, void* indices)
// Each call to glArrayElement() has the same effect // as a call to glVertex3fv() and a call to glColor4fv() glBegin(GL_QUADS); // front glArrayElement(4); glArrayElement(5); glArrayElement(6); glArrayElement(7); glEnd();
void glDrawElements(GLenum mode, GLsizei count, GLenum type, void* indices)
static GLubyte front[] = {4, 5, 6, 7}; static GLubyte right[] = {1, 2, 6, 5}; static GLubyte bottom[] = {0, 1, 5, 4}; static GLubyte back[] = {0, 3, 2, 1}; static GLubyte left[] = {0, 4, 7, 3}; static GLubyte top[] = {2, 3, 7, 6}; glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, right); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottom); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, back); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, left); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, top);
void glMatrixMode(GLenum mode)
mode
can be GL_MODELVIEW
,
GL_PROJECTION
, or GL_TEXTURE
void glLoadIdentity()
void glLoadMatrix*(const type* m)
m
gluLookAt()
glTranslate*()
,
glScale*()
, glRotate*()
glOrtho(left, right, bottom, top, near, far)
glFrustum(left, right, bottom, top, near, far)
gluPerspective(fovy, aspect, near, far)
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
#include <GL/glut.h> /** * \file * A simple OpenGL application demonstrates the use * of projections * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ int window; /** * Create a window. * * @param title The title * @param width The width (in pixels) * @param height The height (in pixels) * @param x The x position * @param y The y position * @return The window id */ int createWindow(const char* title, int width, int height, int x, int y) { int id; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(x, y); id = glutCreateWindow(title); return id; } /** * Perform OpenGL initializations. */ void init() { glClearColor(1.0, 1.0, 1.0, 1.0); // Setup the projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glOrtho(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); // Orthographic gluPerspective(65.0, 1.0, 1.0, 20.0); // Perspective } /** * Display/render the OpenGL window. */ void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 0.0); // Transform glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, // Camera at (0,0,5) 0.0, 0.0, 0.0, // Aim camera towards (0,0,0) 0.0, 1.0, 0.0); // Up is (0,1,0) // Create a cube glutWireCube(1.0); glFlush(); // Some other useful geometric objects in GLUT: // // glutSolidSphere(), glutWireSphere() // glutSolidCone(), glutWireCone() // glutSolidTirus(), glutWireTorus() // glutSolidTeapot(), glutWireTeapot() // // as well as Dodecahedron, Octahedron, Tetrahedron, and Icosahedron } /** * The entry point of the application. * * This function contains calls to GLUT. * * @param argc The number of command line arguments * @param argv The array of command line arguments * @return A status code */ int main(int argc, char** argv) { glutInit(&argc, argv); window = createWindow("Projection", 640, 480, 0, 0); glutDisplayFunc(display); init(); glutMainLoop(); }