2014 OpenGLGlut
2014 OpenGLGlut
Computer Graphics
OpenGL and GLUT Programming
OpenGL:Beginning
Beginning
OpenGL:
OpenGL reference books:
1. The RED BOOK: Mason Woo, Neider, Jackie, Tom Davis,
Dave Shreiner, OpenGL Programming Guide, 8th Edition, The
Official Guide to Learning OpenGL, Version 4.3 2013.
2. The BLUE BOOK: OpenGL ARB, OpenGL Reference Manual,
4th Edition, The Official Reference Document to OpenGL,
Version 1.4, 2004.
3. Richard S. Wright, et. al. OpenGL SuperBible, 6th Edition, 2014.
Companion website: http://www.starstonesoftware.com/OpenGL/
(OpenGL 4.3)
4. Randi J, Rost, et. al. OpenGL Shading Language, 3rd Edition by AddisonWesley Professional, 2009. (OpenGL 3.1)
Tool: GLview
OpenGL Extensions Viewer: to check which version your
hardware supports
The vendor name
The OpenGL version
The renderer name
The extensions of the current OpenGL 3D accelerator
http://www.realtech-vr.com/glview/version4.html
Now supporting OpenGL 3.0 to 4.1 with rendering tests.
GLView
How to setup?
Setup OpenGL&GLUT in VC6.0
Setup OpenGL&GLUT in VS2008
Setup OpenGL&GLUT in VS2010 Express
v6.0A\Lib\GL\glut32.lib
(VC2008)
to <drive>: \Program Files>\Microsoft SDKs\ Windows\
v7.0A\Lib\GL\glut32.lib
(VC2010)
7
Compilation on Windows
Visual C++
Create a Win32 console application
Add path to find include files if needed (GL/glut.h)
Add opengl32.lib, glu32.lib, glut32.lib to project
settings (for library linking VC6)
glut32.dll is used during the program execution.
(Other DLL files are included in the device driver
of the graphics accelerator.)
//OpenGL initialization
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void main ()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow(Simple OpenGL );
myInit();
glutDisplayFunc(display);
glutMainLoop();
}
display callback
10
11
Program structure
Most OpenGL programs have a similar structure
that consists of the following functions
main():
opens one or more windows with the required properties
defines the callback functions
enters event loop (last executable statement)
callbacks
display function
input and window functions
12
14
More detail
buffers.
15
16
17
glutInitWindowSize(400, 300);
glutInitWindowPosition(50, 100);
Creating a Window
glutCreateWindow(xxxx);
glutCreateWindow() create the graphics window. It takes a
character string as a parameter which may appear in the title
bar.
Note: It only sends a request of the windows creation to the system.
The window is not really to be displayed until glutMainLoop is
18
entered.
19
Event processing
GLUT is designed as an event-driven engine
Each one of the possible events must be registered
in one of the GLUT state variables, by callback
registration routines, such as glutMouseFunc;
glutDisplayFunc;
Callback Registration Routine Syntax
g l u t [SomeEvent] F u n c
e.g.: mouse clicking - glutMouseFunc
a routine written by user - myMouse()
To register the callback function by
glutMouseFunc( myMouse );
20
21
22
Types of event
System events
Display event
Reshape event
Idle event and timer event
User input events
Mouse click, motion of the mouse, keyboard hits
23
24
glutSwapBuffers();
}
26
glutTimerFunc()
Three arguments:
the sleep time as an integer in milliseconds
the users callback function
an integer identifier, which is passed into the
callback function
If multiple timer callbacks requested
27
28
29
30
32
33
code
35
Using global
The form of all GLUT callbacks is fixed
void mymouse(GLint button, GLint state,
GLint x, GLint y)
void mydisplay()
It must use globals to pass information to callbacks
float t; /*global */
void mydisplay()
{
/* draw something that depends on t */
}
36
37
Display callback
The most important callback
glutDisplayFunc(xxxx);
Put all your routines that you need to draw a scene in
the display callback function.
Display callback It is called whenever it is necessary to
redraw the image, such as:
The initial creation of the window;
Whenever the window is uncovered by the removal of some
overlapping window;
Whenever the program explicitly requests the redrawing
(through the use of glutPostRedisplay() function, as in the case
of an animation, where this would happen continuously).38
Drawing commands
glBegin(mode)
glEnd()
Between these two commands a list of vertices is
given, which defines the object.
The sort of object to be defined is determined by
the mode argument.
40
41
1. glVertex2i(2, 4)
2. glVertex3ub(3, 4, 5)
42
Drawing
commands
All valid Commands
between glBegin()
and glEnd()
(refer to Red book)
NO others!
43
45
Reshape callback
The reshape callback would contain the following
call which resizes the viewport, whenever the
window is resized.
46
Projection
Map the drawing region to the window
In this simple example: the drawing area is a unit
square over the interval [0, 1] with the origin in
the lower left corner
Firstly, inform OpenGL where the idealized
drawing area is. Then OpenGL can map it to the
viewport by a transformation matrix: projection
matrix.
gluOrtho2D(left, right, bottom, top)
Any drawing outside of this region will automatically be
clipped away by OpenGL.
47
2D projection
The first command tells OpenGL to modify the projection
transformation.
Then, initialize the current matrix to the identity
48
The sample
program
49