OpenGL and GLUT Basics
Outline
• OpenGL & GLUT basics
– Grpahics Pipeline
– 2-D drawing
– User interaction
2
Graphics Pipeline
3
OpenGL – What is It?
• GL (Graphics Library): Library of 2-D, 3-D
drawing primitives and operations
– API for 3-D hardware acceleration
• GLU (GL Utilities): Miscellaneous
functions dealing with camera set-up and
higher-level shape descriptions
• GLUT (GL Utility Toolkit): Window-system
independent toolkit with numerous utility
functions, mostly dealing with user
interface
4
Event-driven GLUT program structure
1. Configure and open window
2. Initialize OpenGL state, program variables
3. Register callback functions
• Display (where rendering occurs)
• Resize
• User input: keyboard, mouse clicks,
motion, etc.
4. Enter event processing loop
5
Simple OpenGL program
#include <stdio.h>
#include <GL/glut.h>
void main(int argc, char** argv)
{
glutInit(&argc, argv); // configure and open window
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(100, 100);
glutCreateWindow(“hello”);
init(); // set OpenGL states, variables
glutDisplayFunc(display); // register callback
routines
glutKeyboardFunc(keyboard);
glutMainLoop(); // enter event-driven loop
}
6
Configure and open window
• glutInit: Pass command-line flags on to GLUT
• glutInitDisplayMode: OR together bit masks to
set modes on pixel type (indexed vs. true
color), buffering, etc.
• glutInitWindowSize, glutCreateWindow: Set
drawing window attributes, then make it
7
Initialize OpenGL state
• init(): Set OpenGL state, program variables
– Use GL types/typedefs GLfloat, GLint, GL_TRUE,
GL_FALSE, etc. for
cross-platform compatibility
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, right, 0, top);
}
sets “units” of subsequent draw commands
8
OpenGL screen coordinates
• Bottom left corner is origin
• gluOrtho2D() sets the units of the screen
coordinate system
•gluOrtho2D(0, w, 0, h) means the
coordinates are in units of pixels
•gluOrtho2D(0, 1, 0, 1) means the
coordinates are in units of “fractions of window
size” (regardless of actual window size)
9
Example: Specifying the center of a
square
10
Example: Specifying the center of a
square
11
A complete OpenGL program
#include <stdio.h>
#include<windows.h>
#include <GL/glut.h>
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 150);
glutCreateWindow ("my first attempt");
glutDisplayFunc(myDisplay);
myInit ();
glutMainLoop();
}
12
A complete OpenGL program (cont.)
void myDisplay(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd();
glFlush ();
}
13
A complete OpenGL program (cont.)
void myInit (void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
14
Rendering Steps
In function registered with glutDisplayFunc():
1. Clear window
• glClear(GL_COLOR_BUFFER_BIT);
2. Draw shapes
• Set colors, patterns, point/line sizes
• Specify type of geometric primitive(s) and list
vertices
3. Swap buffers if display mode is GLUT_DOUBLE
4. Force all operations to complete with glFlush()
15
Single- vs. double-buffering
• Single-buffering: Draw directly to screen
buffer
• Double-buffering: Draw to offscreen
buffer, then make that the screen buffer
when done
• For animation, double-buffering is better
because it eliminates flickering
16
OpenGL Geometric Primitives
17
Specifying Geometric Primitives
Primitives are specified using
glBegin(primType);
...
glEnd();
– primType determines how vertices are combined
GLfloat red, green, blue;
GLfloat x, y;
glBegin(primType);
for (i = 0; i < nVerts; i++) {
glColor3f(red, green, blue);
glVertex2f(x, y);
... // change coord. values
}
glEnd();
18
OpenGL Command Formats
19