Programming Basics in OpenGL

blareweyrSoftware and s/w Development

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

110 views

Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 1
Programming Basics in OpenGL
Basic Structure
OpenGL is not object oriented!It has a global state organized as a tree-like
stack via ‘push’ and ‘pop’ separators.Everything you see on the screen is a
function of the objects and attributes you feed into the Global State.
image = GlobState(attributes;objects)
The display will continue with the attributes you define until interupted.
Major Components
• Primitives (Objects):what can be seen - points,line segments,polygons,
pixels,curves
• Attributes:Maintained as system states,determine how objects are dis-
played - color,current,depth buffer,accum buffer,eval,fog,hint,light-
ing,line,list,pixel mode,point,polygon,polygon stipple,scissor,stencil
buffer,texture,transform,viewport
• Display:Controlled by two matrix stacks -
glMatrixMode(GL
PROJECTION) Viewing:projection,clipping
glMatrixMode(GL
MODELVIEW) Transformation:rotate,scale,trans-
late
• Events:input,error handling,network connection,etc.
Sample Code
1bufftst.c
Beginning program to compile and test your
hardware configuration
hello.c
Introductory program from The Red Book
A great resource:
http://www.lighthouse3d.com ⇒
Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 2
Primitives
Objects
glBegin(GL
SOMETHING)...glEnd(GL
SOMETHING)
Connectivity between vertices depends on the type of primitive being built.
Example:
glClear()
glColor3f()
glBegin (GL_POINTS)
glVertex3f()
glEnd (GL_POINTS)
glFlush()
p
2
p
1
p
0
p
7
p
6
GL_TRIANGLESGL_QUADSGL_POLYGONGL_POINTS
p
3
p
4
p
5
p
2
p
1
p
0
p
7
p
6
p
3
p
4
p
5
p
2
p
1
p
0
p
7
p
6
p
3
p
4
p
5
p
2
p
1
p
0
p
7
p
6
p
3
p
4
p
5
p
1
GL_POINTS
p
3
p
5
p
7
p
0
p
2
p
4
p
6
p
1
GL_QUAD_STRIP
p
3
p
5
p
7
p
0
p
2
p
4
p
6
p
1
GL_TRIANGLE_STRIP
p
3
p
5
p
7
p
0
p
2
p
4
p
6
Ideal usage of GL
POLYGON is to form polygons that are simple,convex,and
flat.Simple polygons have no holes or self-intersections.If not simple,there
are issues when rendering:
Scanline Scanline
(a) (b)
How do we decide what to fill in?
Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 3
Text
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Computer Graphics
Raster Text vs.Stroke Text
• Raster Text (also Bitmap Text) deals with specifying pixels to draw to
the screen
– glPointSize(pixels) has screen raster dependence

– glRasterPos determines the placement in window

• Stroke Text (also Geometric Text) forms letters using geometric primitives
(lines,points,etc.) so they can be transformed easier than raster text.
– glutStrokeCharacter(font,char) You’re limited to two different font
choices

Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 4
Attributes
Attributes are bound to primitives.The binding may change.
The present attribute values are part of the state of the graphics system.
￿
￿
Color - Additive & Subtractive
Blue
Red
Green
Yellow
Cyan
Magenta
(a) (b)
Magenta
Cyan
Yellow
White
Green
Red
Blue
Black
Cyan
Blue
Green Yellow
Red
Magenta
White
Black
R
G
B
• glColor(r,g,b) Defines attribute that is bound to primitives

• glClearColor(r,g,b,α) Determines background color

• Each allow 2
24
colors
• 1280 ×1024 pixels with 24 bits (RGB) + α > 4  2
20
bytes = 4MB.
• Color gamut
• Hue,Saturation,Intensity model
Applet
Additive Color Mixing
How red,blue,and green are mixed to produce
color on the screen.
Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 5
Definition of Viewing Space
Properties of the clipping and viewing volume
• Use glOrtho( left,right,bottom,top,near,far ) to specify volume

• Camera always looks down the negative z-axis.
• Camera ‘views’ everything in the viewing volume (frustum) – also objects
‘behind’ the camera.
• glOrtho2D( left,right,bottom,top) = glOrtho( left,right,bottom,top,
1,-1 )
Viewing Frustum Window
(0,0)
(-5,-5,5)
(0,0)
R
3
N
2
OpenGL and GLUT
OpenGL is designed to work across platforms,and thus does not handle any
OS specific calls to display windows.We use GLUT(OpenGL Utility Toolkit)
to handle I/O.See
glut.h
• glutInitWindowSize()

How large is the window?Note:image may be
smaller than window
x
y
w
h
Viewport
Graphics window
Clipping window
Distortion occurs if width/height ratio of glOrtho 6= glutInitWindowSize
h
w
Jorg’s Graphics Lecture Notes – Programming Basics in OpenGL 6
w and h are dynamic variables within the global state
Remedy:
glViewport( lowerleft
x,lowerleft
y,w,h )
glOrtho( left*(w/h),right*(w/h),up,down,near,far )
• glutInitWindowPosition() Where on the screen does the image appear?
Note there are two distinct coordinate systems at work:
2D Device Coordinates
Physical
Origin at Top Left
(also Raster or Screen Coordinates)
3D World Coordinates
Mathematical
Origin defined by viewport
• Other coordinate systems to be aware of:
– OpenGL scene:(0,0) lower left,x right,y up
– X windows:(0,0) upper left,x right,y down
– ASCII:(0,0) upper left,x down,y right (landscape)
Event Processing
• Immediate mode:Primitives rendered as soon as defined.Present system
state determines appearance.
• glutMainLoop() begins the event processing loop

• glutDisplayFunc(*func) defines the function called initially and when mov-
ing windows

• glutIdleFunc(*func) defines the function called while no events are occur-
ing