Title: Graphics Programming
1Graphics Programming
2Objectives
- Introduce a standard program structure
- Two-dimensional viewing as a special case of
three-dimensional viewing - Fundamental OpenGL primitives and their
attributes - A more sophisticated 3D example
- Introduce hidden-surface removal
3Window-Based Programming
void main() // Skeleton of even-driven OpenGL
program initialize things Create a screen
window glutDisplayFunc ( myDisplay )
//register redraw( ) glutReshapeFunc (
myReshape ) //register reshape( )
glutMouseFunc ( myMouse ) //register mouse
action( ) glutKeyboardFunc ( myKeyboard )
//register keyboard action( ) initialize other
things glutMainLoop() //enter main loop all
of the callback functions are defined here
4main.c
- include ltGL/glut.hgt
- int main(int argc, char argv)
-
- glutInit(argc,argv)
- glutInitDisplayMode(GLUT_SINGLEGLUT_RGB)
- glutInitWindowSize(500,500)
- glutInitWindowPosition(0,0)
- glutCreateWindow("simple")
- glutDisplayFunc(mydisplay)
- init()
- glutMainLoop()
5init.c
- void init()
-
- glClearColor (0.0, 0.0, 0.0, 1.0)
- glColor3f(1.0, 1.0, 1.0)
- glMatrixMode (GL_PROJECTION)
- glLoadIdentity ()
- glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
6The Orthographic View
7Coordinate Systems
- Internally, OpenGL will convert to camera (eye)
coordinates and later to screen coordinates - OpenGL uses some internal representations that
usually are not visible to the application
OpenGL places a camera at the origin in object
space pointing in the negative z direction
8Orthographic Viewing
- In the orthographic view,
9Transformations and Viewing
- In OpenGL, projection is carried out by a
projection matrix (transformation). We must set
the matrix mode first - glMatrixMode (GL_PROJECTION)
- Transformations incremental we start with an
identity matrix and multiply it with a projection
matrix - glLoadIdentity()
- glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
102D and 3D viewing
- In glOrtho(left, right, bottom, top, near, far)
the near and far distances are measured from the
camera - If the application is in two dimensions
-
11mydisplay.c
- void mydisplay()
-
- glClear(GL_COLOR_BUFFER_BIT)
- glBegin(GL_POLYGON)
- glVertex2f(-0.5, -0.5)
- glVertex2f(-0.5, 0.5)
- glVertex2f(0.5, 0.5)
- glVertex2f(0.5, -0.5)
- glEnd()
- glFlush()
OpenGL displays polygons correctly if they are
12OpenGL Primitives
13Attributes
- Attributes determine the appearance of objects
Arial Arial bold Arial italic
14RGB color
- Each color component is stored separately in the
frame buffer - Usually 8 bits per component in buffer
- Color values in glColor3f range from 0.0 (none)
to 1.0 (all), - The values range from 0 to 255 in glColor3ub
15Indexed Color
- Colors are indices into tables of RGB values
-
16Color in OpenGL
- The color as set by glColor becomes part of the
state and will be used until changed - Default is smooth shading
- Alternative is flat shading
17Example Sierpinski Gasket
- Start with a triangle
- Connect bisectors of sides and remove central
triangle - Repeat 5 times
18Gasket Program
- include ltGL/glut.hgt
- GLfloat v32-1.0, -0.58,1.0, -0.58,
- 0.0, 1.15 / initial triangle /
- int n / number of recursive steps /
/ display one triangle / void triangle(
GLfloat a, GLfloat b, GLfloat c)
glVertex2fv(a) glVertex2fv(b)
glVertex2fv(c)
19Triangle Subdivision
- void divide_triangle(GLfloat a, GLfloat b,
- GLfloat c, int m)
-
- point2 v0, v1, v2
- int j
- if(mgt0)
-
- for(j0 jlt2 j) v0j(ajbj)/2
- for(j0 jlt2 j) v1j(ajcj)/2
- for(j0 jlt2 j) v2j(bjcj)/2
- divide_triangle(a, v0, v1, m-1)
- divide_triangle(c, v1, v2, m-1)
- divide_triangle(b, v2, v0, m-1)
-
- else( triangle(a,b,c) )
- / draw triangle at end of recursion /
20display and init Functions
- void display()
- glClear(GL_COLOR_BUFFER_BIT)
- glBegin(GL_TRIANGLES)
- divide_triangle(v0, v1, v2, n)
- glEnd()
- glFlush()
-
- void myinit()
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluOrtho2D(-2.0, 2.0, -2.0, 2.0)
- glMatrixMode(GL_MODELVIEW)
- glClearColor (1.0, 1.0, 1.0,1.0)
- glColor3f(0.0,0.0,0.0)
21main Function
- int main(int argc, char argv)
- n4
- glutInit(argc, argv)
- glutInitDisplayMode(GLUT_SINGLEGLUT_RGB)
- glutInitWindowSize(500, 500)
- glutCreateWindow(2D Gasket")
- glutDisplayFunc(display)
- myinit()
- glutMainLoop()
223D Applications
- In OpenGL, two-dimensional applications are a
special case of three-dimensional graphics - Going to 3D
- Not much changes
- Use glVertex3( )
- Have to worry about the order in which polygons
are drawn or use hidden-surface removal
233D Gasket
24triangle code
- void triangle( GLfloat a, GLfloat b, GLfloat
c) -
- glVertex3fv(a)
- glVertex3fv(b)
- glVertex3fv(c)
25subdivision
- void divide_triangle(GLfloat a, GLfloat b,
- GLfloat c, int m)
- GLfloat v13, v23, v33
- int j
- if(mgt0)
-
- for(j0 jlt3 j) v1j(ajbj)/2
- for(j0 jlt3 j) v2j(ajcj)/2
- for(j0 jlt3 j) v3j(bjcj)/2
- divide_triangle(a, v1, v2, m-1)
- divide_triangle(c, v2, v3, m-1)
- divide_triangle(b, v3, v1, m-1)
-
- else(triangle(a,b,c))
26tetrahedron
- void tetrahedron( int m)
-
- glColor3f(1.0,0.0,0.0)
- divide_triangle(v0, v1, v2, m)
- glColor3f(0.0,1.0,0.0)
- divide_triangle(v3, v2, v1, m)
- glColor3f(0.0,0.0,1.0)
- divide_triangle(v0, v3, v1, m)
- glColor3f(0.0,0.0,0.0)
- divide_triangle(v0, v2, v3, m)
27Hidden-Surface Removal
get this
want this
28Hidden-Surface Removal
- We want to see only those surfaces in front of
other surfaces -
29Using the z-buffer algorithm
- The algorithm
- in main.c
-
- in init.c,
- in the display callback
30Viewport
- Simple rendering pipeline
- The need of viewport transformation
-
31World Window and Viewport
32Viewport
- Do not have use the entire window for the image
glViewport(x,y,w,h) - Values in pixels (screen coordinates)
33Window-to-Viewport Mapping
- (x, y) in world window should be mapped to
(sx,sy) in viewport proportionally. - Mapping is done internally in OpenGL
34Window-to-Viewport Mapping
- Every vertex used in window to define an object
should be mapped to viewport
35Window-to-Viewport Mapping
36Example
The window has (W.l, W.r, W.b, W.t) (0, 2.0, 0,
1.0), and the viewport has (V.l, V.r, V.b, V.t)
(40, 400, 60, 300). We compute
37Resizing without Distortion
- To prevent from distortion, a viewport with the
same aspect ratio of world window is required. - aspect ratio width / height
glViewport ( 0, 0, W, W/R )
glViewport ( 0, 0, HR, H )
38Resizing without Distortion
glutReshapeFunc ( myReshape ) // Making a
matched viewport void myReshape ( GLsizei W,
GLsizei H ) //use (global)window aspect
ratio if ( RgtW/H ) setViewport( 0, W, 0, W/R
) else setViewport( 0, HR, 0, H )
39Use Mapping in OpenGL
//---setWindow --- void setWindow ( GLdouble
left, Gldouble right,
GLdouble bottom, GLdouble top )
glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D(left, right,
bottom, top) //---- setViewport ----- void
setViewport ( GLdouble left, Gldouble right,
GLdouble bottom, GLdouble top )
glViewport(left, bottom, right left,
top - bottom)
40Example Tiling
- //set a fixed window
- setWindow ( 0, 640.0, 0, 440.0 )
- for (int i0 ilt5 i) //for each column
- for (int j0 jlt5 j) //for each row
- //set the next viewport
- glViewport ( i64, j44, 64, 44 )
- //draw it again
- drawPolylineFile( dino.dat )
-
41Example Tiling
- for (int i 0i lt5i)
- for (int j 0j lt5j)
- if ((ij)2 0) //if (ij)is even
- //right-side-up window
- setWindow(0.0,640.0,0.0,440.0)
- else
- //upside-down window
- setWindow(0.0,640.0,440.0,0.0)
- //set the next viewport
- glViewport(i 64,j 44,64,44)
- drawPolylineFile("dino.dat")//draw it again
-
42Zooming and Panning
- Zooming scale the world window with respect to
the center of it with viewport fixed. -
- Panning shift the world window with viewport
fixed.