34. OpenGL
Programming 2
Dated: 04-07-2025
#include "GL/glut.h"
#include <stdlib.h>
static void reshape(int w, int h) {
glViewport(0, 0, w, h);
}
static void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 27: // Escape
exit(0);
break;
}
}
static void display() {
}
static void idle() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glRotatef(0.5f, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.5f, 0.2f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
static void initGL() {
float ratio = (float)640 / 480;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, ratio, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.0, 0.0, -5.0);
}
int main(int argc, char* argv[]) {
// Initialize the GLUT library.
glutInit(&argc, argv);
// Set up the OpenGL rendering context.
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
// Create a window and set its width and height.
glutCreateWindow("Deform");
glutReshapeWindow(640, 480);
// Initialize our data structures.
initGL();
// The keyboard function gets called whenever we hit a key.
glutKeyboardFunc(keyboard);
// The display function gets called whenever the window
// requires an update or when we explicitly call
// glutPostRedisplay()
glutDisplayFunc(display);
// The reshape function gets called whenever the window changes
// shape.
glutReshapeFunc(reshape);
// The idle function gets called when there are no window
// events to process.
glutIdleFunc(idle);
// Get the ball rolling!
glutMainLoop();
return 0;
}
Above program's output
void glMatrixMode(GLenum mode);
Specified which matrix
1 is the current one.
Value | Meaning |
---|---|
GL_MODELVIEW |
Applies subsequent matrix 1 operations to the modelview matrix stack. |
GL_PROJECTION |
Applies subsequent matrix 1 operations to the projection matrix stack. |
GL_TEXTURE |
Applies subsequent matrix 1 operations to the texture matrix 1 stack. |
Error code | Condition |
---|---|
GL_INVALID_ENUM |
mode was not an accepted value. |
GL_INVALID_OPERATION |
glMatrixMode was called between a call to glBegin and the corresponding call to glEnd . |
void glLoadIdentity(void);
Replaces current matrix
1 with identity matrix
1
Error code | Condition |
---|---|
GL_INVALID_OPERATION |
glLoadIdentity was called between a call to glBegin and the corresponding call to glEnd . |
void glTranslated(GLdouble x, GLdouble y, GLdouble z);
Multiplies the current matrix
1 with a translation matrix
.1
void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
Multiplies the current matrix
1 with a translation matrix
.1
Use glPushMatrix
and glPopMatrix
to save and restore the untranslated coordinate system
.2
Error code | Condition |
---|---|
GL_INVALID_OPERATION |
glTranslate was called between a call to glBegin and glEnd . |
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
Term | Description |
---|---|
fovy |
The field of view angle, in degrees, in the y-direction. |
aspect |
The aspect ratio that determines the field of view in the x-direction. The aspect ratio is the ratio of \(x\) (width) to \(y\) (height). |
zNear |
The distance from the viewer to the near clipping plane (always positive). |
zFar |
The distance from the viewer to the far clipping plane (always positive). |
void glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
Term | Description |
---|---|
angle |
The angle of rotation, in degrees. |
x , y , z |
The x, y, and z coordinates of a vector ,3 respectively. |
These functions
compute a matrix
1 that performs counter clock wise rotation of angle
degrees about the vector
3 from origin
to \(\langle x, y ,z\rangle\).
Use glPushMatrix
and glPopMatrix
to save and restore the unrotated coordinate system
.2
Error code | Condition |
---|---|
GL_INVALID_OPERATION |
glRotate was called between a call to glBegin and glEnd . |
void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
Specifies the red, green, blue, and alpha values used by glClear
to clear the color buffers
, clamped to range \([0, 1]\).
Error code | Condition |
---|---|
GL_INVALID_OPERATION |
glClearColor was called between a call to glBegin and the corresponding call to glEnd . |
Following functions
set the current color
.
glColor3b
glColor3d
glColor3f
glColor3i
glColor3s
glColor3ub
glColor3ui
glColor3us
glColor4b
glColor4d
glColor4f
glColor4i
glColor4s
glColor4ub
glColor4ui
glColor4us
glColor3bv
glColor3dv
glColor3fv
glColor3iv
glColor3sv
glColor3ubv
glColor3uiv
glColor3usv
glColor4bv
glColor4dv
glColor4fv
glColor4iv
glColor4sv
glColor4ubv
glColor4uiv
glColor4usv
In particular, you can call glColor
between a call to glBegin
and the corresponding call to glEnd
.
void glClear(GLbitfield mask);
Clears buffers
to preset values.
Mask | Buffer to be Cleared |
---|---|
GL_COLOR_BUFFER_BIT |
The buffers currently enabled for color writing. |
GL_DEPTH_BUFFER_BIT |
The depth buffer. |
GL_ACCUM_BUFFER_BIT |
The accumulation buffer. |
GL_STENCIL_BUFFER_BIT |
The stencil buffer. |
The glClear
function
sets the bitplane area of the window
to values previously selected by
glClearColor
glClearIndex
glClearDepth
glClearStencil
glClearAccum
You can clear multiple color
buffers simultaneously by selecting more than one buffer
at a time using glDrawBuffer
.
Error code | Condition |
---|---|
GL_INVALID_VALUE |
Any bit other than the four defined bits was set in mask . |
GL_INVALID_OPERATION |
glClear was called between a call to glBegin and glEnd . |
The glBegin
and glEnd
functions
delimit the vertices
of a primitive or a group of like primitives.
void glBegin(GLenum mode);
void glEnd(void);
Primitive Type | Description |
---|---|
GL_POINTS |
Treats each vertex as a single point. Vertex \(n\) defines point \(n\). \(N\) points are drawn. |
GL_LINES |
Each pair of vertices defines a line. Vertices \(2n+1\) and \(2n\) define line \(n\). \(N/2\) lines are drawn. |
GL_LINE_STRIP |
Connects vertices into a line strip. Vertices \(n\) and \(n+1\) define line \(n\). \(N-1\) lines are drawn. |
GL_LINE_LOOP |
Like GL_LINE_STRIP , but connects last vertex back to the first. Vertices \(n\) and \(n+1\), and then \(N\) and \(1\). \(N\) lines are drawn. |
GL_TRIANGLES |
Each group of 3 vertices forms a triangle. Vertices \(3n-2\), \(3n-1\), and \(3n\) define triangle \(n\). \(N/3\) triangles are drawn. |
GL_TRIANGLE_STRIP |
Connected group of triangles. For odd \(n\), vertices \(n\), \(n+1\), \(n+2\); for even \(n\), \(n+1\), \(n\), \(n+2\) define triangle \(n\). \(N-2\) triangles are drawn. |
GL_TRIANGLE_FAN |
Connected group of triangles sharing the first vertex. Vertices \(1\), \(n+1\), and \(n+2\) define triangle \(n\). \(N-2\) triangles are drawn. |
GL_QUADS |
Each group of 4 vertices defines a quad. Vertices \(4n-3\), \(4n-2\), \(4n-1\), and \(4n\) define quadrilateral \(n\). \(N/4\) quads are drawn. |
GL_QUAD_STRIP |
Connected group of quads. For each pair after the first, vertices \(2n-1\), \(2n\), \(2n+2\), and \(2n+1\) define quad \(n\). \(N\) quads are drawn. |
GL_POLYGON |
Draws a single convex polygon using all vertices \(1\) through \(N\). |
You can use only a subset of OpenGL
functions between glBegin
and glEnd
. The functions
you can use are:
glVertex
glColor
glIndex
gINormal
glMaterial
Minimum number of vertices | Type of primitive |
---|---|
1 | Point |
2 | Line |
3 | Triangle |
4 | Quadrilateral |
3 | Polygon |
Modes that require a certain multiple of vertices
are
GL_LINES
GL_TRIANGLES
GL_QUADS
GL_QUAD_STRIP
Error code | Condition |
---|---|
GL_INVALID_ENUM |
mode was set to an unaccepted value. |
GL_INVALID_OPERATION |
A function other than glVertex , glColor , glIndex , glNormal , glTexCoord , glEvalCoord , glEvalPoint , glMaterial , glEdgeFlag , glCallList , or glCallLists was called between glBegin and the corresponding glEnd . The function glEnd was called before the corresponding glBegin was called, or glBegin was called within a glBegin /glEnd sequence. |
These functions specify a vertex
.
glVertex2d
glVertex2f
glVertex2i
glVertex2s
glVertex3d
glVertex3f
glVertex3i
glVertex3s
glVertex4d
glVertex4f
glVertex4i
glVertex4s
glVertex2dv
glVertex2fv
glVertex2iv
glVertex2sv
glVertex3dv
glVertex3fv
glVertex3iv
glVertex3sv
glVertex4dv
glVertex4fv
glVertex4iv
glVertex4sv