Vous êtes sur la page 1sur 54

Graphics Programming

Light

aevans@salleurl.edu

Programación de Gráficos 3D
1
Alun Evans – aevans@salleurl.edu
RGB colour model

Additive colour model - adding ‘primary’ colours to black


(i.e. white = maximum)

Device dependent

Good because it fits in with


wavelength of eye cones

Programación de Gráficos 3D
2
Alun Evans – aevans@salleurl.edu
Applying colour to a triangle
1) Interpolate vertex colour
2) Send colour as uniform
3) Send colour as texture

Programación de Gráficos 3D
3
Alun Evans – aevans@salleurl.edu
Spot the difference

Programación de Gráficos 3D
4
Alun Evans – aevans@salleurl.edu
Spot the difference

Programación de Gráficos 3D
5
Alun Evans – aevans@salleurl.edu
The difference is light!

Programación de Gráficos 3D
6
Alun Evans – aevans@salleurl.edu
Making things more realistic

Simulating shading with LIGHT

Programación de Gráficos 3D
7
Alun Evans – aevans@salleurl.edu
Materials in 3D graphics

In 3D graphics, we use the term Material to describe the


properties of an object and how it reacts to light.

The properties of a material are used in the equations and


techniques to calculate the pixel painted on the screen.

Material != Surface/Object

Programación de Gráficos 3D
8
Alun Evans – aevans@salleurl.edu
Light - diffuse

Programación de Gráficos 3D
9
Alun Evans – aevans@salleurl.edu
Diffuse reflection

Light hits surface, is scattered either due to irregular


surface OR sub-surface and emerges in different
directions

Programación de Gráficos 3D
10
Alun Evans – aevans@salleurl.edu
Coloured diffuse reflection

A white material absorbs no light - it reflects all


wavelengths

Some materials absorb certain wavelengths and reflect


others - this gives us colour

Programación de Gráficos 3D
11
Alun Evans – aevans@salleurl.edu
Light - specular

Programación de Gráficos 3D
12
Alun Evans – aevans@salleurl.edu
Specular reflection

Mirror-like reflection of light

Colour is specular colour of surface

the ‘white spots’ on


objects

Programación de Gráficos 3D
13
Alun Evans – aevans@salleurl.edu
Mixing specular and diffuse

Programación de Gráficos 3D
14
Alun Evans – aevans@salleurl.edu
In the real world

Programación de Gráficos 3D
15
Alun Evans – aevans@salleurl.edu
(nearly) perfect specular reflection

Programación de Gráficos 3D
16
Alun Evans – aevans@salleurl.edu
Almost zero specular reflection

Programación de Gráficos 3D
17
Alun Evans – aevans@salleurl.edu
Sub surface scattering

Programación de Gráficos 3D
18
Alun Evans – aevans@salleurl.edu
Simulating this in graphics

Graphics is always a simulation!

Programación de Gráficos 3D
19
Alun Evans – aevans@salleurl.edu
Two models of illumination

1. Local 2. Global
Illumination Illumination

Pixels on screen

Programación de Gráficos 3D
20
Alun Evans – aevans@salleurl.edu
1. Local (Direct) illumination
Each object in the scene is shaded independently (with
special exceptions such as reflective surfaces) relative to light
sources
Usually much faster than global illumination, but also
much less realistic

Programación de Gráficos 3D
21
Alun Evans – aevans@salleurl.edu
Global Illumination

Models inter-reflection i.e. the colour of one object gets


transferred to another
e.g Ray Tracing, Radiosity (Study it in Grafics II)

Programación de Gráficos 3D
22
Alun Evans – aevans@salleurl.edu
Global vs Local Illumination

Programación de Gráficos 3D
23
Alun Evans – aevans@salleurl.edu
Local Illumination

We’re going to work almost exclusively with Local


Illumination

Why? It’s much much faster

(plus, it’s also ‘easier’!)

Programación de Gráficos 3D
24
Alun Evans – aevans@salleurl.edu
Illuminating a surface with light

Programación de Gráficos 3D
25
Alun Evans – aevans@salleurl.edu
How did they shade Virtua Fighter?
First polygon based fighting game
(just before Tekken)

1993 in arcades (by Sega)

1995 on Sega Saturn

Each polygon has a colour

The colour of the pixels on the


screen changes proportionally
according to the amount of light
hitting the polygon

Programación de Gráficos 3D
26
Alun Evans – aevans@salleurl.edu
Lambertian reflection

… says that the radiant intensity


from an ideal diffusely reflecting surface

is directly proportional

to the cosine of the angle θ


between the vector from the surface to the light
and the surface normal.

Programación de Gráficos 3D
27
Alun Evans – aevans@salleurl.edu
Lambert’s Law

Programación de Gráficos 3D
28
Alun Evans – aevans@salleurl.edu
How do we get the cosine of the angle between
two vectors?
The dot product (producto escalar)

Programación de Gráficos 3D
29
Alun Evans – aevans@salleurl.edu
N dot L

Dot product of Normal Vector (N) and Light Vector (vector


from point to light) (L)

Material colour * NdotL = pixel colour

Programación de Gráficos 3D
30
Alun Evans – aevans@salleurl.edu
Lambert’s law simulates diffuse reflection

Programación de Gráficos 3D
31
Alun Evans – aevans@salleurl.edu
Flat Shading - simplest way of shading

