3D graphics pipeline Radosław Mantiuk Wydział Informatyki

Komentarze

Transkrypt

3D graphics pipeline Radosław Mantiuk Wydział Informatyki
Grafika komputerowa
3D graphics pipeline
Radosław Mantiuk
Wydział Informatyki
Zachodniopomorski Uniwersytet Technologiczny
Grafika komputerowa
System graficzny
3D scene
graphics engine
input
raster 2D image
output
Real-time
graphics system
local illumination
Grafika komputerowa
3D scene
Input data set required to generate the output raster image
-  Geometry of the 3D objects
-  Materials
-  surface color
-  texture
-  types of the surface
-  Camera
-  Light sources
-  Environment (fog, smoke, etc.)
objects
camera
light
3D scene
Grafika komputerowa
3D scene: Environment
fog
Grafika komputerowa
3D scene: How to represent the object shape
• 
• 
• 
• 
mathematical equations (e.g. sphere, plane, etc.)
fractals
curves and splines
constructive solid geometry (CSG)
•  polygons (triangles and/or quadrics)
Grafika komputerowa
3D scene: Modeler – specialized software
Grafika komputerowa
3D scene: Polygons
! 
! 
! 
! 
triangles + quadrics (all vertices belong to one plane)
many triangles
hierarchies of triangles
any shape can be approximated using a triangle dataset
Grafika komputerowa
3D scene: Aggregations of polygons
less data needed to store the object geometry
„strip”
v10
„fan”
v8
v9
„mesh”
Grafika komputerowa
3D scene: Hierarchies of triangles
repeating shapes do not need to be
define twice
Courtesy of Andries van Dam
Grafika komputerowa
Real-time graphics pipeline
application
geometry
processing
rasterization
Grafika komputerowa
Processing of geometry
Grafika komputerowa
OpenGL: triangles and quads
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, +1.0f, 0.0f );
glVertex3f( -1.0f, 0.0f, 0.0f );
glVertex3f( +1.0f, -1.0f, 0.0f );
glEnd();
glBegin(GL_QUADS);
glNormal3f( 0, 0, -1.0f );
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( -0.5f, +0.5f, -0.5f );
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( +0.5f, +0.5f, -0.5f );
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( +0.5f, -0.5f, -0.5f );
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glEnd();
glutSolidSphere()
Grafika komputerowa
OpenGL: Defining object
Grafika komputerowa
OpenGL: Defining object
glutSolidCube(1)
Grafika komputerowa
OpenGL: Defining object
glScalef( 1.5f, 3.0f, 1.5f );
glutSolidCube(1);
Grafika komputerowa
OpenGL: Defining object
glRotatef( 10.0f, 0.0f, 1.0f, 0.0f );
glRotatef( 60.0f, 1.0f, 0.0f, 0.0f );
glScalef( 1.5f, 3.0f, 1.5f );
glutSolidCube(1);
Grafika komputerowa
OpenGL: Defining object
glTranslatef( 1.0f, 1.0f, 0.0f );
glRotatef( 10.0f, 0.0f, 1.0f, 0.0f );
glRotatef( 60.0f, 1.0f, 0.0f, 0.0f );
glScalef( 1.5f, 3.0f, 1.5f );
glutSolidCube(1);
Grafika komputerowa
OpenGL: Vectors and matrices
macierz transformacji obiektów
!
#
#
#
#
#"
$ !
x ' & # r11sx
y' & # r21
=#
' &
z & # r31
1 &% #" 0
r12
r13
r22 sy
r23
r32
r33sz
0
0
t x $! x
&#
t y &# y
&
tz &# z
#
&
1 %" 1
$
&
&
&
&
%
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
glTranslatef( 1.0f, 1.0f, 0.0f );
glRotatef( 10.0f, 0.0f, 1.0f, 0.0f );
glRotatef( 60.0f, 1.0f, 0.0f, 0.0f );
glScalef( 1.5f, 3.0f, 1.5f );
glutSolidCube(1);
glPopMatrix();
Grafika komputerowa
OpenGL: Transformations are not commutative
M' = M * R * T
glRotatef( 45.0f, 0.0f, 0.0f, 1.0f );
glTranslatef( 2.0f, 0.0f, 0.0f );
glutWireCube( 1 );
M' = M * T * R
glTranslatef( 2.0f, 0.0f, 0.0f );
glRotatef( 45.0f, 0.0f, 0.0f, 1.0f );
glutWireCube( 1 );
Grafika komputerowa
OpenGL: Hierarchies
glPushMatrix();
glRotatef( 20.0f, 0.0f, 1.0f, 0.0f );
glRotatef( 10.0f, 1.0f, 0.0f, 0.0f );
glPushMatrix();
glScalef( 0.7f, 0.7f, 0.7f );
glTranslatef( 1.2f, 1.0f, 0.0f );
glutWireSphere( 1, 20, 20 );
glPopMatrix();
glPushMatrix();
glScalef( 0.7f, 0.7f, 0.7f );
glTranslatef( -1.2f, 1.0f, 0.0f );
glutWireSphere( 1, 20, 20 );
glPopMatrix();
glPushMatrix();
glScalef( 0.5f, 1.0f, 1.0f );
glTranslatef( 0.0f, -0.7f, 0.0f );
glutWireCube( 1 );
glPopMatrix();
glPushMatrix();
glScalef( 2.0f, 0.5f, 1.0f );
glTranslatef( 0.0f, -3.5f, 0.0f );
glutWireCube( 1 );
glPopMatrix();
glPopMatrix();
Grafika komputerowa
Pinhole camera
infinitesimal aperture, no lens, perfect image without camera optic aberrations
Grafika komputerowa
Camera parameters
!  camera defines visible portion of space
!  camera defines projection (parallel, perspective)
up
€
€
lookp
!  zoom (Field of View)
!  eye position (eyep)
!  look position (lookp)
!  image orientation (up)
!  aspect ratio (x and y resolution
in pixels)
fovY
eyep
Courtesy of Andries van Dam
Grafika komputerowa
Field of View
clipping planes
Courtesy of Andries van Dam
Grafika komputerowa
OpenGL: Camera
projection matrix (GL_PROJECTION)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( fovy, aspect, zNear, zFar );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( eyepX, eyepY, eyepZ, lookpX, lookpY, lookpZ, upX, upY, upZ );
Y
up
lookp
X
zFar
eyep
zNear
Z
gluPerspective( 50, 1, 1, 1000);
gluLookAt( 0, 0, 7, 0, 0, 0, 0, 1, 0 );
Grafika komputerowa
OpenGL: Transformacje geometryczne
verticies
object coordinates
ModelView matrix
camera space
Projection matrix
clipping
divide by w
normalized device coordinates (NDC)
raster window
transformation
screen space (pixels)
Grafika komputerowa
OpenGL: object space -> camera space
verticies
MModelView = MView * MModel
model space
gluLookAt(...)
ModelView
camera space
glScale()
glTranslate()
glRotate()
Projection
clipping
divide by w
NDC space
window
transformation
screen space
! x
# cam
# ycam
#
# zcam
#" 1
!
$
#
&
#
&
& = M mod elView #
#
&
#
&%
"
!
xobj $
&
#
#
yobj &
& = M view • M mod el #
zobj &
#
&
#
1 %
"
xobj $
&
yobj &
&
zobj &
&
1 %
Grafika komputerowa
OpenGL: object space -> camera space
verticies
model space
ModelView
camera space
Projection
clipping
divide by w
NDC space
window
transformation
screen space
Grafika komputerowa
OpenGL: Projection
GL_PROJECTION matrix
verticies
gluPerspective(...)
model space
ModelView
camera space
Projection
void gluPerspective(double fovY, double aspectRatio, double front, double back)
{
const double DEG2RAD = 3.14159265 / 180;
double tangent = tan(fovY/2 * DEG2RAD); // tangent of half fovY
double height = front * tangent;
// half height of near plane
double width = height * aspectRatio;
// half width of near plane
clipping
divide by w
// params: left, right, bottom, top, near, far
glFrustum(-width, width, -height, height, front, back);
NDC space
window
transformation
screen space
}
Grafika komputerowa
OpenGL: Perspective projection
GL_PROJECTION
verticies
model space
ModelView
camera space
Projection
clipping
divide by w
NDC space
window
transformation
screen space
! 2 ⋅ near
#
! x w $ # right − left
# clip & #
# yclip w & #
0
#
&=#
# zclip w & #
#
& #
0
" w % #
#
0
"
0
−(right + left)
right − left
2 ⋅ near
bottom − top
−(bottom + top)
bottom − top
0
far + near
far − near
0
1
$
&
&! x
&# cam
0
&# ycam
&#
z
−2 ⋅ near ⋅ far &# cam
&# 1
far − near &"
&
0
%
0
$
&
&
&
&
&%
Grafika komputerowa
gluOrtho2D(...)
OpenGL: Parallel projection
perspective
verticies
model space
ModelView
camera space
Projection
clipping
divide by w
parallel
NDC space
window
transformation
screen space
gluOrtho2D(...)
Grafika komputerowa
OpenGL: Clipping
verticies
Clipping removing objects located outside the view field
model space
ModelView
camera space
Projection
clipping
divide by w
NDC space
window
transformation
screen space
Culling – removing object covered by other objects including itself
(backface culling).
Grafika komputerowa
OpenGL: NDC oraz współrzędne ekranu
verticies
model space
ModelView
camera space
Projection
clipping
divide by w
NDC space
window
transformation
screen space
Normalised Device Coordinates (NDC)
! x
# ndc
# yndc
#
#" zndc
$ ! x /w
& # clip
& = # yclip / w
& #
&% #" zclip / w
$
&
&
&
&%
Grafika komputerowa
OpenGL: NDC and screen space
verticies
model space
ModelView
camera space
Projection
clipping
Screen coordinates:
xndc +1
⋅ width
2
y +1
yw = ndc ⋅ height
2
xw =
(0,0)
divide by w
NDC space
window
transformation
screen space
(w,h)
Grafika komputerowa
OpenGL: NDC and screen space
verticies
model space
ModelView
defining viewport:
glViewport(x, y, w, h);
camera space
Projection
clipping
divide by w
NDC space
window
transformation
screen space
! x
# w
# yw
#
#" zw
! width
width
#
xndc + (x +
)
$ #
2
2
& # height
height
yndc + (y +
)
&=#
2
2
& #
&% # far − near
far + near
z
+
ndc
#
2
2
"
$
&
&
&
&
&
&
&
%
Grafika komputerowa
Illumination model
Grafika komputerowa
3D scene: Light sources (1)
Emission of light
point light
directional light
spot light
Grafika komputerowa
3D scene: Light sources (2)
Natural light sources generate soft shadows
linear light
area light
Grafika komputerowa
OpenGL: Point light
glEnable( GL_NORMALIZE ); // normalize vectors after scaling
float g_lightPos[4] = { 0.0f, 0.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_POSITION, g_lightPos);
GLfloat color[] = { 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, color);
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
glLightfv(GL_LIGHT1, GL_EMISSION, color);
glLightfv(GL_LIGHT1, GL_SPECULAR, color);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
Grafika komputerowa
OpenGL: Directional light
glEnable( GL_NORMALIZE ); // normalize vectors after scaling
float g_lightDir[4] = { 0.0f, 0.0f, -1.0f, 0.0f };
glLightfv(GL_LIGHT1, GL_POSITION, g_lightPos);
GLfloat color[] = { 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, color);
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
glLightfv(GL_LIGHT1, GL_EMISSION, color);
glLightfv(GL_LIGHT1, GL_SPECULAR, color);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
Grafika komputerowa
OpenGL: Spot light
glEnable( GL_NORMALIZE ); // normalize vectors after scaling
float g_lightPos[4] = { 0.0f, 0.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_POSITION, g_lightPos);
GLfloat color[] = { 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, color);
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
glLightfv(GL_LIGHT1, GL_EMISSION, color);
glLightfv(GL_LIGHT1, GL_SPECULAR, color);
// dodatkowe parametry
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,95.0); // cutting angle
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,2.0); // weakening factor
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir); // light direction
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
Grafika komputerowa
Scena 3D: Material
How an object surface reflects the light
ambient
ambient + diffuse
diffuse
specular
ambient + diffuse + specular
Grafika komputerowa
OpenGL: Material
float colw[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv( GL_FRONT, GL_SPECULAR, colw);
glMaterialf( GL_FRONT, GL_SHININESS, 40.0f );
float col[4] = { 0.01f, 0.6f, 0.01f, 1.0f };
glMaterialfv( GL_FRONT, GL_DIFFUSE, col);
float cola[4] = { 0.01f, 0.2f, 0.01f, 1.0f };
glMaterialfv( GL_FRONT, GL_AMBIENT, cola);
Grafika komputerowa
Scena 3D: Physics
α
α α
€
ambient + diffuse
€€
β
specular
transparent
surfaces not
€
taken into account
Grafika komputerowa
Rasterization
Grafika komputerowa
Rasterization
Defines hot to compute a color of the output pixels of the rendered image. This
data is stored in the frame buffer.
Triangle setup
Scan conversion
Pixel shading
Merging
Grafika komputerowa
Scan conversion
To find pixels corresponding with a triangle
Grafika komputerowa
Edge walking technique
1. 
2. 
3. 
Choose filling direction.
Compute spans (edge pixels).
Fill with the horizontal lines.
Pictures courtesy of MIT (lecture 6.837)
Grafika komputerowa
Pixel shading
To compute illumination for each pixel in the triangle
Grafika komputerowa
Texturing
To add textures to the image
Grafika komputerowa
Merging
•  Obliczenie koloru RGB pikseli na podstawie danych fragmentów poszczególnych
trójkątów obejmujących ten piksel.#
•  Umieszczenie danych w buforze kolorów/ramki (ang. frame buffer)#
•  Obliczanie widoczności (bufor Z).#
•  Obliczanie przezroczystości (kanał Alfa).#
•  Mieszanie za pomocą bufora szablonu (ang. stencil buffer).#
•  Wykorzystanie bufora akumulacyjnego (ang. accumulation buffer).#
•  Wykorzystanie podwójnego buforowania (ang. double buffering).#
fragment - zbiór danych, na podstawie których oblicza się kolor piksela
Grafika komputerowa
Occlusion tests
Z-buffer
A buffer in the memory, which stores the closest distance between camera and the
surface represented by the pixel.
Z-values inside the triangle are interpolated
Grafika komputerowa
Literatura
1. 
Tomas Akenine-Moller, Eric Haines, Naty Hoffman, Real-Time Rendering (3rd
edition), A K Peters, 2008.

Podobne dokumenty