You are on page 1of 8

Ôn tập đồ họa

CHAPTER 3
Coordinate Systems
Homogeneous coordinate
v = [α1 α2 α3 0 ]T
p = [β1 β2 β3 1 ]T

Change of Coordinate Systems

a=[α1 α2 α3 ] và b=[β1 β2 β3]


v =α1v1+ α2v2 +α3v3 = [α1 α2 α3] [v1 v2 v3] T
=β1u1+ β2u2 +β3u3 = [β1 β2 β3] [u1 u2 u3] T

u1 = γ11v1+γ12v2+γ13v3
u2 = γ21v1+γ22v2+γ23v3
u3 = γ31v1+γ32v2+γ33v3

a=MT b

Với a=[α1 α2 α3 1] và b=[β1 β2 β3 1] hoặc a=[α1 α2 α3 0] và b=[β1 β2 β3 0]

Affine Transformations
Translation
T=

Loan Nguyen_ BKIT08.net Page 1


Rotation

Scaling

Shearing

x’ = x + y cot θ, y’ = y, z’ = z

Inverses
Translation: T-1(dx, dy, dz) = T(-dx, -dy, -dz)
Rotation: R -1(θ) = R(-θ)= RT(θ)
Scaling: S-1(sx, sy, sz) = S(1/sx, 1/sy, 1/sz)

Note: p’ = ABCp = A(B(Cp))

Loan Nguyen_ BKIT08.net Page 2


Code:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glRotatef(30.0, 0.0, 0.0, 1.0);
glTranslatef(-1.0, -2.0, -3.0);
stacks matrix:
glPushMatrix()
glPopMatrix()
get modelView matrix:
double m[16];
glGetFloatv(GL_MODELVIEW, m);

Inward and Outward Facing Polygons


right-hand rule

Quaternions:

q = s + xi+ yj+ zk
q =(s, x, y, z)
q =(s, v)
i2=j2=k2= ijk = −1
ij = k, jk = I, ki = j, ji = -k, kj = -I, ik = -j
q + q’= (s, v)+(s’, v’)= (s + xi+ yj+ zk)+(s’+ x’i+ y’j+ z’k)= =(s + s’, v + v’)
qq’=(ss’-vv’,v x v’+ sv’+s’v)
q-1=(s,-v)/||q||2

Rotation with quaternions:

q =(cos θ/2 , n sin θ/2), ||n||=1


p=(0,v)
q-1 =(cos θ/2 ,- n sin θ/2)
Rθ,n(v)= qpq−1

Loan Nguyen_ BKIT08.net Page 3


Conversion

quaternions to matrices
q =(s, x, y, z)


matrices to quaternions

 q=(s,x,y,z)

Vertex List

Edge List

Loan Nguyen_ BKIT08.net Page 4


Code
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
// glVertexPointer (GLint size,GLenum type,GLsizei stride,const GLvoid *
pointer);
glColorPointer(3, GL_FLOAT, 0, colors);
// void glColorPointer( GLint size,GLenum type,GLsizei stride,const GLvoid * pointer);
GLubyte cubeIndices[24] = {0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};
for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE,
&cubeIndices[4*i]);//1
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);//2
//use (1) or (2)

CHAPTER 4 VIEWING

glLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

Orthogonal Viewing

glOrtho(left,right,bottom,top,near,far)

OpenGL Perspective

glFrustum(left,right,bottom,top,near,far)
gluPerpective(fovy, aspect, near, far)// aspect=w/h

Loan Nguyen_ BKIT08.net Page 5


Orthogonal Normalization

Move center to origin: (chuyển tâm hình hộp về gốc tọa độ)
T(-(left+right)/2, -(bottom+top)/2,-(-near-far)/2))
Scale to have sides of length 2(scale hình hộp về khối vuông cạnh = 2 từ (-1,-1,-1) đến(1,1,1) )
Near plane: z = -near → z’’ = -1
Far plane z = -far → z’’ = +1
S(2/(left-right),2/(top-bottom),2/(-near- (-far)))

(z=-near) → -1 instead of (1)


(z=-far) → 1 instead of (-1)

P = MorthST

Oblique Normalization

Oblique Projection = Shear + Orthogonal Projection

P = Morth H(θ,φ)
P = Morth STH(θ,φ)

Loan Nguyen_ BKIT08.net Page 6


Perspective Normalization

Shearing (H): ( (r+l)/2, (t+b)/2,-n,1) (0,0,-n,1)


Scaling(S) : (l,b,-n,1) H(-n,-n,-n,1)
Normalization (N):
P = NSH

CHAPTER 5 LIGHTING AND SHADING


Lihting:
Phong lighting model:
I =kd Id l · n + ks Is (v · r )α + ka Ia
n: normal vector
l: unit vector from the surface point in the direction of the light source
kd/s/a: diffuse/specular/ambient reflection coefficient
r: reflected light direction ( r = 2(l · n)n – l )
v : unit vector from the surface point to the viewer

glMaterialfv(.) : materials’ properties

glLightfv(.):light intensities

Modified Phong Model: (Blinn-Phong Model)


h = ( l + v )/ | l + v |
2ψ =φ
ψ: halfway angle
I =kd Id l · n + ks Is (h · n )α + ka Ia

Loan Nguyen_ BKIT08.net Page 7


GL float diffuse0[]={1.0, 0.0, 0.0, 1.0};
GL float ambient0[]={1.0, 0.0, 0.0, 1.0};
GL float specular0[]={1.0, 0.0, 0.0, 1.0};
Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};
// w =1.0: a finite location
// w =0.0:parallel source
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightv(GL_LIGHT0, GL_SPECULAR, specular0);
glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);//a:power

GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};


GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat shine = 100.0
glMaterialf(GL_FRONT, GL_AMBIENT, ambient);
glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialf(GL_FRONT, GL_SPECULAR, specular);
glMaterialf(GL_FRONT, GL_SHININESS, shine);

Shading
Flat shading (vertices in a same polygon have a same color)
Smooth shading (Interpolate vertex shades across each polygon):
Gouraud shading
Phong shading

n =(p1 − p0) × (p2 − p0)

Gouraud Shading
Find average normal at each vertex (vertex normals)
Apply modified Phong model at each vertex
Interpolate vertex shades across each polygon

Phong Shading
Find vertex normals
Interpolate vertex normals across edges
Interpolate edge normals across polygon
Apply modified Phong model at each fragment

Loan Nguyen_ BKIT08.net Page 8

You might also like