Treat every pixel on poly


the same i.e. Normal for
each pixel = face normal

calculate Lambert for each


face, apply colour to each
pixel

Programación de Gráficos 3D
32
Alun Evans – aevans@salleurl.edu
Modern Flat Shading

Programación de Gráficos 3D
33
Alun Evans – aevans@salleurl.edu
Calculating surface normal

The cross product (producto vectorial)

Programación de Gráficos 3D
34
Alun Evans – aevans@salleurl.edu
Flat shading algorithm

For each triangle:


calculate normal
calculate NdotL
For each pixel in triangle:
draw

What’s the problem with flat shading?

Programación de Gráficos 3D
35
Alun Evans – aevans@salleurl.edu
Flat shading vs smooth shading

Programación de Gráficos 3D
36
Alun Evans – aevans@salleurl.edu
Henri Gouraud

1971

Realised that if you could


interpolate colour
between vertices

Then you can calculate


the colour at the
vertices too

Programación de Gráficos 3D
37
Alun Evans – aevans@salleurl.edu
Vertex normals vs Face Normals

Programación de Gráficos 3D
38
Alun Evans – aevans@salleurl.edu
Gouraud Shading

Calculate colour at each vertex

Interpolate colour across each triangle

Programación de Gráficos 3D
39
Alun Evans – aevans@salleurl.edu
Gouraud vs flat shading

Programación de Gráficos 3D
40
Alun Evans – aevans@salleurl.edu
Our lighting model so far...

1) Calculate normal at each vertex by interpolating face


normals

2) Use NdotL to calculate (Lambert) colour at each vertex

3) During raster, for each pixel in poly, calculate


proportional distance to each vertex, and linear
interpolate colour

Programación de Gráficos 3D
41
Alun Evans – aevans@salleurl.edu
Normals in models
Most modelling packages export per-vertex normals, so
we can load them into GPU memory (faster than
calculating them).

Programación de Gráficos 3D
42
Alun Evans – aevans@salleurl.edu
Implementing Lambert reflection in OpenGL

Programación de Gráficos 3D
43
Alun Evans – aevans@salleurl.edu
Before we start, naming conventions in
shaders
attributes are the things that come into the vertex shader
- variables start with a_

varyings are things we pass between shader, variables


start with v_

uniforms are constants in both shaders, variables start


with u_

Programación de Gráficos 3D
44
Alun Evans – aevans@salleurl.edu
VBOs

VBO: Vertex Position (floats - 3)


Vertex Shader

VBO: Vertex Normal (floats - 3)

VAO
VBO: Vertex UV (floats - 2)

Fragment Shader
VBO: Indices (ints)

Programación de Videojuegos
45
Alun Evans – aevans@salleurl.edu
Steps to see normals - same as UVs
1) Add new VBO to read normals of sphere

2) Add new attribute to shader e.g.


in vec3 a_normal;

3) Add new varying to pass normal vector from vertex


shader to fragment shader

4) Normalize normal and paint in shader e.g


vec3 N = normalize(v_normal);
fragColor = vec4(N, 1.0);
Programación de Videojuegos
46
Alun Evans – aevans@salleurl.edu
Visualising normals should look something
like this:

Programación de Gráficos 3D
47
Alun Evans – aevans@salleurl.edu
Adding light

Programación de Gráficos 3D
48
Alun Evans – aevans@salleurl.edu
Steps to see normals - same as UVs
1) Add new global light variable to represent direction to light
e.g.
vec3 g_light_dir(100, 100, 100);

2) Create a new uniform in fragment shader to receive the light

3) Upload light direction to shader


glUniform3f(u_light_dir, g_light_dir.x, g_light_dir.y, g_light_dir.z);

Programación de Videojuegos
49
Alun Evans – aevans@salleurl.edu
Calculating NdotL in shader
We must make sure NdotL is greater than 0 to avoid
errors. e.g. this situation makes no sense, as cos(100o)
< 0, so we would be ‘subtracting’ light.
N

Programación de Videojuegos
50
Alun Evans – aevans@salleurl.edu
Calculating NdotL

vec3 L = normalize(u_light_dir);

float NdotL = max( dot(N,L) , 0.0 );

//....

vec3 final_color = texture_color.xyz * NdotL;

fragColor = vec4(final_color, 1.0);

Programación de Videojuegos
51
Alun Evans – aevans@salleurl.edu
Advanced Adding the universe

Programación de Videojuegos
52
Alun Evans – aevans@salleurl.edu
Advanced: add the universe
1. Download milkyway.bmp from estudy

2. Load milkyway.bmp as second texture

3. Draw same sphere geometry a second time, using


different texture. Scale to make it bigger than 1st
sphere

4. call glCullFace(GL_FRONT); before drawing the


universe sphere, so inside of sphere is drawn

Programación de Videojuegos
53
Alun Evans – aevans@salleurl.edu
More advanced

5. Create a second shader which only draws the texture


colour i.e.
fragColor = vec4(texture_color.xyz, 1.0);

6. disable depth test before drawing universe sphere,


enable it afterwards. If you draw the universe before the
earth, the universe will always be behind

7. translate the universe sphere to the camera position


every frame, so camera is always in center of the universe

Programación de Videojuegos
54
Alun Evans – aevans@salleurl.edu

Vous aimerez peut-être aussi