OpenGL and GLUT - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

OpenGL and GLUT

Description:

... (with respect to the vertex) which appears to be in reverse: ... You should extract the glut files and put them in a directory visible to your project ... – PowerPoint PPT presentation

Number of Views:324
Avg rating:3.0/5.0
Slides: 44
Provided by: johndin
Category:

less

Transcript and Presenter's Notes

Title: OpenGL and GLUT


1
OpenGL and GLUT
2
OpenGL
  • a software interface to graphics hardware
  • a library for 3d graphics and modelling
  • portable and fast
  • intended for use with computer hardware that is
    designed and optimized for 3d graphics
  • NT ships with a generic software based version

3
History
  • OpenGL is relatively new (1992)
  • forerunner GL from Silicon Graphics
  • IrisGL - a 3D API for high-end IRIS graphics
    workstations
  • OpenGL attempts to be more portable
  • OpenGL Architecture Review Board (ARB) decides on
    all enhancements

4
Overview
  • OpenGL is a procedural graphics language
  • programmer describes the steps involved to
    achieve a certain display
  • steps involve C style function calls to a
    highly portable API
  • fairly direct control over fundamental operations
    of two and three dimensional graphics

5
The OpenGL Standard
  • an API not a language
  • other graphics packages GKS (ISO ANSI approved
    standard), PHIGS (standard), PEX, GL / IrisGL,
    Renderman (API Language), PostScript, BGI
    (Borland Graphics Interface), Renderware

6
What can it do ?
  • Display primitives
  • Coordinate transformations (transformation matrix
    manipulation)
  • Lighting calculations
  • Antialiasing
  • Pixel Update Operations
  • Display-List Mode

7
OpenGL Pipeline
Transform Geometry
Clip to View Volume
Project to Viewport
Rasterise
vertices
pixels
perform per-vertex rotations translations and
scaling to achieve final geometry, then transform
to the camera co-ordinate system
project vertices onto the 2D plane
representing the viewport/screen
convert all polygons, line and point to pixel
values
eliminate vertices that will not be visible in
the final image
8
OpenGL Primitives
  • All geometric objects in OpenGL are created from
    a set of basic primitives.
  • Certain primitives are provided to allow
    optimi-sation of geometry for improved rendering
    speed.
  • Line based primitives

GL_LINE_STRIP
GL_LINE_LOOP
GL_LINES
GL_POINTS
9
OpenGL Primitives
  • Polygon primitives

v4
v4
v4
GL_POLYGON
GL_QUADS
GL_TRIANGLES
v4
v3
v8
v2
v4
v5
v6
v3
v2
v5
v2
v1
v4
v1
v6
v3
v5
v7
v1
GL_QUAD_STRIP
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
10
Scale
  • all vectors are scaled from the origin

Original
scale all axes
scale Y axis
offset from origin
distance from origin also scales
11
Scale
We would also like to scale points thus we need
a homogeneous transformation for consistency
12
Rotation
  • Rotations are anti-clockwise about the origin

rotation of 45o about the Z axis
offset from origin rotation
13
Rotation
14
Rotation
  • 2D rotation of q about origin
  • 3D homogeneous rotations
  • Note
  • If M -1 MT then M is orthonormal. All
    orthonormal matrices are rotations about the
    origin.

15
Translation
  • Translation only applies to points, we never
    translate vectors.
  • Remember points have homogeneous co-ordinate w
    1

translate along y
16
Transformation Composition
  • More complex transformations can be created by
    concatenating or composing individual
    transformations together.
  • Matrix multiplication is non-commutative ? order
    is vital
  • We can create an affine transformation
    representing rotation about a point PR
  • translate to origin, rotate about origin,
    translate back to original location

17
Transformation Composition
18
Transformation Composition
Rotation in XY plane by q degrees anti-clockwise
about point P
19
Transformations and OpenGL
  • glRotatef(angle, vx, vy, vz)
  • rotates about axis (vx, vy, vz) by angle
    (specified in degrees)
  • glTranslate(dx, dy, dz)
  • translates by displacement vector (dx, dy, dz)
  • glScalef(sx, sy, sz)
  • apply scaling of sx in x direction, sy in y
    direction and sz in z direction (note that these
    values specify the diagonal of a required matrix)
  • glLoadIdentity()
  • creates an identity matrix (used for clearing all
    transformations)
  • glLoadMatrixf(matrixptr)
  • loads a user specified transformation matrix
    where matrixptr is defined as GLfloat
    matrixptr16

