Title: OpenGL
1OpenGL
2Relevant Libraries
OpenGL
C/C Code
Graphics Hardware
GLU
Win32
OpenGL Application
GLUT
C/C Libraries
- OpenGL has no windowing functions of its own
- We need to use something like the GLUT libraries
for windowing operations etc.
3Setting up to use OpenGL in MS Visual Studio
- If you have Visual Studio 6, you should already
have the OpenGL libraries installed. - You will need the following files for GLUT
- glut32.dll, glut.h, glut32.lib
- The GLUT source files and examples can be
obtained at various sites on the web but the
above (minimum required) files precompiled can be
obtained on my page at - http//isg.cs.tcd.ie/dingliaj/3d4ii/glut37p.zip
- You should extract the glut files and put them in
a directory visible to your project - You need to link the following libraries into
your project (In Project-gtSettings Link Tab
General under Object/Library Modules insert
opengl32.lib glu32.lib glut32.lib) - In your source code make sure you include glut.h
wherever you need to use OpenGL or Glut
functions/variables - For more information see http//isg.cs.tcd.ie/ding
liaj/cs5/glutsetup.html or you can email me for
specific questions
4Simple GLUT Program
int main() glutInitDisplayMode(GLUT_SINGLE
GLUT_RGB) glutCreateWindow(First GLUT
program) init() glutDisplayFunc(myDisplay)
glutMainLoop() return 0
- Set up GLUT with RGB mode and single buffer
- Create a GLUT window this requires that you
specify a display callback with glutDisplayFunc - glutMainLoop starts the GLUT event handler
transfers control of the program to GLUT
5Simple GLUT Program the display callback
void display() glClear (GL_COLOR_BUFFER_BIT)
glColor3f(1.0f, 1.0f, 1.0f) glBegin(GL_QUADS)
glVertex3d(1.0, 1.0, 0.0) glVertex3d(-1.0,
1.0, 0.0) glVertex3d(-1.0, -1.0,
0.0) glVertex3f(1.0, -1.0, 0.0) glEnd()
- This is where most of your openGL functions will
be called - In this example
- Clear the screen (or clear the frame buffer)
- Set the drawing color to white
- Draw a quad primitive with the specified vertices
6Simple GLUT program openGL initialization
void init() glClearColor(0.0f, 0.0f, 0.0f,
0.0f) glOrtho(-2, 2, -2, 2, -2, 2)
- Some OpenGL functions associated with setting up
initial states only need to be called once in
most programs. It is useful to place these in a
separate initialization function. - E.g.
- Set the clear color (the background) to black
- Tell OpenGL to use Orthographic projection with a
bounding box defined by - (xmin, xmax, ymin, ymax, zmin, zmax)(-2, 2, -2,
2, -2, 2)
7GLUT
- OpenGL designed to be windows system independent
- Windows system operations e.g. creating a
rendering window and handling window system
events are left to native window system to define - Learning native system API can be quite difficult
- GLUT is a system independent interface between
for OpenGL programming - It is designed to be simple and yet still meet
the needs of useful OpenGL programs
8GLUT design philosophy
- GLUT requires very few routines to display a
graphics scene rendered using OpenGL - Like OpenGL the GLUT API is stateful and most
initial states are predefined with defaults that
are reasonable for simple programs - The API is as much as possible windows system
independent - GLUT routines are logically organized into
several sub-APIs according to their functionality
9GLUT sub-APIs
- Initialisation
- Command line processing, window system
initialisation initial window state - Beginning Event Processing
- Enter event processing loop
- Window Management
- Overlay Management
- Menu Management
- Callback Registration
- Registers procedures which will be called by GLUT
event processing loop - Color Index ColorMap management
- State Retrieval
- Font Rendering
- Geometric Shape Rendering
10Conventions
- GLUT coordinates are expressed in pixels with
(0,0) as the upper-left - In most GLUT routines basic types (in, char) are
used as parameters. In routines where parameters
are passed directly to OpenGL OpenGL types are
used (e.g. GLfloat) - GLUT functions are prefixed glut (e.g.
glutInitDisplayMode) - Constants are prefixed by GLUT_.. (e.g. GLUT_RGB)
11Initialisation
void glutInit(int argcp, char argv) void
glutInitWindowSize(int width, int height) void
glutInitWindowPosition(int y, int y) void
glutInitDisplayMode(unsigned int mode)
- glutInit initializes the GLUT library
- Display modes
- GLUT_RGBA, GLUT_RGB, GLUT_INDEX, GLUT_SINGLE,
GLUT_DOUBLE, GLUT_ACCUM, GLUT_ALPHA, GLUT_DEPTH,
GLUT_STENCIL, GLUT_MULTISAMPLE, GLUT_STEREO
12Beginning Event Processing
While (TRUE) egetNextEvent() switch (e)
case (MOUSE_EVENT) call
registered MouseFunc break case
(RESIZE_EVENT) call registered
ReshapeFunc break
- void glutMainLoop(void)
- glutMainLoop enters the GLUT event processing
loop - Should only be called once in a GLUT program
never returns - Calls as necesarry any registered callbacks
13Window Management
int glutCreateWindow(char name) void
glutPostRedisplay(void) void glutSwapBuffers(void
) void glutSetWindow(int win) int
glutGetWindow(void)
- glutCreateWindow creates a top level window
returns window id display is actually only
called when glutMainLoop is entered - glutPostRedisplay marks current window as needing
to be redisplayed
14Call Back Registration
- void glutDisplayFunc(void (func) (void))
- void glutReshapeFunc(void (func) (int width, int
height)) - void glutKeyboardFunc(void (func) (unsigned char
key, int x, int y)) - void glutMouseFunc(void (func) (int button, int
state, int x, int y)) - void glutIdleFunc(void (func) (void))
- void glutTimerFunc(unsigned int msecs, void
(func) (int value), value)
15Geometric Object Rendering
- void glutSolidSphere(GLdouble radius, GLint
slices, GLint stacks) - void glutWireSphere(GLdouble radius, GLint
slices, GLint stacks) - void glutSolidCube(GLdouble size)
- void glutWireCube(GLdouble size)
20, 6
8, 20
20, 20
16GLUT References
- The full GLUT manual (PS/PDF) is available
locally for download/viewing at - http//isg.cs.tcd.ie/dingliaj/openGL/glut.ps
- http//isg.cs.tcd.ie/dingliaj/openGL/glut.pdf
- HTML version online
- http//reality.sgi.com/mjk_asd/spec3/spec3.html
- More GLUT info at opengl.org
- http//www.opengl.org/developers/documentation/glu
t.html