Programming with OpenGL I

blareweyrSoftware and s/w Development

Dec 13, 2013 (3 years and 6 months ago)

270 views

C O M P U T E R G R A P H I CS
Jie chen
Computer graphic
--
Programming with OpenGL I
2
C O M P U T E R G R A P H I CS
Jie chen
What is new
•Website:
http://www.ee.oulu.fi/~jiechen/Course.htm
•Lecture slides for 3
rd
are online now
3
C O M P U T E R G R A P H I CS
Jie chen
A Good news about VS2008
A Good news about VS2008
4
C O M P U T E R G R A P H I CS
Jie chen
VS2008
•A
student
studentof University of Oulu (OulunYliopisto) can
download her/his own
free
freecopies of MS Visual Studio
2008 Professional Edition (and other software) here:
https://www.dreamspark.com/
–This is a Microsoft's site where user requires an MS Live account
(free) and that needs to be verified to be a student account from
our university. This is done using the authentication service that
our university provides and can be done with an account to Paju.
•Also our IT department also links to the site as one of
the software sources for students (in Finnish only)
http://www.oulu.fi/tietohallinto/opiskelijoille/ohjelmistot.ht
ml
5
C O M P U T E R G R A P H I CS
Jie chen
Setup for VS2008
•Run Visual C++ 2008. Go to Tools -> Options, then Projects and
Solutions -> VC++ Directories ->"Show directories for".
–adding "include files”for the folder where you installed GLUT lib and
include folder.
–adding “library files”for the folder where you installed gult32.lib.
6
C O M P U T E R G R A P H I CS
Jie chen
Setup for VS2008
•Go to Project -> Properties. Click on Configuration Properties. Click
the "Configuration Manager" button in the upper-right corner.
Change the "Active solution configuration" from "Debug" to
"Release". Click close, then click OK.
•In Project -> Properties, go to Configuration Properties -> General.
Where it shows the output directory as "Release", backspace the
word "Release", and click OK.
–This makes Visual C++ put the executable in the same directory as the
source code, so when our program needs to open a file, it looks for it in
that directory.
–In this case, the program will have to load in an image file called
"vtr.bmp".
•Go to Build -> Build project_nameto build your project.
•Run the program by going to Debug -> Start Without Debugging. If
all goes well, the test program should run.
7
C O M P U T E R G R A P H I CS
Jie chen
A simple example using OpenGL
8
C O M P U T E R G R A P H I CS
Jie chen
A simple example using OpenGL
•Download the"basic shapes"
program, and
compile and run it (details on how to do that can
be found inLecture 3).
•Take a look at it, and hit ESC when you're done.
•It should look like the following image:
9
C O M P U T E R G R A P H I CS
Jie chen
Overview of How the Program Works
•How does the program work?
–The basic idea is that we tell OpenGL
the 3D coordinates of all of the vertices
of our shapes.
–OpenGL uses the standard x and y axes,
with the positive x direction pointing
toward the right and the positive y
direction pointing upward.
–However, in 3D we need another
dimension, the z dimension. The
positive z direction points out of the
screen.
10
C O M P U T E R G R A P H I CS
Jie chen
Overview of How the Program Works
•How does OpenGL use these 3D
coordinates?
•It simulates the way that our eyes work.
eyes
3D points
11
C O M P U T E R G R A P H I CS
Jie chen
Overview of How the Program Works
•OpenGL converts all of the 3D pointsto pixel
coordinatesbefore it draws anything.
•To do this, it draws a line from each point in the scene to
your eye and takes the intersection of the lines and the
screen rectangle, as in the above picture.
•So, when OpenGL wants to draw a triangle, it converts
the three vertices into pixel coordinates and draws a
"2D" triangle using those coordinates.
3D points
pixel coordinates
12
C O M P U T E R G R A P H I CS
Jie chen
Overview of How the Program Works
•The user's "eye" is always at the originand
looking in the negative z direction.
•Of course, OpenGL doesn't draw anything that is
behind the "eye".
–After all, it isn't the all-seeing eye of Sauron.
The eye of Sauron,
The Lord of the Rings
13
C O M P U T E R G R A P H I CS
Jie chen
Overview of How the Program Works
•How far away is the screen rectangle from
your eye?
•It doesn't matter.
–No matter how far away the screen rectangle
is, a given 3D point will map to the same pixel
coordinates.
–All that matters is the angle that your eye can
see.
14
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•All of this stuff about pixel coordinates is
great and all, but as researcher or
programmer, we want to see some code.
•Take a look at main.cpp.
–Let's go through the file and see if we can
understand what it's doing.
15
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•First, we include our header files.
–Pretty standard stuff for C++.
–If we're using a Mac, we want our program to
include GLUT/glut.hand OpenGL/OpenGL.h;
–otherwise, we include GL/glut.h.
16
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•It just makes it so that we don't have to
typestd::a lot;
–for example, so we can usecoutinstead
ofstd::cout.
17
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•This function handles any keys pressed by the
user.
•For now, all that it does is quit the program when
the user presses ESC, by callingexit.
•The function is passed the x and y coordinates
of the mouse, but we don't need them.
18
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•TheinitRenderingfunction initializes our
rendering parameters.
–The call makes sure that an object (O2)
shows up behind an object (O1).
–Note thatglEnable, like every OpenGL
function, begins with "gl".
19
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•ThehandleResizefunction is called
whenever the window is resized.
–wandhare the new width and height of the
window.
20
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•void gluPerspective(GLdoublefovy, GLdouble
aspect,GLdoublenear, GLdoublefar);.
–fovy=Θ=45.0: telling OpenGL the angle that user's eye can see.
–Near=1.0:indicates not to draw anything with a z coordinate of
smaller than 1. This is so that when something is right next to our
eye, it doesn't fill up the whole screen.
–Far=200.0tells OpenGL not to draw anything with a z
coordinate larger than 200. We don't care very much about stuff
that's really far away.
viewing volume
21
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•So, why doesgluPerspectivebegin with "glu" instead of
"gl"?
–gl: a OpenGL function
–glu: a GLU (GL Utility) function
–glut: a GLUT (GL Utility Toolkit) function
–For examples:
•glRectf(-25.0, -25.0, 25.0, 25.0);
•gluOrtho2D (0.0, w, 0.0, h);
•glutSwapBuffers();
–We won't really worry about the difference among OpenGL, GLU,
and GLUT.
•Just include “glut.h”, that is enough for Windows.
22
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•ThedrawScenefunction is where the 3D
drawing actually occurs.
–callglClearto clear information from the last
time we drew.
–In most every OpenGL program, you'll want to
do this.
23
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•GL_COLOR_BUFFER_BIT: Color
Buffer
–OpenGL defined constants begin
with GL_, use all capital letters,
and use underscores to separate
words
–TheColor Bufferstore the color for
each pixels of the current frame.
–The color is inRGBAmode
i.e.,Red,Green,Blue andAlpha.
–The first 3 components (RGB) can
be considered as color of a pixel.
–Alphavalue can be considered as
theopacityortransparencyof a
pixel.
24
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•GL_DEPTH_BUFFER_BIT: Depth Buffer
–Depth Bufferholds the depth of each pixels of a frame.
–It is also calledz buffer.
–Depth buffer is associated with Depth Test.
–For each pixel drawn, Depth Testcompare the current
depth stored in the depth buffer with the depth of the
new pixel to draw.
–a pixel is drawn or not depending on result of depth test.
–We usually use the depth buffer to draw nearest pixels
(p1), pixels behind are not drawn (p2).
viewing volume
p1
p2
25
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•More aboutBuffer?
–http://jerome.jouvie.free.fr/OpenGl/Lesson
s/Lesson5.php
26
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•For now, we'll ignore this.
•It'll make sense after the next lesson.
27
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•Draw the trapezoid (Begin the substance of our
program).
–CallglBegin(GL_QUADS)to tell OpenGL that we want
to start drawing quadrilaterals.
–Specify the four 3D coordinates of the vertices of the
trapezoid, in order, using calls to glVertex3f.
–After drawing quadrilaterals, callglEnd().
•Note that every call toglBeginmust have a matching call
toglEnd.
(-0.7f,-1.5f, -5.0f)(0.7f, -1.5f, -5.0f)
(0.4f, -0.5f, -5.0f)
(-0.4f, -0.5f, -5.0f)
28
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•Draw the pentagon.
–To draw it, we split it up into three triangles, which is
pretty standard for OpenGL.
–Calling glBegin(GL_TRIANGLES)to tell OpenGL
that let us begin to draw triangles.
–Specify coordinates of the vertices.
–OpenGL automatically puts the coordinates together
in groups of three.
–Each group of three coordinates represents one
triangle.
29
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•Finally, we draw the triangle.
•We haven't calledglEnd()to tell OpenGL
that we're done drawing triangles yet, so it
knows that we're still giving it triangle
coordinates.
30
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•Finish drawing triangles -> callglEnd().
•Note that we could have drawn the four triangles using
four calls toglBegin(GL_TRIANGLES)and four
accompanying calls toglEnd(). However, this makes the
program slower, and you shouldn't do it.
•There are other things we can pass toglBeginin
addition toGL_TRIANGLESandGL_QUADS, but
triangles and quadrilaterals are the most common things
to draw.
31
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•This line makes OpenGL actually move
the scene to the window.
•We'll call it whenever we're done drawing
a scene.
32
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•main function.
–start by initializing GLUT.
–In the call toglutInitWindowSizeand set the window
to be 400x400.
–CallglutCreateWindowto tell OpenGL what title we
want for the window.
–CallinitRenderingto initialize OpenGL rendering.
33
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•Point GLUT to the functions to handle
keypressesand drawing and resizing the
window.
•One important thing:
–we're not allowed to draw anything except
inside thedrawScenefunction that we
explicitly give to GLUT
34
C O M P U T E R G R A P H I CS
Jie chen
Going Through the Source Code
•CallglutMainLoop, which tells GLUT to do its thing.
–capture key and mouse input
–draw the scene by calling ourdrawScenefunction
–do some other stuff
•glutMainLooplike a defective boomerang, never returns.
–GLUT just takes care of the rest of our program's execution.
•After the call, return 0so that the compiler doesn't
complain about themainfunction not returning anything,
but the program will never get to that line.
•And that's how our first OpenGL program works.
35
C O M P U T E R G R A P H I CS
Jie chen
More details about this example
36
C O M P U T E R G R A P H I CS
Jie chen
OpenGL function format
glVertex4f(x,y,z,w)
belongs to GL library
function name
x,y,z,warefloats
glVertex4fv(p)
pis a pointer to an array
dimensions
gl: a OpenGL function
glu: a GLU (GL Utility) function
glut: a GLUT (GL Utility Toolkit)
function
37
C O M P U T E R G R A P H I CS
Jie chen
OpenGL function format
glVertex4f(x,y,z,w)
•x,yand z are coordinates and w is a factor, so the
coordinates is equivalent to (x/w, y/w, z/w).
•The default values of z and w are z =0and w=1.
•For examples:
glVertex4f(1,2,3, 3)-> glVertex4f(1/3,2/3,1, 1)
glVertex2f(1,2)-> glVertex4f(1, 2, 0, 1)
glVertex3f(1,2,3)-> glVertex4f(1,2,3, 1)
i.e., z =0and w=1
i.e., w=1
38
C O M P U T E R G R A P H I CS
Jie chen
OpenGL function format
glVertex3f(x,y,z)
39
C O M P U T E R G R A P H I CS
Jie chen
OpenGL Primitives
GL_POINTS
GL_POINTS
GL_LINES
GL_LINES
GL_LINE_LOOP
GL_LINE_LOOP
GL_LINE_STRIP
GL_LINE_STRIP
40
C O M P U T E R G R A P H I CS
Jie chen
OpenGL Primitives
GL_QUAD_STRIP
GL_QUAD_STRIP
GL_POLYGON
GL_POLYGON
GL_TRIANGLE_STRIP
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
GL_TRIANGLE_FAN
GL_TRIANGLES
GL_TRIANGLES
41
C O M P U T E R G R A P H I CS
Jie chen
Polygon Issues
•OpenGL will only display polygons correctly that are
–Simple
: edges cannot cross
–Convex
: All points on line segment between two points in
a polygon are also in the polygon
–Flat
: all vertices are in the same plane
•User program can check if above true
–OpenGL will produce output if these conditions are
violated but it may not be what is desired
•Triangles satisfy all conditions
nonsimplepolygon
nonconvexpolygon
p1
p2
42
C O M P U T E R G R A P H I CS
Jie chen
Polygon Issues
•How can we plot those polygons which do not satisfy these
conditions?
–nonsimplepolygon: edges DOcross
–nonconvexpolygon : There arepoints on line segment between two
points in a polygon areNOTin the polygon
–Flat
: all vertices areNOTin the same plane
•Solution: divide them using Trianglesbecause triangles satisfy all
conditionsor quadrangle
nonsimplepolygon
nonconvexpolygon
43
C O M P U T E R G R A P H I CS
Jie chen
Polygon Issues
•Subdividing
–to Improve a Polygonal Approximation to a
Surface using approximating triangles
20triangles
80triangles320triangles
44
C O M P U T E R G R A P H I CS
Jie chen
Polygon Issues
•Do something huge!
Demo
45
C O M P U T E R G R A P H I CS
Jie chen
Polygon Issues
•Do something huge!
46
C O M P U T E R G R A P H I CS
Jie chen
Hints for polygonizingsurfaces
•Keep polygon orientations consistent
–all clockwise or all counterclockwise
–important for polygon culling and two-sided
lighting
•Watch out for any nontriangularpolygons
–three vertices of a triangle are always on a
plane; any polygon with four or more vertices
might not
47
C O M P U T E R G R A P H I CS
Jie chen
Hints for polygonizingsurfaces
•There's a trade-off between the
display speed and the image quality
–few polygons render quickly but might
have a jagged appearance; millions of
tiny polygons probably look good but
might take a long time to render
–use large polygons where the surface is
relatively flat, and small polygons in
regions of high curvature
•Avoid T-intersections in our models
–there's no guarantee that the line
segments AB and BC lie on exactly the
same pixels as the segment AC
–this can cause cracks to appear in the
surface
48
C O M P U T E R G R A P H I CS
Jie chen
Some terms
•Rendering: the process by which a computer creates
images from models.
•model, or object: constructed from geometric
primitives -points, lines, and polygons -that are
specified by their vertices.
•pixel: the smallest visible element that the display
hardware can put on the screen. The final rendered
image consists of pixels drawn on the screen.
•Bitplane: an area of memory that holds one bit of
information (for instance, what color it is supposed to
be) for every pixel on the screen.
•framebuffer: Organized by the bitplanes. It holds all
the information that the graphics display needs to
control the color and intensity of all the pixels on the
screen.
pixel
49
C O M P U T E R G R A P H I CS
Jie chen
24-bit true color
pixel
Bitplane
DAC: digital-to-analog converter
0 1 0 0 1 0 1 1
1 0 1 0 1 1 0 0
0 0 0 0 1 0 1 0
8 bit DAC
8 bit DAC
8 bit DAC
registers
Blue 75
Green 172
Red 10
Color Guns
Frame Buffer
CRT Raster
8
8
8
50
C O M P U T E R G R A P H I CS
Jie chen
•The end of this lecture!