20
Transformations and OpenGL
  • OpenGL defines 3 matrices for manipulation of 3D
    scenes
  • GL_MODELVIEW manipulates the view and models
    simultaneously
  • GL_PROJECTION performs 3D 2D projection for
    display
  • GL_TEXTURE for manipulating textures prior to
    mapping on objects
  • Each acts as a state parameter once set it
    remains until altered.
  • Having defined a GL_MODELVIEW matrix, all
    subsequent vertices are created with the
    specified transformation.
  • Matrix transformation operations apply to the
    currently selected system matrix
  • use glMatrixMode(GL_MODELVIEW) to select modeling
    matrix

21
Transformations and OpenGL
Vertex Geometry Pipeline
MODELVIEW matrix
PROJECTION matrix
perspective division
viewport transformation
original vertex
final window coordinates
normalised device coordinates (foreshortened)
2d projection of vertex onto viewing plane
vertex in the eye coordinate space
22
Transformations and OpenGL
  • The MODELVIEW matrix is a 4x4 affine
    transformation matrix and therefore has 12
    degrees of freedom
  • The MODELVIEW matrix is used for both the model
    and the camera transformation
  • rotating the model is equivalent to rotating the
    camera in the opposite direction ? OpenGL uses
    the same transformation matrix
  • this sometimes causes confusion!

23
Transformations and OpenGL
  • Each time an OpenGL transformation M is called
    the current MODELVIEW matrix C is altered

glTranslatef(1.5, 0.0, 0.0) glRotatef(45.0, 0.0,
0.0, 1.0)
24
Transformations and OpenGL
  • Transformations are applied in the order
    specified (with respect to the vertex) which
    appears to be in reverse

glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTr
anslatef(1.5, 0.0, 0.0) glRotatef(45.0, 0.0,
0.0, 1.0) glVertex3f(1.0, 0.0, 0.0)
original
rotate
translate
25
Relevant 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.

26
Relevant Libraries
  • The GL library
  • core functions of OpenGL e.g. modelling, viewing,
    clipping, lighting
  • The GL Utility (GLU) library
  • creation of common objects (e.g. spheres,
    quadrics)
  • specification of standard views (e.g.
    perspective, orthographic)
  • The GL Utility Toolkit (GLUT)
  • provides the interface with the windowing system.
  • window management, menus, mouse interaction

27
The OpenGL Utility Toolkit
  • OpenGL has no window system interface of its own
  • GLUT is a programming interface for writing
    window system independent OpenGL programs
  • Alternatives are native window systems APIs e.g.
    Xlib, Motif, Win32, tk, aux
  • The GLUT API is mostly windows system independent
    and implementations exist for most popular
    platforms
  • GLUT is not part of the standard OpenGL
    distribution

28
GLUT support
  • - Multiple windows for OpenGL rendering
  • - Callback driven event processing
  • - Sophisticated input devices
  • - An 'idle' routine and timers
  • - A simple, cascading pop-up menu facility
  • - Utility routines to generate various solid
    and wire frame objects
  • - Support for bitmap and stroke fonts
  • - Miscellaneous window management functions

29
Setting 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

30
Simple 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

31
Simple 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

32
Simple 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)

33
GLUT
  • 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

34
GLUT 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

35
GLUT 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

36
Conventions
  • 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)

37
Initialisation
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

38
Beginning 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

39
Window 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

40
Call 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)

41
Geometric 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
20, 20
8, 20
42
GLUT 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
  • http//www.opengl.org/Documentation/GLUT.html
  • More GLUT info at opengl.org
  • http//www.opengl.org/developers/documentation/glu
    t.html
  • GLUT for win32
  • http//www.xmission.com/nate/glut.html

43
OpenGL info
  • Hearn and Baker OpenGL Supplement
  • http//www.ncsa.uiuc.edu/Vis/Graphics/OpenGL.html
  • OpenGL Programming Guide (2nd Ed) - Mason Woo,
    Jackie Neider, and Tom Davis
  • OpenGL Reference Manual (2nd Ed) - OpenGL ARB,
    Editors R. Kempf, C. Frazier
  • OpenGL SuperBible - R.S. Wright, M. Sweet
Write a Comment
User Comments (0)
About PowerShow.com