View
236
Download
1
Category
Preview:
Citation preview
OPENGL INTRODUCTION
林宏祥 2014/10/06
此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』
TODAY WE WILL TALK ABOUT…
1. Rasterization and transformation implementation
2. OpenGL progress
RASTERIZATION
3D model
2D image
RASTERIZATION
transformation
clipping
scan conversion
suited for hardware acceleration
TRANSFORMATION
=> matrix vector multiplication
=> from object space to clipping space
Space transition: a vector is multiplied by a corresponding transformation matrix.
The series of space: a definition describe the rendering process
LOADING MODEL FILETriangle
126.000000 202.500000 0.000000 -0.902861 -0.429933 -0.00000012
89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612
88.211967 209.144516 88.211967 -0.703896 0.095197 -0.70389512
Triangle
88.211967 209.144516 88.211967 -0.703896 0.095197 -0.70389512
124.242210 209.144516 0.000000 -0.995496 0.094807 -0.00000012
126.000000 202.500000 0.000000 -0.902861 -0.429933 -0.00000012
Triangle
89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612
0.000000 202.500000 126.000000 -0.000000 -0.429933 -0.90286112
0.000000 209.144516 124.242210 -0.000000 0.094807 -0.99549612
Triangle
0.000000 209.144516 124.242210 -0.000000 0.094807 -0.99549612
88.211967 209.144516 88.211967 -0.703895 0.095197 -0.70389612
89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612
object coordinate
Rotation
Translation
MOVE AND ROTATE OBJECT
world coordinate
camera coordinate
camera parameters in : eye point, reference point, up vector
ADD CAMERA
NOW WE ENTER CLIPPING SPACE
Clipping space: a viewing volume in 3D space.
The viewing volume is related to projection model.
Orthogonal Projection
-Z
X
xcamerax
cameraxx
Perspective Projection
-Z
X
xcamera
x
d
z
dz
xx camera
Viewing volume of orthogonal projection
Near Clipping Plane
Far Clipping PlaneLeft Clipping Plane
Right Clipping Plane
Bottom Clipping Plane
Top Clipping Plane
x
y
-z
(camera coordinates)
Viewing volume of perspective projection
Far Clipping Plane
Near Clipping Plane
Left Clipping Plane
Right Clipping Plane
Bottom Clipping Plane
Top Clipping Plane
x
y
-z
(camera coordinates)
What is the transformation from camera space to clipping space?
Let’s see normalized device coordinates space.
NDC SPACE
NDC space: a viewing volume in 3D space (cuboid).
x
y
z
Note: NDC is a left-hand coordinate system (because of z-buffer)
enclosed by (in OpenGL system): xNDC = 1, xNDC=-1, yNDC=1, yNDC=-1, zNDC =1, zNDC=-1
FROM CLIPPING SPACETO NDC SPACE
viewing volume in clipping space == viewing volume in NDC space after the division operation.
(Clipping coordinates is also a left-hand coordinate system)
Viewing volume of orthogonal projection
nearVal
farValleft
right
bottom
top
x
y
-z
(camera coordinates)
1
1000
200
02
0
002
1camera
camera
camera
clipping
clipping
clipping
z
y
x
nearValfarVal
nearValfarVal
nearValfarVal
bottomtop
bottomtop
bottomtop
leftright
leftright
leftright
z
y
x
1NDC
NDC
NDC
z
y
x
NDC space
A CANONICAL VIEW
xNDC0
1-1
xcamera
rightleft
Viewing volume on NDC space
Viewing Volume on camera space
2
rightleft
1)(1
0x
leftright2
leftrightx
clippingcamera
same mapping in y direction and z direction
The z component will multiply (-1) for flipping.
zFar
zNear
Viewing volume of perspective projection
(camera coordinates)
x
y
-z
)2/tan(
1
fovyf fovy: the viewing angle in y direction
Perspective Projection
-Z
X
xcamera
x
d = 1
some scaling
-Z
X
d
z
dz
xx camera
NDC
A CANONICAL VIEW..
-Z
-zNear
-zFar
y0
zcamerafovy
Y
f
1
z-
y)
2
fovytan(
camera
0
-Z
-zNear
-zFar
y0
zcamerafovy
Y
After projection, the boundaries should map to 1, -1 )
fz-
(
yy
camera
cameraNDC
-Z
-zNear
-zFar
y0
zcamerafovy
Y
ycamera
Similarly in x, but consider the aspect.
)f
z-(aspect
xx
camera
cameraNDC
)f
z-(
yy
camera
cameraNDC
cz
m
camera
NDCz
cameraz
1NDCz
Mapping on z: inverse proportional to camera coordinates.
cz
m
camera
NDCz
-zFar is mapping to 1, -zNear is mapping to -1, then
czFar-
m1
czNear-
m1
zFar-zNear
zNearzFar c ,
zFar)-(zNear
zNearzFar2m
zFar-zNear
zNearzFar
)zFar)(-z-(zNear
zNearzFar2z
cameraNDC
)f
z-(
yy
camera
cameraNDC
)f
z-(aspect
xx
camera
cameraNDC
1
0100
200
100
100
camera
camera
camera
camera
clipping
clipping
clipping
z
y
x
zFarzNear
zNearzFar
zFarzNear
zNearzFarf
aspect
f
z
z
y
x
1
)/(
)/(
)/(
cameraclipping
cameraclipping
cameraclipping
zz
zy
zx
NDC space
FROM NDC SPACE TO WINDOW SPACE
02)1( xw
xx NDCwindow
02)1( yh
yy NDCwindow
(x0, y0): window position
(w, h): size of the window
SCAN CONVERSION
Vector to fragments
OPENGL PROGRESS
Fixed openGL pipeline (openGL 1.x)
Programmable openGL pipeline (above openGL 2.x)
Fixed openGL pipeline Programmable openGL pipeline
The programming becomes very different since modern hardware (GPU) has changed.
ADVANTAGE ON FIXED PIPELINE
Easy to learn: regard the pipeline as a black box.
DIS-ADVANTAGE ON FIXED PIPELINE PROGRAMMING
Debugging is hard if you do not know openGL pipeline.
(In modern graphic programming, we “must” understand the programmable system or we cannot programming easily)
Only provide specific functions, thus limiting the creativity and problem solving.
The programming on modern programmable GPU is totally different from that of fixed pipeline. (You must learn from the start).
DEPRECATED聲明不贊成 & EVIL OPENGL
OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf
Current version: OpenGL & GLSL 4.3
OpenGL 4.3 Reference card: Deprecated functions are gone!
http://www.khronos.org/files/opengl43-quick-reference-card.pdf
and many many many more…
…since 2008!(GLSL since 2004)
GPUs have changed!
(from “progressive openGL” 2012 slides)
RENDER LOOP(CLIENT)
initialize window
load shader program
clear frame buffer
Update transformation
Update Objects
Draw Object
SwapBuffers
while (running):
a linear array on GPU memory
SHADER DATA Uniform= Shared Constant
Vertex Data= ANYTHING YOU WANT!
Example?Positions…Normals…Colors…
Texture Coordinates…
“Per-object constant”(from “progressive openGL” slides, 2012)
SHADER DATA AND GLSL
OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf
“Per-object constant”
(from “progressive openGL” slides, 2012)
IN VS. OUT
Vertex Shader
in = Data from Vertex Buffer
out = Rasterizer in
FragmentShader
in = Rasterizer out
out = ANYTHING YOU WANT!
http://en.wikibooks.org/wiki/GLSL_Programming/Rasterization
http://en.wikibooks.org/wiki/GLSL_Programming/Rasterization
Rasterizer
GPU Memory
Fragment
Pixel… but usually pixel color and depth
(from “progressive openGL” slides, 2012)
EXAMPLE: DRAW AN TRIANGLE
CLIENT//declare a linear array on CPU memory
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f, (use xyz vector to represent a vertex)
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f, };
GLuint vertexbuffer;
// generate the vertex buffer object(VBO)
glGenBuffers(1, &vertexbuffer);
// bind VBO to GL_ARRAY_BUFFER, a state in OpenGL context
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// allocate GPU memory and copy VBO content to the memory
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
CLIENTdo{
…
// Use shader program
glUseProgram(programID);
// Give an attribute id to the VBO ( a VBO can be assigned many attribute ids)
glEnableVertexAttribArray(0);
//bind VBO vertexbuffer again (in other application we may have many VBOs)
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );//describe the content in VBO
// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
….
// Swap buffers
glfwSwapBuffers();
} while( ……);
(3 floats for a point) (offset) (padding)
(read 3 vertices once)
VERTEX SHADER
//specify GLSL version#version 330 core// Get vertex data from the VBO according to the vertex attribute id. The vertex data will stored in declared variable “vertex_position”layout(location = 0) in vec3 vertex_position;
void main(){// gl_Position is a built-in variable in GLSL, which is an output variable of the vertex shadergl_Position = vec4(vertex_position, 1.0);
}
note: vertex shader “must” output vertex position (in clipping coordinate space) to let OpenGL system perform scan conversion
FRAGMENT SHADER
#version 330//declare an output variable “color” to the imageout vec3 color;void main(){// output red color for each segmentcolor = vec3(1,0,0);}
Note:
In fragment shader, it receives a fragment in a “triangle” when vertex shader finish processing three vertices (remember GL_TRIANGLE in client code?)
The fragment is already in window coordinate here. We can derive the coordinate from the built-in variable for other application
REFERENCE
Dominik Seifert, “Progressive OpenGL” slides, 2012 ICG course.
Hong-Shiang Lin, “ICG clipping” slides, 2012 ICG course.
Jason L.McKesson, “Learning Modern 3D Graphics Programming” website. 2012
Recommended