Title: Rotated Ellipses
1Rotated Ellipses
- Hard!
- Many APIs dont support non-axis aligned
ellipses - DaSilvas Masters Thesis, Brown University, 1989
- http//www.codeguru.com/gdi/ellipse.shtml
2OpenGL and GLUT
3What is OpenGL?
- A software interface to graphics hardware
- Industry Standard
- Platform independent (unlike Direct3D)
- Low level
- No modeling functions
- Simple geometric primitives
- Points, lines, polygons
4Drivers
- Windows ships with OpenGL 1.1 software drivers
- Current 3D graphics cards should ship with OpenGL
1.2 - Linux - Mesa, a work-alike 3D graphics library
5Complementary Libraries
- OpenGL Utility Library (GLU)
- quadratic surfaces, NURBS curves, etc.
- Open Inventor (a high level OO 3D library)
- Windowing?
- X window interface library (GLX)
- MS Windows window interface library (WGL)
- Macintosh window interface library (AGL)
- GL Auxiliary library (GLAUX)
- OpenGL Utility Toolkit (GLUT)
6Other Windowing Libraries
- All you need for OpenGL is an OpenGL context in
which to draw
- VisualBasic
- Win32
- Aqua
- Tcl/Tk
- Java
- FLTK
- WX Windows
- QT
- GTK
7Books
- Mason Woo, Jackie Neider, Tom Davis, Dave
Shreiner, OpenGL Architecture Review Board,
OpenGL(R) Programming Guide The Official Guide
to Learning OpenGL, Version 1.2, ISBN 0201604582,
AKA the redbook - Richard S. Wright Jr., Michael R. Sweet, OpenGL
SuperBible, Second Edition (2nd Edition), ISBN
1571691642
8Websites
- Older version of redbook (OpenGL 1.1)
- http//ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/
- SGI_Developer/OpenGL_PG/
- OpenGL website
- http//www.opengl.org
- SGI OpenGL
- http//www.sgi.com/software/opengl
9Simplified Pipeline
- 1. Construct Shapes from Geometric Primitives,
thereby creating mathematical descriptions of
objects (points, lines, polygons, images, and
bitmaps) - 2. Arrange the objects in 3D space and select
the desired viewpoint for viewing the scene
10Simplified Pipeline (cont.)
- 3. Calculate the color of all the objects.
Either by assignment by the program, lighting
conditions, texture mapping, or a combination - 4. Convert the mathematical description of
objects and associated color information to
pixels on the screen (rasterization)
11Client/Server
- For X Windows
- The program that issues drawing commands is the
client - The computer that does receives the commands and
does the drawing is the server
12State Machine
- The OpenGL server is a state machine
- Retains state (background color for example)
until explicitly changed - Many state variables are modes enabled by
glEnable or glDisable (fog, lighting, depthtest,
etc.) - Other state variables include view and projection
transforms
13Programming
- Quick look at code
- Include files
- Library files
- Setting up windows
- Display function
- First running example
14Example Code
- include ltwhateverYouNeed.hgt
- main()
-
- InitializeAWindowPlease()
- glClearColor (0.0, 0.0, 0.0, 0.0)
- glClear (GL_COLOR_BUFFER_BIT)
- glColor3f (1.0, 1.0, 1.0)
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
- glBegin(GL_POLYGON)
- glVertex3f (0.25, 0.25, 0.0)
- glVertex3f (0.75, 0.25, 0.0)
- glVertex3f (0.75, 0.75, 0.0)
- glVertex3f (0.25, 0.75, 0.0)
- glEnd()
- glFlush()
- UpdateTheWindowCheckForEvents()
15GL Functions and Datatypes
16Include Files
- Include files
- include ltGL/gl.hgt
- include ltGL/glu.hgt
- or
- include ltGL/glut.hgt
- sometimes glut.h is placed in the directory above
(some Unix). - include ltglut.hgt
17Library Files
- opengl32.lib
- glu32.lib
- glut32.lib
- glaux.lib (optional)
18Window Management
- Five tasks needed for window initialization
- glutInit(int argc, char argv)
- glutInitDisplayMode(unsigned int mode)
- glutInitWindowPosition(int x, int y)
- glutInitWindowSize(int width, int size)
- int glutCreateWindow(char string)
19Display Callback
- The drawing function
- glutDisplayFunc(void (func)(void))
- Calling for a redraw
- glutPostRedisplay(void)
20Main Loop
- glutMainLoop(void)
- GLUT takes over
- handles events
- calls appropriate callback functions for display
and input events - never exits until program ends
- you do all your work in callback functions
21Hello World, GL
- include ltGL/gl.hgt
- include ltGL/glut.hgt
- void display(void)
-
- / clear all pixels /
- glClear (GL_COLOR_BUFFER_BIT)
- / draw white polygon (rectangle) with corners
at - (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
- /
- glColor3f (1.0, 1.0, 1.0)
- glBegin(GL_POLYGON)
- glVertex3f (0.25, 0.25, 0.0)
- glVertex3f (0.75, 0.25, 0.0)
- glVertex3f (0.75, 0.75, 0.0)
- glVertex3f (0.25, 0.75, 0.0)
- glEnd()
- void init (void)
-
- / select clearing (background) color /
- glClearColor (0.0, 0.0, 0.0, 0.0)
- / initialize viewing values /
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
-
- int main(int argc, char argv)
-
- glutInit(argc, argv)
- glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
- glutInitWindowSize (250, 250)
- glutInitWindowPosition (100, 100)
- glutCreateWindow ("hello")
- init ()
22Customizing the Example
- Clearing
- Coloring
- Forcing the draw completion
23Clearing
- Clear the screen (background color)
- glClearColor(0.0, 0.0, 0.0, 0.0)
- glClear(GL_COLOR_BUFFER_BIT)
- (later, you will need to clear the depth buffer)
24Specifying Drawing Color
- glColor3f(Glfloat r, Glfloat g, Glfloat b)
- glColor3f(0.0, 0.0, 0.0) black
- glColor3f(1.0, 0.0, 0.0) red
- glColor3f(0.0, 1.0, 0.0) green
- glColor3f(1.0, 1.0, 0.0) yellow
- glColor3f(0.0, 0.0, 1.0) blue
- glColor3f(1.0, 0.0, 1.0) magenta
- glColor3f(0.0, 1.0, 1.0) cyan
- glColor3f(1.0, 1.0, 1.0) white
25glBegin and glEnd
- Drawing commands are sandwiched between glBegin
and glEnd commands - Can have multiple glBegin/glEnd pairs
- glBegin(GL_POLYGON)
- glVertex3f (0.25, 0.25, 0.0)
- glVertex3f (0.75, 0.25, 0.0)
- glVertex3f (0.75, 0.75, 0.0)
- glVertex3f (0.25, 0.75, 0.0)
- glEnd()
26glBegin and glEnd (cont.)
- GL_POINTS
- individual points
- GL_LINES
- pairs of vertices as individual line segments
- GL_LINE_STRIP
- series of connected lines
- GL_LINE_LOOP
- first and last vertices connected
- GL_TRIANGLES
- triples of vertices as triangles
27glBegin and glEnd (cont.)
- GL_TRIANGLE_STRIP
- linked strip of triangles. Reuse of vertices.
- GL_TRIANGLE_FAN
- linked fans of triangles
- GL_QUADS
- each set of 4 points forms 4 sided polygon
- GL_QUAD_STRIP
- linked strip of quadrilaterals
- GL_POLYGON
- simple convex polygon of all points
28glBegin and glEnd (cont.)
29glFlush
- glFlush to force gl command execution
- cleans out pipeline
- stop buffering commands for network transmission
- software renderers that do not immediately update
the screen - to ensure compatibility, you should have a
glFlush at the end of each frame or scene
30Other Event Handlers
- glutReshapeFunc(void (func)(int w, int h))
- glutKeyboardFunc(void (func)(unsigned char key,
int x, int y)) - glutMouseFunc(void (func)(int button, int state,
int x, int y)) - glutMotionFunc(void (func)(int x, int y))
31GLUT.H
- Look inside GLUT (glut.h) for more cool functions
- http//www.tangent.org/brian/dict/spec3/node110.h
tml - IdleFunc
- TimerFunc
- menu items
- changing the cursor
- drawing text
- handling multiple windows
- built-in models
32Animation
- Charlie Chaplin 16 frames a second
- Modern film 24 frames a second
- NTSC Video 30 frames a second
- PAL Video 25 frames a second
- limit of interactive applications
- 10 frames a second
33Snow Day Implementation
- open_window()
- for (i 0 i lt 1000000 i)
- clear_the_window()
- draw_frame(i)
- wait_until_a_24th_of_a_second_is_over()
-
- complete frame is up only a fraction of a second,
lots of flashing
34Double Buffering
- open_window_in_double_buffer_mode()
- for (i 0 i lt 1000000 i)
- clear_the_window()
- draw_frame(i)
- swap_the_buffers()
-
- Hardware swapping is faster, a fully drawn
picture is up on screen for nearly all the time
35Animation Example
- include ltGL/gl.hgt
- include ltGL/glu.hgt
- include ltGL/glut.hgt
- include ltstdlib.hgt
- static GLfloat spin 0.0
- void init(void)
- glClearColor (0.0, 0.0, 0.0, 0.0)
- glShadeModel (GL_FLAT)
-
- void display(void)
- glClear(GL_COLOR_BUFFER_BIT)
- glPushMatrix()
- glRotatef(spin, 0.0, 0.0, 1.0)
- glColor3f(1.0, 1.0, 1.0)
- glRectf(-25.0, -25.0, 25.0, 25.0)
- glPopMatrix()
- glutSwapBuffers()
- void spinDisplay(void)
- spin spin 2.0
- if (spin gt 360.0)
- spin spin - 360.0
- glutPostRedisplay()
-
- int main(int argc, char argv)
- glutInit(argc, argv)
- glutInitDisplayMode (GLUT_DOUBLE GLUT_RGB)
- glutInitWindowSize (250, 250)
- glutInitWindowPosition (100, 100)
- glutCreateWindow (argv0)
- init ()
- glutDisplayFunc(display)
- glutReshapeFunc(reshape)
- glutMouseFunc(mouse)
- glutMainLoop()
- return 0
void reshape(int w, int h) glViewport (0, 0,
(GLsizei) w, (GLsizei) h) glMatrixMode(GL_PROJ
ECTION) glLoadIdentity() glOrtho(-50.0,
50.0, -50.0, 50.0, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW) glLoadIdentity()
void mouse(int button, int state, int x, int y)
switch (button) case
GLUT_LEFT_BUTTON if (state
GLUT_DOWN) glutIdleFunc(spinDisplay)
break case GLUT_MIDDLE_BUTTON if
(state GLUT_DOWN) glutIdleFunc(NULL)
break default break
36glOrtho
- void glOrtho(GLdouble left,
- GLdouble right,
- GLdouble bottom,
- GLdouble top,
- GLdouble near,
- GLdouble far)
37gluPerspective()
- Perspective Transform
- To be discussed later