Title: Chapter 2 Graphics Programming
1Chapter 2Graphics Programming
2Using OpenGL in Visual C 1/3
- Opengl32.dll and glu32.dll should be in the
system folder - Opengl32.lib and glu32.lib should be in the lib
folder for VC - gl.h and glu.h should be in a folder called GL
under the include folder for VC - Get glut32.lib, glut32.dll and glut.h from the
course homepage and put them in the same places
as the other files
3Using OpenGL in Visual C 2/3
- Fire up visual studio
- Create a new project as the followingFile ? New
? Project (input your project name, then a
directory (workspace) with the same name will be
built) ? Win32 Console Application ? An Empty
Application
4Using OpenGL in Visual C 3/3
- In the workspace click on File View to access
(expand) the source code tree - In Source Files, add in the source code (.c
files) - Select Project ? Settings ? Link andin
Object/library modules add Opengl32.lib
glu32.lib glut32.lib - Press F7 to build your_project.exe
5Sierpinski Gasket 1/2
- Pick an initial point at random inside the
triangle - Select one of the three vertices at random
- Find the point halfway between the initial point
and the randomly selected vertex - Display the new point
- Replace the initial point with this new point
- Return to step 2
6Sierpinski Gasket 2/2
- main()
-
- initialize_the_system()
- for(some_number_of_points)
-
- pt generate_a_point()
- display_the_point(pt)
-
- cleanup()
7Programming 2D Applications
- A vertex is a location in space
- glVertex
- is in the form of nt or ntv
- n is the number of dimensions
- t denotes the data type integer (i), float (f),
or double (d) pointer to an array (v) - define GLfloat float
8Examples
- glVertex2i(GLint xi, GLint yi)
- glVertex3f(GLfloat x, GLfloat y, GLfloat z)
- GLfloat vertex3glVertex3fv(vertex)
9OpenGL Object Examples
- glBegin(GL_LINES) glVertex2f(x1,
y1) glVertex2f(x2, y2)glEnd() - glBegin(GL_POINTS) glVertex2f(x1,
y1) glVertex2f(x2, y2)glEnd()
10Code of Sierpinski Gasket 1/3
- Typedef GLfloat point22
- void display(void)
-
- point2 vertices30,0,250,500,500,0
- / an arbitrary triangle /
- static point2 p 75,50
- / any desired initial point /
- int j, k
- int rand() / standard random-number generator
/
11Code of Sierpinski Gasket 2/3
- for(k0klt5000k)
-
- jrand()3 / pick a random vertex from 0, 1,
2 / - p0(p0verticesj0)/2 / compute new
point / - p1(p1verticesj1)/2
- glBegin(GL_POINTS)
- glVertex2fv(p) / display new point /
- glEnd()
-
- glFlush()
12Code of Sierpinski Gasket 3/3
13Questions
- In what colors are we drawing
- Where on the screen does our image appear
- How large will the image be
- How do we create an area of the screen a window
for our image? - How long will the image remain on the screen?
14Coordinate Systems
15OpenGL API
16Graphics Functions 1/3
17Graphics Functions 2/3
- Primitive functions points, line segments,
polygons, pixels, text, curves, surfaces - Attributes functionscolor, pattern, typeface
- Viewing functionsposition, orientation, clipping
18Graphics Functions 3/3
- Transformation functionsrotation, translation,
scaling - Input functionskeyboards, mice, data tablets
- Control functionscommunicate with windows,
initialization, error handling - Inquiry functions number of colors, camera
parameters/values
19Graphics Pipeline and State Machine
- Functionalities of graphics functions
- Define primitive glvertex
- Change opengl state (most of them!)
- Present color, current matrix
20OpenGL Interface
- GL (OpenGL in Windows)
- GLU (graphics utility library)uses only GL
functions, creates common objects (such as
spheres) - GLUT (GL Utility Toolkit)interfaces with the
window system - GLX glue between OpenGL and Xwindow, used by GLUT
21OpenGL Library Organization
22API Dilemma for Primitives
- Minimal or maximal?
- Convenience versus portability
- OpenGLs intermediate approach
- GL contains a small set of basic primitives
- GLU contains richer set of objects
23OpenGL Primitives
- Geometric primitives and raster primitives
24Points and Line Segments
25Polygon Basics 1/3
Filled objects
Methods of displaying a polygon
26Polygon Basics 2/3
Simple
Nonsimple
27Polygon Basics 3/3
Convexity
Convex objects
28Polygon Types in OpenGL 1/2
In OpenGL, edges and interior must be drawn
separately!
29Polygon Types in OpenGL 2/2
Use triangles if possible because of efficiency!
30Drawing a Sphere 1/5
31Drawing a Sphere 2/5
z
x
?
y
?
32Drawing a Sphere 3/5
- cM_PI/180.0 // degrees to radians,
M_PI3.14159 - for(phi-80.0 philt80.0 phi20.0)
- glBegin(GL_QUAD_STRIP)
- for(theta-180.0 thetalt180.0 theta20.0)
- xsin(ctheta)cos(cphi)
- ycos(ctheta)cos(cphi)
- zsin(cphi)
- glVertex3d(x, y, z)
- xsin(ctheta)cos(c(phi20.0))
- ycos(ctheta)sin(c(phi20.0))
- zsin(c(phi20.0))
- glVertex3d(x, y, z)
-
- glEnd()
Drawing the portion of lower latitudes
33Drawing a Sphere 4/5
- xy0
- z1
- glBegin(GL_TRIANGLE_FAN)
- glVertex3d(x, y, z)
- cM_PI/180.0
- zsin(c80.0)
- for(theta-180.0 thetalt180.0 theta20.0)
- xsin(ctheta)cos(c80.0)
- ycos(ctheta)sin(c80.0)
- glVertex3d(x, y, z)
-
- glEnd()
Drawing the portion around the north pole
34Drawing a Sphere 5/5
- xy0
- z-1
- glBegin(GL_TRIANGLE_FAN)
- glVertex3d(x, y, z)
- z-sin(c80.0)
- for(theta-180.0 thetalt180.0 theta20.0)
- xsin(ctheta)cos(c80.0)
- ycos(ctheta)sin(c80.0)
- glVertex3d(x, y, z)
-
- glEnd()
Drawing the portion around the south pole
35Text Stroke or Raster
36Attributes for Lines, Texts
37Color 1/4
CT1RT2GT3B, T1, T2, T3 are the tristimulus
values
Additive color matching
38Color 2/4
- Basic tenet of three-color theoryIf two colors
produce the same tristimulus values, then they
are visually indistinguishable - The range of colors that we can produce on a
given system is called that systems color gamut
39Color 3/4
Color Solid
40Color 4/4
Subtractive Color
Additive Color
41RGB Color
- glColor3f(1.0, 0.0, 0.0)Can be used to specify
24 bits true colors - Alpha channelUsed in OpenGL as an opacity or
transparency value - glClearColor(1.0, 1.0,1.0,1.0)
- glPointSize(2.0)
42Indexed Color
Color-lookup table
Example km8 pick 256 out of 16M colors
43Two-dimensional Viewing
Objects before clipping
Image after clipping
44Viewing Volume
OpenGL default 2?2?2 cube
45Orthographic View
void glOrtho(GLdouble left, GLdouble right, )
46Matrix Mode
- There are two matrices in OpenGL
- Model-view defines COP and orientation
- Projection defines the projection matrix
- glMatrixMode(GL_PROJECTION)glLoadIdentity()glu
Ortho2D(0.0, 500.0, 0.0, 500.0)glMatrixMode(GL_M
ODELVIEW)
47Control Functions
- OpenGL assumes origin is bottom left
- glutInit(int argcp, char argv)
- glutCreateWindow(char title)
- glutInitDisplayMode(GLUT_RGB GLUT_DEPTH
GLUT_DOUBLE) - glutInitWindowSize(480,640)
- glutInitWindowPosition(0,0)
- OpenGL default RGB color, no hidden-surface
removal, single buffering
48Aspect Ratio
Viewing rectangle glOrtho()
Display window glutInitWindowSize()
49Viewports
void glViewport(GLint x, GLint y, GLsizei w,
GLsizei h) Viewport is part of the state.
50main, display, myinit functions
- void glutMainLoop(void)Why do we need this?
- void glutDisplayFunc(void (func)(void))When is
the function invoked?
51Sample main program
- Void main(int argc, char argv)
-
- glutInit(argc,argv)
- glutInitDisplayMode(GLUT_SINGLE
GLUT_RGB)glutInitWindowSize(500, 500) - glutInitWindowPosition(0, 0)glutCreateWindow(S
imple OpenGL example) - glutDisplayFunc(display)
- myinit()
- glutMainLoop()
For most non-interactive applications
52Gasket Program 1/3
- void myinit(void)
-
- / attributes /
- glClearColor(1.0, 1.0, 1.0, 1.0) / white
background / - glColor3f(1.0, 0.0, 0.0) / draw in red /
- / set up viewing /glMatrixMode(GL_PROJECTION)
glLoadIdentity()gluOrtho2D(0.0, 500.0, 0.0,
500.0)glMatrixMode(GL_MODELVIEW)
53Gasket Program 2/3
- void display(void)
-
- typedef GLfloat point22 / define a point
data type /point2 vertices30.0,0.0,250.0,
500.0,500.0,0.0 / triangle / - int i, j, kint rand()
- point2 p75.0, 50.0 / arbitrary point
inside triangle / - glClear(GL_COLOR_BUFFER_BIT) / clear the
window /
54Gasket Program 3/3
- / compute and output 5000 new points /
- for(k0 klt5000 k)
- jrand()3 / compute point halfway between
vertex and old point / p0(p0vertexj0)/
2.0 p1(p1vertexj1)/2.0 - glBegin(GL_POINTS) / plot point
/ glVertex2fv(p) glEnd() -
- glFlush()
55Using Recursion 1/3
- void triangle(point2 a, point2 b, point2 c)
-
- glBegin(GL_TRIANGLES)
- glVertex2fv(a)
- glVertex2fv(b)
- glVertex2fv(c)
- glEnd()
56Using Recursion 2/3
- void divide_triangle(point2 a, point2 b, point2
c, int k) -
- point2 ab, ac, bc
- int j
- if(kgt0)
- for(j0 jlt2 j) abj(ajbj)/2
- for(j0 jlt2 j) acj(ajcj)/2
- for(j0 jlt2 j) bcj(bjcj)/2
- divide_triangle(a, ab, ac, k-1)
- divide_triangle(c, ac, bc, k-1)
- divide_triangle(b, bc, ab, k-1)
-
- else triangle(a, b, c) / draw triangle at end
of of recursion /
a
ab
ac
b
c
bc
57Using Recursion 3/3
- Void display(void)
-
- glClear(GL_COLOR_BUFFER_BIT)
- divide_triangle(v0, v1, v2, n)
- glFlush()
58Three-Dimensional Gasket 1/2
- typedef Glfloat point33
- point3 vertices4 0,0,0,0,250,500,100,500
,250,250,250,100,250 - Point3 new,old250, 100, 250
- jrand()4
- new0(old0verticesj0)/2
- new1(old1verticesj1)/2
- new2(old2verticesj2)/2
tetrahedron
59Three-Dimensional Gasket 2/2
- / plot point /
- glBegin(GL_POINTS)
- glVertex3fv(new)
- glEnd()
- / replace old point by new /
- old0new0
- old1new1
- old2new2
603D Recursive Gasket 1/3
- void triangle(point3 a, point3 b, point3 c)
-
- glBegin(GL_POLYGON)
- glVertex3fv(a)
- glVertex3fv(b)
- glVertex3fv(c)
- glEnd()
613D Recursive Gasket 2/3
- void divide_triangle(point3 a, point3 b, point3
c, int k) -
- point3 ab, ac, bc
- int j
- if(kgt0)
- for(j0 jlt3 j) abj(ajbj)/2
- for(j0 jlt3 j) acj(ajcj)/2
- for(j0 jlt3 j) bcj(bjcj)/2
- divide_triangle(a, ab, ac, k-1)
- divide_triangle(c, ac, bc, k-1)
- divide_triangle(b, ab, ab, k-1)
-
- else triangle(a, b, c) / draw triangle at end
of recursion/
623D Recursive Gasket 3/3
- void tetrahedron(int n)
-
- glColor3f(1.0, 0.0, 0.0)
- divide_triangle(v0, v1, v2, k)
- glColor3f(0.0, 1.0, 0.0)
- divide_triangle(v3, v2, v1, k)
- glColor3f(0.0, 0.0, 1.0)
- divide_triangle(v0, v3, v1, k)
- glColor3f(0.0, 0.0, 0.0)
- divide_triangle(v0, v2, v3, k)
63Hidden-Surface Removal 1/3
64Hidden-Surface Removal 2/3
- Z-Buffer
- glutInitDisplayMode(GLUT_SINGLE GLUT_RGB
GLUT_DEPTH) - glEnable(GL_DEPTH_TEST)
- glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
65Hidden-Surface Removal 3/3
- void display()
-
- glClear(GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT) - tetrahedron(n)
- glFlush()
66Summary and Notes
- Coding OpenGL in Visual C
- OpenGL API
- Graphics (primitives, attributes), viewing,
control, communications with windows - Sample codes