Professional Documents
Culture Documents
TO
COMPUTER
GRAPHIC S
OpenGL 1/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Recap
CG markets: entertainment and science
same tools (hw & sw), different apps come talk to me about a Directed Study
CG paradigms:
sample-based graphics (pixels, image processing) geometry-based graphics
Geometric modeling
primitives decomposition of a model
OpenGL 2/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 3/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Nail Head Body (cylinder) Shaft Point (cylinder) (cone) tree diagram
root node
leaf nodes
Such diagrams are part of 3D program interfaces (e.g., 3D Studio MAX, Maya) As data structure to be rendered, it is called a scenegraph
OpenGL 4/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Translate and Rotate Primitives in their own modeling coordinate system Composition in world (root) coordinate system
Primitives created in decomposition process must be assembled to create final object. Done with affine transformations, T, R, S (as in above example). Other composition operators exist (e.g., Constructive Solid Geometry CSG -- uses Boolean operators).
credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 5/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
2D Primitives
Line Polyline
Polygon Y Y
Circle
OpenGL 6/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Curves
Piecewise linear approximation
Mathematical Splines
(duck)
OpenGL 7/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Example 3D Primitives
Polyline Polyhedron
Sphere
Patch
OpenGL 8/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
x, y Cartesian grid
Integer Grid
NB:Often, screen coordinates are measured top to bottom, based on raster scan
(0,0)
OpenGL 9/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Application program
Graphics System
OpenGL 10/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
2D Hw/Sw Topics
a) Display Hardware: Raster scan vs. Vector b) Color Tables
indirect specification of (pseudo) color color correction, simple types of animation
OpenGL 11/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Ideal Drawing
Vector Drawing
Raster
INTRODUCTION
TO
COMPUTER
GRAPHIC S
2D Raster Architecture
Frame buffer
Display
Raster displays store images (pixmaps, or bitmaps) in a frame buffer, also known as bitmap buffer, or refresh buffer The frame buffer is a chunk of memory located either in separate hardware (VRAM) or in CPUs main memory (DRAM) The frame buffer can be accessed directly, through memory operations Video controller draws all scan-lines at consistent > 60 Hz;
separates update rate of the frame buffer and refresh rate of the display contains Color-Table
OpenGL 13/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
2n intensities or colors
OpenGL 14/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Any specific 2n colors may be inadequate (n may be as low as 16 in low-end systems) Look-up table allows 2n colors out of 224 colors to be used in one image, some other 2n in another image
224 = approx. 16.7 million, exceeds eyes ability to discriminate (somewhere between 7-10 million)
OpenGL 15/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Pixel value is indexed to color look up table (CLUT) where color is stored. Here we use only 12 bits (4bits per color) for clarity typically, 24 bits are used CLUT look-up done at video rates In 24-bit true color systems, 3 x 8 bits for R, G, B; each color has its own 8-bit CLUT (0-255) CLUT allows variety of effects
fast image changes: change table rather than stored image multiple images: select or composite/blend animation hack
OpenGL 16/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
c) BitBlt/RasterOp
(1/3)
Logically operate on each pixel in rectangular source and destination regions in same or different pixmaps to achieve dynamics, e.g., to move/scroll windows on screen RasterOp (Source, Destination) Destination In some implementations S and D need not be same size
OpenGL 17/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
BitBlt/RasterOp
(2/3)
AND (S,D): S can mask out pixels in D OR (S,D): S is non-destructively added to D; used for painting, transparent and kerned characters (where characters extend beyond their boxes)
AND
OR
0 OR anything is anything credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 18/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
More BitBlt/RasterOp
(3/3)
Replace (S,D): S destructively replaces D, i.e., is deleted and copied on top of D (also called Move); used for making opaque characters, icons, scroll Copy (S,D) as above, but S is not deleted XOR (S,D) S selectively inverts D; used in 1-bit systems for cheap cursors:
Note: effects in color systems for all but replace may be weird
OpenGL 19/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Recap
Geometry-based paradigm (model decomposition, scenegraphs) The Graphics Framework 2D hw/sw select topics
display hw: vector vs. raster; the framebuffer, the video controller, the display color-tables BitBlt operation
Application program
Graphics System
OpenGL 20/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Application model
Application program
Graphics System
OpenGL 21/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Graphics Library
examples: OpenGL, DirectX, X3D provides representations and support for:
primitives attributes color line style material properties for 3D lights transformations
OpenGL 22/54
INTRODUCTION
TO
COMPUTER
GRAPHICS
What is OpenGL?
The Open Graphics Library
3-D graphics API specification a software interface to graphics hardware1 raster graphics library pass in vertices, normals, and other scene data get pixels out industry standard supported across many platforms Mac OS, Windows, Linux, iPhone, PSP specification publicly available
September 6, 2011
OpenGL 23/54
INTRODUCTION
TO
COMPUTER
GRAPHICS
example interaction:
program
begin triangle normal (0, 0, -1) vertex (-1, 1, -1, 1) vertex (1, -1, -1, 1) vertex (-1, -1, -1, 1) end triangle <scan-converts the given triangle with normal (0,0,-1) at all vertices>
OpenGL
September 6, 2011
OpenGL 24/54
INTRODUCTION
TO
COMPUTER
GRAPHICS
procedural model
usually accessed through a plain C API NOT object-oriented at all (though this changes gradually in OpenGL 3.1 and beyond)
September 6, 2011
OpenGL 25/54
INTRODUCTION
TO
COMPUTER
GRAPHICS
Direct3D provides a much more object-oriented API than GL. Still, many would argue Direct3D is clunkier and harder to use than OpenGL. Overall, OpenGL is more widely used than Direct3D due to its simplicity and availability on a wider range of platforms. (both used in the games industry, however OpenGL used also w/out fail in scivis.)
September 6, 2011
OpenGL 26/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 28/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Use two buffers (two chunks of memory) Draw always on the back buffer When done drawing, swap buffers
credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 29/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
glutInitDisplayMode()
#include <GL/glut.h> #include <stdio.h> int main(int argc, char **argv) { glutInit( &argc, argv ); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400, 300); glutInitWindowPosition(0,0); glutCreateWindow("GLUT Skeleton"); //initialize callbacks here // my_setup(); glutMainLoop(); return(0); } GLUT_DOUBLE, GLUT_SINGLE GLUT_RGB, GLUT_RGBA, GLUT_INDEX GLUT_DEPTH
credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 30/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
glutInitWindow*()
#include <GL/glut.h> #include <stdio.h> int main(int argc, char **argv) { glutInit( &argc, argv ); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400, 300); glutInitWindowPosition(0,0); glutCreateWindow("GLUT Skeleton"); //initialize callbacks here // my_setup(); glutMainLoop(); return(0); }
OpenGL 31/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
glutCreateWindow()
#include <GL/glut.h> #include <stdio.h> int main(int argc, char **argv) { glutInit( &argc, argv ); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400, 300); glutInitWindowPosition(0,0); glutCreateWindow("GLUT Skeleton"); //initialize callbacks here // my_setup(); glutMainLoop(); return(0); } glutCreateWindow merely requests a window
OpenGL 32/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 33/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
glutMainLoop()
Event-Driven Programming
Interactive graphics program are typically event-driven
OpenGL 34/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Callbacks
OpenGL handles events through the mechanism of callbacks: associate events with procedures procedure gets called-back whenever its associated event takes place Every OpenGL program must handle the display event
OpenGL 35/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Common Callbacks
Input events: glutMouseFunc glutKeyboardFunc System events: glutDisplayFunc (!!!) glutReshapeFunc glutTimerFunc glutIdleFunc Example: setting up callbacks glutDisplayFunc( my_display ); glutIdleFunc( my_idle );
OpenGL 36/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Example my_display()
void my_display(void) { //insert your drawing code always here glClear(GL_COLOR_BUFFER_BIT) ; glColor3f(0,0,1) ; glBegin(GL_POLYGON); glVertex2f(-0.25, 0.75); glVertex2f(0.95, 0.75); glVertex2f(0.95, -0.55); glVertex2f(-0.25, -0.55); glEnd(); glutSwapBuffers(); } return ;
OpenGL 37/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 38/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 39/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 41/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Mouse Inputs
Associate mouse events with my_mouse procedure:
glutMouseFunc(my_mouse)
Write your own code inside my-mouse: void my_mouse(int button, int state, int x, int y) where button can take these values: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON state: GLUT_UP, GLUT_DOWN x, y:
position in window (in pixels) with origin at upper left
September 6-8, 2011 OpenGL 42/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Drawing Space
void glut_setup(void) { glutInitWindowSize(400,100); glutInitWindowPosition(0,0); glutDisplayFunc(my_display); return ; } void my_display(void) { glBegin(GL_POLYGON); glVertex2f(-0.1, 0.7); glVertex2f(0.9, 0.7); glVertex2f(0.9, -0.3); glVertex2f(-0.1, -0.3); glEnd(); glutSwapBuffers(); return ;
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Viewport
Defines where in the window to display the image Uses within window coordinate system (pixel unit; origin at lower left corner) OpenGL default: set to be the entire window
default window reshape: set viewport to whole window
OpenGL 44/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 45/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 46/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
Changes of Co-Ordinates
glBegin(GL_POLYGON); glVertex2i(1,2); glVertex2i(7,2); glVertex2i(4,10); glEnd();
gluOrtho2D(2,8,5,12);
glViewPort(0,0,500,300); glutInitWindowSize(800,500);
glutInitWindowPosition(0,0);
OpenGL 47/54
INTRODUCTION
TO
COMPUTER
GRAPHIC S
OpenGL 48/54