Title: Computer Graphics
1Computer Graphics
2OpenGL
- Most Widely Adopted Graphics Standard
- High Visual Quality and Performance
- Industry standard, Reliable and portable
- Stable, Easy to use and Well-documented
3OpenGL software libraries
4Software libraries
- OpenGL provides most of the graphics
functionality - GLU provides support for some additional
operations and primitive types, and is
implemented using OpenGL function calls - glut designed specifically to be used with
OpenGL and it takes care of things like opening
windows, redraw events, and keyboard and mouse
input. It effectively hides all the windowing
system dependencies for OpenGL
5Installing OpenGL for Windows
- bundled with Win98, Win95, WinNT or download it
- look for dynamically-linked libraries (dlls)
- C\Winnt\system32\Opengl32.dll on Windows NT
- C\Windows\system\Opengl32.dll on Windows 95
- look for header files, e.g., for Visual C
- C\Program Files\Microsoft Visual
Studio\VC98\include\gl\.h
6Installing glut for Windows
- http//www.opengl.org/developers/documentation/glu
t.html?glutfirst_hit - glut.h
- C\Program Files\Microsoft Visual
Studio\VC98\include\gl\glut.h - glut32.lib
- C\Program Files\Microsoft Visual
Studio\VC98\lib\glut32.lib - glut32.dll
- C\Windows\system32\glut32.dll on Windows 95, 98
- C\WINNT\system32\glut32.dll on Windows 2000, NT
7Compiling with Visual C
- Start Visual C
- Choose File ? new, then from the Projects tab
select Win32 Console Application - Assign the project a name (lab00' for example),
and choose a directory where the project files
will be stored - Choose File ? new, then from the Files tab
select C Source File - Assign the file a name, lab00' for example. The
.cpp extension will be added for you. - Fill in the file lab00.cpp
8lab00.cpp
- // lab00.cpp, Computer Graphics,
lbg_at_dongseo.ac.kr - include ltGL/glut.hgt
- void myDisplay(void)
-
- glClearColor (.75, .75, .75, 0.0)
- glMatrixMode(GL_PROJECTION)
- glOrtho(0, 500, 0, 500, -1, 1)
- glMatrixMode(GL_MODELVIEW)
- glClear(GL_COLOR_BUFFER_BIT)
- glColor3f(0.60, .40, .70)
- glBegin(GL_POLYGON)
- glVertex2i(450, 250)
- glVertex2i(412, 367)
- glVertex2i(312, 440)
- glVertex2i(188, 440)
- glVertex2i(88, 368)
- glVertex2i(50, 250)
- glVertex2i(88, 132)
void main(int argc, char argv) glutInit(argc
,argv) glutInitDisplayMode (GLUT_SINGLE
GLUT_RGB) glutInitWindowSize(500,500) glutInit
WindowPosition(0,0) glutCreateWindow("lab00")
glutDisplayFunc(myDisplay) glutMainLoop()
9Compiling with Visual C
- Choose Project ? Settings and select the C/C
panel. Enable Generate browse info. - Select the Link panel. At the beginning (or
anywhere) of the Object/library modules entry,
add the libraries opengl32.lib and glut32.lib.
Accept all the project setting changes by
choosing OK.
10Compiling with Visual C
- Choose Build ? Build lab00.exe, or simply hit the
F7 key in order to compile and link. - A successful compilation would have produced the
following output in your 'build' window - ------------Configuration lab00 - Win32
Debug----------- Compiling... lab00.cpp
Linking... lab00.exe - 0 error(s), 0 warning(s)
11Compiling with Visual C
- If there are errors, hitting F4 will take you to
the location of the first error message in the
code, and thereafter always take you to the next
error message. Or, one can double click on any
error message to jump to the relevant source
code. - Once the code has successfully compiled and
linked, Build-Execute or Ctrl-F5 will run the
code, as will double-clicking on the test.exe
icon (found in the Debug directory) from a file
browsing window.
12Basic drawing
- OpenGL is a "state" machine Once it is in a
state, it stays in that state until the state is
changed. - Basic commands
- glClearColor(red, green, blue, 0.0)
- // sets background color
- glClear(GL_COLOR_BUFFER_BIT)
- // clears the window to the background color
- glColor3f(red, green, blue)
- // values between 0.0 (no color) 1.0 (full
color) - glBegin()
- // determines how vertices are interpreted
13Basic drawing
- glVertex2i(412, 367)
- glVertex2i(312, 440)
- glVertex2i(188, 440)
- glVertex2i(88, 368)
- glVertex2i(50, 250)
- glVertex2i(88, 132)
- glVertex2i(188, 60)
- glVertex2i(312, 60)
- glVertex2i(412, 132)
- glEnd()
- // clears the state initiated by
14Colors
- 1.0f, 0.0f, 0.0f //red
- 0.0f, 1.0f, 0.0f //green
- 0.0f, 0.0f, 1.0f //blue
- 1.0f, 1.0f, 0.0f //yellow
- 1.0f, 0.0f, 1.0f //magenta
- 0.0f, 1.0f, 1.0f //cyan
- 1.0f, 1.0f, 1.0f //white
- .25f, .25f, .25f //dark gray
- .75f, .75f, .75f //light gray
- .60f, .40f, .12f //brown
- .60f, .40f, .70f //barney purple
- .98f, .625f, .12f //pumpkin orange
- .98f, .04f, .70f //pastel pink
15Drawing primitives
- OpenGL supports several basic primitive types,
including points, lines, quadrilaterals, and
general polygons. All of these primitives are
specified using a sequence of vertices. - glBegin and glEnd delimit the vertices that
define a primitive or a group of like primitives.
- GL_POINTS Treats each vertex as a single point.
Vertex n defines point n. N points are drawn.
16Drawing primitives
- GL_LINES Treats each pair of vertices as an
independent line segment. Vertices 2n-1 and 2n
define line n. N/2 lines are drawn. - GL_LINE_STRIP Draws a connected group of line
segments from the first vertex to the last.
Vertices n and n1 define line n. N-1 lines are
drawn. - GL_LINE_LOOP Draws a connected group of line
segments from the first vertex to the last, then
back to the first. Vertices n and n1 define line
n. The last line, however, is defined by vertices
N and 1. N lines are drawn.
17Drawing primitives
- GL_TRIANGLES Treats each triplet of vertices as
an independent triangle. Vertices 3n-2, 3n-1, and
3n define triangle n. N/3 triangles are drawn. - GL_TRIANGLE_STRIP Draws a connected group of
triangles. One triangle is defined for each
vertex presented after the first two vertices.
For odd n, vertices n, n1, and n2 define
triangle n. For even n, vertices n1, n, and n2
define triangle n. N-2 triangles are drawn. - GL_TRIANGLE_FAN Draws a connected group of
triangles. One triangle is defined for each
vertex presented after the first two vertices.
Vertices 1, n1, and n2 define triangle n. N-2
triangles are drawn.
18Triangle
19Drawing primitives
- GL_QUADS Treats each group of four vertices as an
independent quadrilateral. Vertices 4n-3, 4n-2,
4n-1, and 4n define quadrilateral n. N/4
quadrilaterals are drawn. - GL_QUAD_STRIP Draws a connected group of
quadrilaterals. One quadrilateral is defined for
each pair of vertices presented after the first
pair. Vertices 2n-1, 2n, 2n2, and 2n1 define
quadrilateral n. N/2-1 quadrilaterals are drawn.
Note that the order in which vertices are used to
construct a quadrilateral from strip data is
different from that used with independent data. - GL_POLYGON Draws a single, convex polygon.
Vertices 1 through N define this polygon.
20Quad
21Basic primitives
22Basic primitives
- GL_LINE_LOOP, GL_TRIANGLES
23Basic primitives
- GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
24Basic primitives
25OpenGL Naming Convension
- gl gl library
- Vertex command
- 2f number and type of parameters
- OpenGL Data Type Presentation C-language
- GLbyte 8-bit integer signed char b
- GLshort 16-bit integer short s
- GLint, GLsizei 32-bit integer long l
- GLfloat,GLclampf 32-bit float float f
- GLdouble,GLclampd 64-bit float double d
- GLubyte,GLboolean 8-bit unsigned unsigned
char ub - GLushort 16-bit unsigned unsigned short us
- GLuint,GLenum 32-bit unsigned unsigned long ui
26- glVertex234b s i f d ub us uiv()
- // allows 2 (x,y), 3 (x,y,z), or 4 (x,y,z,w)
values - // allows the following data types
- // b signed char 8 bit integer
- // s short 16 bit integer
- // i integer 32 bit integer
- // f float 32 bits
- // d double 64 bits
- // ub unsigned byte 8 bits
- // us unsigned short 16 bits
- // ui unsigned int 32 bits
- // optional v ? defined in an array (vector)
27lab01.cpp
- // lab01.cpp, Computer Graphics,
lbg_at_dongseo.ac.kr - include ltstdio.hgt
- include ltstdlib.hgt
- include ltGL/glut.hgt
- GLint windowWidth, windowHeight, N4
- void myAxis(void)
-
- int i
- glColor3f(0.98, .04, .70)
- glBegin(GL_LINES)
- for(i1 iltN i)
- glVertex2i(iwindowWidth/N, 0)
- glVertex2i(iwindowWidth/N, windowHeight)
- glVertex2i(0, iwindowHeight/N)
- glVertex2i(windowWidth, iwindowHeight/N)
-
void myDraw(void) glColor3f(0.60, .40, .70)
glBegin(GL_POLYGON) glVertex2i(windowWidth/N,
windowHeight/N) glVertex2i(3windowWidth/N,
windowHeight/N) glVertex2i(3windowWidth/N,
3windowHeight/N) glVertex2i(windowWidth/N,
3windowHeight/N) glEnd() void
myDisplay(void) glClear(GL_COLOR_BUFFER_BIT)
myAxis() myDraw() glFlush()
28lab01.cpp
- void myReshape(int width, int height)
-
- glClearColor (.75, .75, .75, 0.0)
- glViewport(0,0,width,height)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glOrtho(0, width, 0, height, -1, 1)
- windowWidthwidth
- windowHeightheight
- glMatrixMode(GL_MODELVIEW)
-
- void main(int argc, char argv)
-
- glutInit(argc,argv)
- glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
- glutInitWindowSize(500,500)
- glutInitWindowPosition(0,0)
- glutCreateWindow("lab01")
29Lab 01
30Lab 0101, 0102
31Lab 0103, 0104
32Lab 0105, 0106
33Lab 0107, 0108
34Lab 0109