Académique Documents
Professionnel Documents
Culture Documents
Angel/Shreiner/Mller
Output device
Input devices
Image formed and stored in frame buffer
Machiraju/Zhang/Mller
Cmpt 361
Torsten Mller
The algorithm
for each polygon in the scene
project its vertices onto viewing (image) plane
for each pixel inside the polygon formed on viewing plane
determine point on polygon corresponding to this pixel
get pixel color according to some illumination model
get depth value for this pixel (distance from point to plane)
if depth value < stored depth value for the pixel
update pixel color in frame buffer
update depth value in depth buffer (z-buffer)
end if
Question: What does the depth buffer store after running the z-buffer
end if
algorithm for the scene? Does polygon order change depth? Image?
Machiraju/Zhang/Mller
Angel/Shreiner/Mller
Development of the
OpenGL API
Angel/Shreiner/Mller
Core
Both 2D and 3D
PHIGS and X
Programmers Hierarchical Graphics System
(PHIGS)
Arose from CAD community
Database model with retained graphics (structures)
X Window System
DEC/MIT effort
Client-server architecture with graphics
SGI and GL
Silicon Graphics (SGI) revolutionized the
graphics workstation by implementing the
pipeline in hardware (1982)
To access the system, application programmers
used a library called GL
With GL, it was relatively simple to program
three dimensional interactive applications
Angel/Shreiner/Mller
10
OpenGL
The success of GL lead to OpenGL (1992), a
platform-independent API that was
Easy to use
Close enough to the hardware to get excellent
performance
Focus on rendering
Omitted windowing and input to avoid window
system dependencies
Angel/Shreiner/Mller
11
OpenGL Evolution
Originally controlled by an Architectural
Review Board (ARB)
Members included SGI, Microsoft, Nvidia, HP,
3DLabs, IBM,
Now Khronos Group
Was relatively stable (through version 2.5)
Backward compatible
Evolution reflected new hardware capabilities
3D texture mapping and texture objects
Vertex and fragment programs
12
OpenGL Architecture
Angel/Shreiner/Mller
13
Modern OpenGL
Performance is achieved by using GPU rather
than CPU
Control GPU through programs called shaders
Applications job is to send data to GPU
GPU does all rendering
Angel/Shreiner/Mller
14
http://gridtalk-project.blogspot.com/2010/07/future-of-computing-gpgpu.html
Angel/Shreiner/Mller
15
OpenGL 3.1
Totally shader-based
No default shaders
Each application must provide both a vertex and a
fragment shader
No immediate mode
Few state variables
Most 2.5 functions deprecated
Backward compatibility not required
Angel/Shreiner/Mller
16
Other Versions
OpenGL ES
Embedded systems
Version 1.0 simplified OpenGL 2.1
Version 2.0 simplified OpenGL 3.1
Shader based
WebGL
Javascript implementation of ES 2.0
Supported on newer browsers
17
Disadvantages
New versions not backward compatible
Windows only
18
OpenGL Libraries
OpenGL core library
OpenGL32 on Windows
GL on most unix/linux systems (libGL.a)
19
GLUT
OpenGL Utility Toolkit (GLUT)
Provides functionality common to all window
systems
Open a window
Get input from mouse and keyboard
Menus
Event-driven
20
Graphics libraries
GLUI: GLUT-based user interface library
(libglui)
Offer controls to OpenGL applications, e.g.,
buttons,
checkboxes,
radio buttons,
etc.
Angel/Shreiner/Mller
21
freeglut
GLUT was created long ago and has been
unchanged
Amazing that it works with OpenGL 3.1
Some functionality cant work since it requires
deprecated functions
Angel/Shreiner/Mller
22
GLEW
OpenGL Extension Wrangler Library
Makes it easy to access OpenGL extensions
available on a particular system
Avoids having to have specific entry points in
Windows code
Application needs only to include glew.h and
run a glewInit()
Angel/Shreiner/Mller
23
Software Organization
Angel/Shreiner/Mller
24
OpenGL is an API
Application Programmers Interface:
a link between
low-level: graphics hardware
high-level: application program you write
25
OpenGL Functions
Angel/Shreiner/Mller
26
OpenGL Functions
Primitives
Points
Line Segments
Triangles
Attributes
Transformations
Viewing
Modeling
Control (GLUT)
Input (GLUT)
Query
Angel/Shreiner/Mller
27
OpenGL State
OpenGL is a state machine
OpenGL functions are of two types
Primitive generating
Can cause output if primitive is visible
How vertices are processed and appearance of
primitive are controlled by the state
State changing
Transformation functions
Attribute functions
Under 3.1 most state variables are defined by the
application and Angel/Shreiner/Mller
sent to the shaders
28
29
dimensions
glUniform3f(x,y,z)
x,y,z are floats
belongs to GL library
glUniform3fv(p)
p is a pointer to an array
Angel/Shreiner/Mller
30
OpenGL #defines
Most constants are defined in the include files
gl.h, glu.h and glut.h
Note #include <GL/glut.h> should
automatically include the others
Examples
glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT)
31
Angel/Shreiner/Mller
32
GLSL
OpenGL Shading Language
C-like with
Matrix and vector types (2, 3, 4 dimensional)
Overloaded operators
C++ like constructors
33
Today
Development of the OpenGL API
OpenGL Architecture
OpenGL as a state machine
OpenGL as a data flow machine
Functions
Types
Formats
Simple program
Shaders
Readings: Sections 2.1-2.10
Angel/Shreiner/Mller
Colours and attributes
34
Angel/Shreiner/Mller
35
Objectives
Build a complete first program
Introduce shaders
Introduce a standard program structure
Simple viewing
Two-dimensional viewing as a special case of
three-dimensional viewing
Angel/Shreiner/Mller
36
Program Structure
Most OpenGL programs have a similar structure that consists of
the following functions
main():
enters event loop (last executable statement)
initShader():
create geometry
read, compile and link shaders
callbacks
Display function
Angel/Shreiner/Mller
Input and window functions
37
main.c
#include <GL/glew.h>
#include <GL/glut.h>
includes gl.h
38
GLUT functions
glutInit allows application to get command line arguments
and initializes system
gluInitDisplayMode requests properties for the window
(the rendering context)
RGB color
Single buffering
Properties logically ORed together
glutInitWindowSize in pixels
glutInitWindowPosition from top-left corner of
display
glutCreateWindow create window with title simple
glutDisplayFunc display callback
Angel/Shreiner/Mller
39
Immediate mode
Each time a vertex is specified in application, its
location is sent to the GPU
Old style uses glVertex
Creates bottleneck between CPU and GPU
Removed from OpenGL 3.1
Angel/Shreiner/Mller
40
Angel/Shreiner/Mller
41
Display Callback
Once we get data to GLU, we can initiate the
rendering with a simple callback
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();
}
42
Vertex Arrays
Vertices can have many attributes
Position
Color
Texture Coordinates
Application data
43
44
Buffer Object
Buffer objects allow us to transfer large
amounts of data to the GPU
Need to create, bind, and identify data
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,
sizeof(points), points, GL_STATIC_DRAW);
45
Initialization
Vertex array objects and buffer objects can be
set up in init()
Set clear color and other OpenGL parameters
Set up shaders as part of initialization
Read
Compile
Link
46
Coordinate Systems
The units in points are determined by the application and
are called
object (or model) coordinates
world coordinates
projection transform
47
OpenGL Camera
OpenGL places a camera at the origin in object
space pointing in the negative z direction
The default viewing volume
is a box centered at the
origin with sides of
length 2
Angel/Shreiner/Mller
48
Orthographic Viewing
In the default orthographic view, points are
projected forward along the z axis onto the
plane z=0
z=0
z=0
Angel/Shreiner/Mller
49
Viewports
Do not have to use the entire window for the
image: glViewport(x,y,w,h)
Values in pixels (window coordinates)
Angel/Shreiner/Mller
50
51
OpenGL Shaders
Angel/Shreiner/Mller
Objectives
Simple Shaders
Vertex shader
Fragment shaders
Angel/Shreiner/Mller
53
Lighting
More realistic models
Cartoon shaders
Angel/Shreiner/Mller
54
smooth shading
environment
mapping
Angel/Shreiner/Mller
bump mapping
56
Writing Shaders
First programmable shaders were programmed
in an assembly-like manner
OpenGL extensions added for vertex and
fragment shaders
Cg (C for graphics) C-like language for
programming shaders
Works with both OpenGL and DirectX
Interface to OpenGL complex
57
GLSL
58
Angel/Shreiner/Mller
59
Angel/Shreiner/Mller
60
Execution Model
Application
Program
Vertex data
Shader Program
Shader Program
glDrawArrays
GPU
Fragment
Fragment
Color
Vertex
Angel/Shreiner/Mller
Data Types
C types: int, float, bool
Vectors:
float vec2, vec3, vec4
Also int (ivec) and boolean (bvec)
62
Pointers
There are no pointers in GLSL
We can use C structs which
can be copied back from functions
Angel/Shreiner/Mller
63
Qualifiers
GLSL has many of the same qualifiers such as
const as C/C++
Need others due to the nature of the execution model
Variables can change
64
Uniform Qualified
Variables that are constant for an entire
primitive
Can be changed in application and sent to
shaders
Cannot be changed in shader
Used to pass information to shader such as the
bounding box of a primitive
Angel/Shreiner/Mller
65
Attribute Qualifier
Attribute-qualified variables can change at
most once per vertex
There are a few built in variables such as
gl_Position but most have been
deprecated
User defined (in application program)
Use in qualifier to get to shader
in float temperature
in vec3 velocity
Angel/Shreiner/Mller
66
Varying Qualified
Variables that are passed from vertex shader to
fragment shader
Automatically interpolated by the rasterizer
Old style used the varying qualifier
varying vec4 color;
67
Angel/Shreiner/Mller
68
Angel/Shreiner/Mller
69
Passing values
call by value-return
Variables are copied in
Returned values are copied back
Three possibilities
in
out
inout (deprecated)
Angel/Shreiner/Mller
70
71
72
Angel/Shreiner/Mller
73
Objectives
Angel/Shreiner/Mller
74
OpenGL Primitives
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
Angel/Shreiner/Mller
75
Polygon Issues
OpenGL will only display triangles
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
nonconvex polygon
76
Polygon Testing
Conceptually simple to test for simplicity and
convexity
Time consuming
Earlier versions assumed both and left testing
to the application
Present version only renders triangles
Need algorithm to triangulate an arbitrary
polygon
Angel/Shreiner/Mller
77
78
Triangularization
d
Convex polygon
c
79
Attributes
Attributes determine the appearance of objects
80
RGB colour
Each colour component is stored separately in
the frame buffer
Usually 8 bits per component in buffer
Colour values can range from 0.0 (none) to 1.0
(all) using floats or over the range from 0 to
255 using unsigned bytes
Angel/Shreiner/Mller
81
Indexed Colour
Colours are indices into tables of RGB values
Requires less memory
indices usually 8 bits
not as important now
Memory inexpensive
Need more colors for shading
Angel/Shreiner/Mller
82
Smooth Colour
Default is smooth shading
OpenGL interpolates vertex colors across visible
polygons
Angel/Shreiner/Mller
83
Setting Colours
Colours are ultimately set in the fragment
shader but can be determined in either shader
or in the application
Application colour: pass to vertex shader as a
uniform variable or as a vertex attribute
Vertex shader color: pass to fragment shader as
varying variable
Fragment color: can alter via shader code
Angel/Shreiner/Mller
84
More GLSL
Angel/Shreiner/Mller
85
Objectives
Coupling shaders to applications
Reading
Compiling
Linking
Vertex Attributes
Setting up uniform variables
Example applications
Angel/Shreiner/Mller
86
Read shaders
Compile shaders
Create a program object
Link everything together
Link variables in application with variables in
shaders
Vertex attributes
Uniform variables
Angel/Shreiner/Mller
87
Program Object
Container for shaders
Can contain multiple shaders
Other GLSL functions
GLuint myProgObj;
myProgObj = glCreateProgram();
/* define shader objects here */
glUseProgram(myProgObj);
glLinkProgram(myProgObj);
Angel/Shreiner/Mller
88
Vertex Attributes
Vertex attributes are named in the shaders
Linker forms a table
Application can get index from table and tie it
to an application variable
Similar process for uniform variables
Angel/Shreiner/Mller
89
Angel/Shreiner/Mller
90
Angel/Shreiner/Mller
91
Double Buffering
Updating the value of a uniform variable opens
the door to animating an application
Execute glUniform in display callback
Force a redraw through
glutPostRedisplay()
92
Swap buffers
void mydisplay()
{
glClear();
glDrawArrays();
glutSwapBuffers();
}
Angel/Shreiner/Mller
93
Idle Callback
Idle callback specifies function to be executed
when no other actions pending
glutIdleFunc(myIdle);
void myIdle()
{
// recompute display
glutPostRedisplay();
}
Angel/Shreiner/Mller
94
Adding Color
If we set a color in the application, we can
send it to the shaders as a vertex attribute or as
a uniform variable depending on how often it
changes
Lets associate a color with each vertex
Set up an array of same size as positions
Send to GPU as a vertex buffer object
Angel/Shreiner/Mller
95
Setting Colors
typedef vec3 color3;
color3 base_colors[4] = {color3(1.0, 0.0. 0.0),
.
color3 colors[NumVertices];
vec3 points[NumVertices];
//in loop setting positions
colors[i] = basecolors[color_index]
position[i] = .
Angel/Shreiner/Mller
96
97
Angel/Shreiner/Mller
98