OpenGL (1) (2)
OpenGL (1) (2)
OpenGL Programming
What is OpenGL
OpenGL is a software API to graphics hardware.
designed as a streamlined, hardware-independent
interface to be implemented on many different hardware
platforms
Intuitive, procedural interface with c binding
No windowing commands !
No high-level commands for describing models of three-
dimensional objects
The OpenGL Utility Library (GLU) provides many of the
modeling features, such as quadric surfaces and NURBS
curves and surfaces
SGI and GL
Silicon Graphics (SGI) revolutionized the
graphics workstation by implementing the
pipeline in hardware (1982)
To access the system, application
…
Windowing with OpenGL
OpenGL is independent of any specific
window system
OpenGL can be used with different window
systems
X windows (GLX)
MFC
…
GLUTprovide a portable API for creating
window and interacting with I/O devices
GLUT
OpenGL Utility Toolkit (GLUT)
Provides functionality common to all window systems
Open a window
Get input from mouse and keyboard
Menus
Event-driven
Code is portable but GLUT lacks the functionality of a
good toolkit for a specific platform
No slide bars, buttons, …
Software Organization
application program
OpenGL Motif
widget or similar GLUT
GLX, AGL
or WGL GLU
Texture
Memory
Pixel
Operations
OpenGL as a state machine
GL State Variables- can be set and queried by OpenGL. Remains
unchanged until the next change.
Projection and viewing matrix
Color and material properties
Lights and shading
Line and polygon drawing modes
…
OpenGL functions are of two types
Primitive generating
Can cause output if primitive is visible
How vertices are processed and appearance of primitive are controlled by the
state
State changing
Transformation functions
Attribute functions
OpenGL Syntax
Functions have prefix gl and initial capital letters for each word
glClearColor(), glEnable(), glPushMatrix() …
glu for GLU functions
gluLookAt(), gluPerspective() …
Constants begin with GL_, use all capital letters
GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW …
Extra letters in some commands indicate the number and type of
variables
glColor3f(), glVertex3f() …
OpenGL data types
GLfloat, GLdouble, GLint, GLenum, …
Underlying storage mode is the same
Easy to create overloaded functions in C++ but issue is efficiency
OpenGL function format
function name
dimensions
glVertex3f(x,y,z)
glVertex3fv(p)
p is a pointer to an array
OpenGL #defines
Most
constants are defined in the include files
gl.h, glu.h and glut.h
Note #include <GL/glut.h> should
automatically include the others
Examples
glBegin(GL_POLYGON)
glClear(GL_COLOR_BUFFER_BIT)
include
files also define OpenGL data types:
GLfloat, GLdouble,….
GLUT
Developed by Mark Kilgard
Hides the complexities of differing window
system APIs
Default user interface for class projects
Glut routines have prefix glut
glutCreateWindow() …
Has very limited GUI interface
Glui is the C++ extension of glut
Glut Routines
Initialization: glutInit() processes (and removes) commandline
arguments that may be of interest to glut and the window system and
does general initialization of Glut and OpenGL
Must be called before any other glut routines
Display Mode: The next procedure, glutInitDisplayMode(),
performs initializations informing OpenGL how to set up the frame
buffer.
Display Mode Meaning
GLUT_RGB Use RGB colors
GLUT_RGBA Use RGB plus alpha (for transparency)
GLUT_INDEX Use indexed colors (not recommended)
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
viewing volume
Event Handling
Virtually all interactive graphics programs are even
driven
GLUT uses callbacks to handle events
Windows system invokes a particular procedure when an
event of particular type occurs.
MOST IMPORTANT: display event
Signaled when window first displays and whenever portions
of the window reveals from blocking window
glutDisplayFunc(void (*func)(void)) registers
the display callback function
Running the program: glutMainLoop()
Main event loop. Never exit()
More Callbacks
glutReshapeFunc(void (*func)(int w, int h)) indicates
what action should be taken when the window is resized.
Repeat
Example
Five subdivisions
Gasket Program
#include <GL/glut.h>
/* initial triangle */
GLfloat v[3][2]={{-1.0, -0.58},
{1.0, -0.58}, {0.0, 1.15}};
int n; /* number of recursive steps */
void triangle( GLfloat *a, GLfloat *b, GLfloat *c)
/* display one triangle */
{
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}
Triangle Subdivision
void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int
m)
{
/* triangle subdivision using vertex numbers */
point2 v0, v1, v2;
int j;
if(m>0)
{
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
divide_triangle(a, v0, v1, m-1);
divide_triangle(c, v1, v2, m-1);
divide_triangle(b, v2, v0, m-1);
}
else(triangle(a,b,c));
/* draw triangle at end of recursion */
}
Gasket Display Functions
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
divide_triangle(v[0], v[1], v[2], n);
glEnd();
glFlush();
}