Introduction To Modern Opengl Programming: Ed Angel University of New Mexico Dave Shreiner Arm, Inc
Introduction To Modern Opengl Programming: Ed Angel University of New Mexico Dave Shreiner Arm, Inc
OpenGL Programming
Ed Angel
University of New Mexico
Dave Shreiner
ARM, Inc
Agenda
What Is OpenGL?
OpenGL is a computer graphics rendering API
With it, you can generate high-quality color images
by rendering with geometric and image primitives
It forms the basis of many interactive applications
that include 3D graphics
By using OpenGL, the graphics part of your
application can be
operating system independent
window system independent
In the Beginning
OpenGL 1.0 was released on July 1st, 1994
Its pipeline was entirely fixed-function
the only operations available were fixed by the
implementation
Vertex
Vertex
Data
Data
Vertex
Vertex
Transform and
Transform
and
Lighting
Lighting
Primitive
Primitive
Setup
Setup and
and
Rasterization
Rasterization
Fragment
Fragment
Coloring
Coloring and
and
Texturing
Texturing
Blending
Blending
Texture
Texture
Store
Store
Pixel
Pixel
Data
Data
Vertex
Vertex
Transform and
Transform
and
Lighting
Lighting
Texture
Texture
Store
Store
Primitive
Primitive
Setup and
Setup
and
Rasterization
Rasterization
Fragment
Fragment
Coloring and
Coloring
and
Texturing
Texturing
Blending
Blending
An Evolutionary Change
OpenGL 3.0 introduced the deprecation model
the method used to remove features from OpenGL
The pipeline remained the same until OpenGL 3.1
(released March 24th, 2009)
Introduced a change in how OpenGL contexts are used
Context Type
Description
Full
Forward Compatible
Pixel
Pixel
Data
Data
Vertex
Vertex
Shader
Shader
Primitive
Primitive
Setup
Setup and
and
Rasterization
Rasterization
Fragment
Fragment
Shader
Shader
Blending
Blending
Texture
Texture
Store
Store
More Programability
OpenGL 3.2 (released August 3rd, 2009) added an
additional shading stage geometry shaders
Vertex
Vertex
Data
Data
Vertex
Vertex
Shader
Shader
Geometry
Geometry
Shader
Shader
Pixel
Pixel
Data
Data
Texture
Texture
Store
Store
Primitive
Primitive
Setup and
Setup
and
Rasterization
Rasterization
Fragment
Fragment
Shader
Shader
Blending
Blending
Profile
Description
core
compatible
core
compatible
Not supported
Full
Forward Compatible
Vertex
Vertex
Shader
Shader
Tessellation
Tessellation
Control
Control
Shader
Shader
Pixel
Pixel
Data
Data
Texture
Texture
Store
Store
Primitive
Primitive
Setup and
Setup
and
Rasterization
Rasterization
Tessellation
Tessellation
Evaluation
Evaluation
Shader
Shader
Geometry
Geometry
Shader
Shader
Fragment
Fragment
Shader
Shader
Blending
Blending
OpenGL Application
Development
Vertices
Vertices
Vertex
Processing
Vertex
Shader
Framebuffer
Fragments
Rasterizer
Pixels
Fragment
Processing
Fragment
Shader
positional coordinates
colors
texture coordinates
any other data associated with that point in space
x
y
z
w
GL_POINTS GL_LINES
GL_LINE_STRIP
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_LINE_LOOP
GL_TRIANGLE_FAN
A First Program
vec4
vec4
point4;
color4;
points[NumVertices];
colors[NumVertices];
Cube Data
// Vertices of a unit cube centered at origin, sides aligned with
axes
point4 vertex_positions[8] = {
point4( -0.5, -0.5, 0.5, 1.0
point4( -0.5, 0.5, 0.5, 1.0
point4( 0.5, 0.5, 0.5, 1.0
point4( 0.5, -0.5, 0.5, 1.0
point4( -0.5, -0.5, -0.5, 1.0
point4( -0.5, 0.5, -0.5, 1.0
point4( 0.5, 0.5, -0.5, 1.0
point4( 0.5, -0.5, -0.5, 1.0
};
),
),
),
),
),
),
),
)
Cube Data
// RGBA colors
color4 vertex_colors[8] = {
color4( 0.0, 0.0, 0.0, 1.0
color4( 1.0, 0.0, 0.0, 1.0
color4( 1.0, 1.0, 0.0, 1.0
color4( 0.0, 1.0, 0.0, 1.0
color4( 0.0, 0.0, 1.0, 1.0
color4( 1.0, 0.0, 1.0, 1.0
color4( 1.0, 1.0, 1.0, 1.0
color4( 0.0, 1.0, 1.0, 1.0
};
),
),
),
),
),
),
),
)
//
//
//
//
//
//
//
//
black
red
yellow
green
blue
magenta
white
cyan
VAOs in Code
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
VBOs in Code
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points) +
sizeof(colors), NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0,
sizeof(points), points );
glBufferSubData( GL_ARRAY_BUFFER, sizeof(points),
sizeof(colors), colors );
Operators
Standard C/C++ arithmetic and logic operators
Operators overloaded for matrix and vector operations
mat4 m;
vec4 a, b, c;
b = a*m;
c = m*a;
Qualifiers
in, out
Copy vertex attributes and other variable to/ from
shaders
in vec2 tex_coord;
out vec4 color;
Flow Control
if
if else
expression ? true-expression : falseexpression
while, do while
for
Functions
Built in
Arithmetic: sqrt, power, abs
Trigonometric: sin, asin
Graphical: length, reflect
User defined
Built-in Variables
in vec4 color;
out vec4 FragColor;
void m ain()
{
FragColor = color;
}
glCreateProgram()
Create
Shader
glCreateShader()
Load
Load Shader
Shader
Source
Source
glShaderSource()
Compile
Shader
glCompileShader()
and linker
A program must contain
vertex and fragment
shaders
other shaders are optional
Attach
Attach Shader
Shader
to
to Program
Program
glAttachShader()
Link Program
glLinkProgram()
Use Program
glUseProgram()
These
steps need
to be
repeated
for each
type of
shader in
the shader
program
A Simpler Way
Uniform Variables
glUniform4f( index, x, y, z, w );
GLboolean
transpose = GL_TRUE;
mat[3][4][4] = { };
Transformations
Camera Analogy
model
Modeling
Transform
Object Coords.
Vertex
Data
Model-View
Transform
World Coords.
Projection
Transform
Eye Coords.
Perspective
Division
(w)
Clip Coords.
Viewport
Transform
Normalized
Device
Coords.
2D Window
Coordinates
Viewing transformations
tripoddefine position and orientation of the viewing volume in the
world
Modeling transformations
moving the model
Viewport transformations
enlarge or reduce the physical photograph
3D Transformations
A vertex is transformed
by 44 matrices
all affine operations are
matrix multiplications
all matrices are stored
column-major in OpenGL
vector is
Mv
m0
m
1
programmers expect
m12
m4
m8
m5
m9
m2
m6
m10
m14
m3
m7
m11
m15
m13
enclosing planes
top & bottom, left & right
Viewing Transformations
Position the camera/eye in the scene
place the tripod down; aim camera
tripod
Translation
Move the origin to a new
location
1 0 0 tx
0 1 0 t
y
T (tx , ty , tz )
0 0 1 t
z
0 0 0 1
Scale
Stretch, mirror or decimate a
coordinate direction
sx
sy
sz
S( sx , sy , sz )
Rotation
0.0,
s.x,
c.x,
0.0,
0.0,
0.0,
0.0,
1.0 );
glutSwapBuffers();
Vertex Lighting
Lighting Principles
Lighting simulates how objects reflect light
material composition of object
lights color and position
global lighting parameters
Diffuse reflections
Specular reflections
Ambient light
Emission
Lighting contributors
Surface material properties
Light properties
Lighting model properties
Surface Normals
Normals define how a surface reflects light
Application usually provides normals as a vertex atttribute
Current normal is used to compute vertexs color
Use unit normals for proper lighting
scaling affects a normals length
Material Properties
Define the surface properties of a primitive
Property
Description
Diffuse
Specular
Highlight color
Ambient
Low-light color
Emission
Glow color
Shader Examples
Fragment Shaders
A shader thats executed for each potential pixel
fragments still need to pass several tests before making it
to the framebuffer
Shader Examples
Vertex Shaders
Moving vertices: height fields
Per vertex lighting: height fields
Per vertex lighting: cartoon shading
Fragment Shaders
Per vertex vs. per fragment lighting: cartoon shader
Samplers: reflection Map
Bump mapping
Height Fields
Mesh Display
Adding Lighting
Mesh Shader
uniform float time, shininess;
uniform vec4 vPosition, light_position diffuse_light, specular_light;
uniform mat4 ModelViewMatrix, ModelViewProjectionMatrix,
NormalMatrix;
void main()
{
vec4 v = vPosition;
vec4 t = sin(0.001*time + 5.0*v);
v.y = 0.1*t.x*t.z;
gl_Position = ModelViewProjectionMatrix * v;
vec4 diffuse, specular;
vec4 eyePosition = ModelViewMatrix * vPosition;
vec4 eyeLightPos = light_position ;
N
L
E
H
=
=
=
=
float Kd
float Ks
diffuse
specular
color
}
normalize(NormalMatrix * Normal);
normalize(eyeLightPos.xyz - eyePosition.xyz);
-normalize(eyePosition.xyz);
normalize(L + E);
=
=
=
=
=
Shaded Mesh
Texture Mapping
Texture Mapping
y
z
geometry
image
s
screen
Vertices
Pixels
Geometry
Pipeline
Pixel
Pipeline
Rasterizer
Fragment
Shader
Applying Textures
wrapping, filtering
Applying Textures
1.
2.
3.
4.
5.
6.
7.
8.
Texture Objects
Mapping a Texture
Based on parametric texture coordinates
coordinates needs to be specified at each
vertex
t
0, 1
Texture Space
1, 1
a
b
0, 0
Object Space
(s, t) = (0.2, 0.8)
A
(0.4, 0.2)
1, 0
C
(0.8, 0.4)
to
Texture Object
GLuint textures[1];
glGenTextures( 1, textures );
glBindTexture( GL_TEXTURE_2D, textures[0] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSize,
TextureSize, GL_RGB, GL_UNSIGNED_BYTE, image );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glActiveTexture( GL_TEXTURE0 );
Vertex Shader
in vec4 vPosition;
in vec4 vColor;
in vec2 vTexCoord;
out vec4 color;
out vec2 texCoord;
void main()
{
color
= vColor;
texCoord
= vTexCoord;
gl_Position = vPosition;
}
Fragment Shader
in vec4 color;
in vec2 texCoord;
out vec4 FragColor;
uniform sampler texture;
void main()
{
FragColor = color * texture( texture, texCoord );
}
Q&A
Thanks for Coming!
Resources
The OpenGL Programming Guide, 7th Edition
Interactive Computer Graphics: A Top-down Approach using
Resources
Thanks!