240

# SER431 Lecture 10

(201809)

## Javier Gonzalez-SanchezPRO

September 17, 2018

## Transcript

1. jgs
SER 431
Javier Gonzalez-Sanchez
[email protected]
PERALTA 230U
Office Hours: By appointment

2. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 1
jgs
Our Goal
§ Planar Shadow: shadow is projected into the plane of the floor.

3. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 2
jgs
L = [Lx, Ly, Lz]
V = [Vx, Vy, Vz]
P = [Px, Py, Pz]
N = [Nx, Ny, Nz]

4. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 3
jgs
§ In a 3D space, the line passing through the point L and point V is
P = L + K *(V - L)
L = [Lx, Ly, Lz]
V = [Vx, Vy, Vz]
P = [Px, Py, Pz]
N = [Nx, Ny, Nz]
L = [0.0, 10.0, 5.0]
P = [20.0, 0.0, -1.0]
V = [10.0, 5.0, 2.0]
K=1
K=2

5. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 4
jgs
§ Having the plain’s normal N and any dot in the plain P,
dotProduct (N, P) + d = 0 // they are perpendicular
L = [Lx, Ly, Lz]
V = [Vx, Vy, Vz]
P = [Px, Py, Pz]
N = [0.0, 1.0, 0.0]
P = [20.0, 0.0, -1.0]

6. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 5
jgs
§ Having the plain’s normal N and any dot in the plain P,
dotProduct (N, P) + d = 0 // they are perpendicular
n • a – 32.5 = 0
n • c – 32.5 = 0
n • b – 32.5 = 0
y = - x + 5
N = [6.5,6.5,z]
b = [5,0,z]
a=[0,5,z]
c = [2.5,2.5,z]

7. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 6
jgs
§ Line passing through L and V:
P = L + K * (V - L)
§ Equation for a plane:
N ● P + d = 0
§ K can be solved as:
K = - (d + N ● L) / (N ●(V-L)
§ Then
P = L + ((d + N ● L) / (N ●(V - L))(V - L)
§ And, simplifying,

8. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 7
jgs
dot(N,L)–Lx Nx – Ly Nx – Lz Nx – Lw Nx
– Lx Ny dot(N,L)–Ly Ny – Lz Ny – Lw Ny
– Lx Nz – Ly Nz dot(N,L)–Lz Nz – Lw Nz
– Lx Nw – Ly Nw – Lz Nw – Lw Nw
Vx
Vy
Vz
1

9. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 8
jgs
dot(N,L)–Lx Nx – Ly Nx – Lz Nx – Lw Nx
– Lx Ny dot(N,L)–Ly Ny – Lz Ny – Lw Ny
– Lx Nz – Ly Nz dot(N,L)–Lz Nz – Lw Nz
– Lx Nw – Ly Nw – Lz Nw – Lw Nw
Scale
Translate
Rotate

10. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 9
jgs

11. jgs
Source Code

12. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 11
jgs

13. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 12
jgs
main()
void main(int argc, char* argv[]) {
// as usual here
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
// as usual here
}

14. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 13
jgs
init()
void init() {
// as usual here
glClearStencil(0);
// floor vertex
dot_vertex_floor.push_back(Vec3(-2000.0, 0.0, 2000.0));
dot_vertex_floor.push_back(Vec3(2000.0, 0.0, 2000.0));
dot_vertex_floor.push_back(Vec3(2000.0, 0.0, -2000.0));
dot_vertex_floor.push_back(Vec3(-2000.0, 0.0, -2000.0));
calculate_floor_normal(&floor_normal, dot_vertex_floor );
// light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}

15. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 14
jgs
display()
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// light source position
light_position[0] = 500 * cos(lightAngle);
light_position[1] = lightHeight;
light_position[2] = 500 * sin(lightAngle);
light_position[3] = 0.0; // directional light
// light moving
lightAngle += 0.0005;

16. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 15
jgs
// projection and view
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, width, height);
gluPerspective(40.0, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
// lookAt
gluLookAt(0.0f, 40.0f, 320.0, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f);
// camera
glScalef(scale, scale, scale);
glRotatef(x_angle, 1.0f, 0.0f, 0.0f);
glRotatef(y_angle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 0.0f);

17. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 16
jgs
// draw
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, light_position); // light position
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
}
// draw floor
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0, 1.0, 1.0, 0.3);
glPushMatrix();
glDisable(GL_LIGHTING);
glTranslatef(-900, 0, -900); glCallList(display1);
glEnable(GL_LIGHTING);
glPopMatrix();
glDisable(GL_BLEND);

18. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 17
jgs
glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Render 50% black shadow color on top of the floor
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING); /* Force the 50% black. */
glColor4f(0.0, 0.0, 0.0, 0.5);
glPushMatrix();
glDisable(GL_DEPTH_TEST);
// shadows for box 1, 2, 3
glEnable(GL_DEPTH_TEST);
glPopMatrix();
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
}

19. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 18
jgs
// box 1
glPushMatrix();
glCallList(display2);
glPopMatrix();
// box 2
glPushMatrix();
glTranslatef(200, 0, 0); glCallList(display3);
glPopMatrix();
// box 3
glPushMatrix();
glTranslatef(-200, 0, 0); glCallList(display4);
glPopMatrix();
// draw the light arrow
drawLightArrow();
glPopMatrix();
glutSwapBuffers();
}

20. jgs
Double Blending

21. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 20
jgs
Avoid Double Blending
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// code here
glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

22. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 21
jgs
Avoid Double Blending
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// code here
glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

23. jgs
Test Yourselves

24. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 23
jgs
Test Yourselves
§ Change renderShadow from 1 to 0
§ Change lightHeight from 100 to 1000 and 10
§ Change light type from directional (0) to positional light
§ Verify:
§ glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
§ glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
§ Make the light rotate like a Sun (around z or x axis)
§ Put together shadows and reflection.
§ Replace a box with an OBJ object.

25. jgs
Javier Gonzalez-Sanchez
[email protected]
Fall 2018
Disclaimer. These slides can only be used as study material for the class SER431 at ASU. They cannot be
distributed or used for another purpose.