程序代写CS代考 algorithm Computer Graphics

Computer Graphics
COMP3421/9415 2021 Term 3 Lecture 11

What did we learn before the break?
Games and Art
¡ñ Game Design and its relationship with Graphics
¡ñ The Art Pipeline
¡ñ Details of Modelling and Animation

What are we starting today?
Lighting
¡ñ What is light?
¡ñ Real World vs Simulation
¡ñ Starting on Lighting in Polygon Rendering

Light

What is light?
And why is it important for vision?
¡ñ Physics of real world light
¡ñ A particle or a wave (or both)
¡ñ A spectrum of electromagnetic radiation
¡ñ Travels in (mostly) straight lines
¡ñ Is effectively instantaneous
¡ñ Reflects off many surfaces in different ways
Image credit: Bungie Studios

Light and is us seeing different wavelengths of light
¡ñ A mix of multiple wavelengths
¡ñ Human vision is detecting reflected light
¡ñ Different surfaces absorb and reflect different
wavelengths
¡ñ . . . and in different directions
¡ñ Our eyes perceive colour based on which
wavelengths reach them
Image credit: Pink Floyd

Simulate Light
A physics based model for virtual lighting
¡ñ Send rays of light out from our light sources
¡ð Could need billions of these to match the number of photons in real light
¡ñ Bounce them off objects
¡ð Every bounce reflects the colour of the object
¡ñ If a reflected ray passes through the near plane of the frustum
¡ñ . . . and hits the camera
¡ñ Then colour the pixel with whatever colour the ray was

Simulate Light

Pros and Cons
This technique seems to make sense
¡ñ Accurate simulation of real light
¡ñ Nothing looks too complicated mathematically
¡ñ Realistic shadows and reflections
Wait, did you say Billions?
¡ñ Computationally very expensive
¡ñ Collision Detection is very expensive
¡ñ Most calculation is wasted

This technique has a name!
¡ñ A very big buzz word in present day Graphics
¡ñ Commercially available from 2018
¡ñ Before that, thought infeasible for realtime
(still incredibly resource hungry)
¡ñ Used in film from the late 1990s
Real Time Image credit: Unreal Engine

Polygon Rendering and Light
We don’t have a physics model
¡ñ We can’t trace rays of light easily
¡ñ What are our tools?
¡ñ Vertices, and Triangles, Coordinates, Camera
¡ñ Vector and Matrix math
¡ñ Can we build an approximate lighting model?
¡ñ That computes a lot faster?

A Simple Idea for Polygon Rendering Lighting
¡ñ Compute Lighting per fragment
¡ñ Simplify the idea of bouncing light
¡ñ Three main parts:
¡ñ Ambient Light
¡ñ Diffuse Light
¡ñ Specular Light

Ambient Light
Indirect Light
¡ñ Even outside of direct light, there’s some light
¡ñ Reflections off other surfaces
¡ñ Correct calculation takes a lot of light bounces
¡ñ . . . and doesn’t usually have much effect!
In
¡ñ Ambient light is constant throughout the scene

Diffuse Light
Directional Light on non-shiny surfaces
¡ñ Light directly from a source
¡ñ Hits a surface and scatters
¡ñ Matte or rough surface objects
In
¡ñ Calculated based on light source
¡ñ and angle to surface

Specular Light
Direct Reflections from shiny surfaces
¡ñ Light from a source
¡ñ Hits a surface and bounces directly
¡ñ Shiny or reflective objects
In
¡ñ Calculated based on direction to camera as well as direction to light source

Light Calculation per Fragment
Image credit: Wikipedia
Equation for the colour of a fragment
¡ñ Ambient: ambient light colour * surface colour
¡ñ+
¡ñ Diffuse: light colour * angle of light to surface * surface colour
¡ñ+
¡ñ Specular: light colour * angle of reflected light compared to viewing angle
* surface colour ¡ñ=
¡ñ Total colour in the fragment

Break Time
Cutting Corners
¡ñ Graphics is a field full of dirty hacks for optimisation
¡ñ We know that is accurate but expensive
¡ñ So we invent tricks like
¡ñ Polygon Rendering is itself a trick to reduce computation
Graphics is all a compromise between visual quality and speed

Lights and Materials

Where does light come from?
In the real world
¡ñ The Sun
¡ñ Lamps and light bulbs
In our virtual scenes
¡ñ Directional Lights (simulates the sun)
¡ñ Point Lights (have a position in the scene)
¡ñ And others . . .

Representation of Light sources
Directional Light
¡ñ Represented by a vector (a direction!)
¡ñ Considered to be so far away that its direction is the same everywhere in
the scene
Point Light(s)
¡ñ Represented by a point (a position in the scene!)
¡ñ Direction to fragments will need to be calculated

Materials
We’ve been calling these textures
¡ñ But there’s so much more!
¡ñ Materials are surface information
¡ñ Colour is one part of the surface information
¡ñ But there are other things:
¡ð How shiny is it?
¡ð Does it have ripples or bumps in it?
¡ñ An object can have multiple materials!

Ambient Lighting

Ambient Lighting Equation
Calculated per scene per fragment
¡ñ Ia = ia * ka
¡ñ Ia Final intensity of ambient light
¡ñ ia Intensity of ambient light in the scene
¡ñ ka Ambient reflectivity of fragment
Eg: Ambient Light is a bit reddish (0.1, 0.05, 0.05), the object’s material reflects bluish in ambient light (0.2, 0.9. 0.2)
Final ambient Light: (0.1, 0.05, 0.05) * (0.2, 0.9. 0.2) = (0.02, 0.045, 0.01)

Ambient Lighting Result
Image credit: learnopengl.com

– Directions of surfaces
Diffuse Lighting needs to know where a surface is facing
¡ñ Normal: a vector perpendicular to the surface
¡ñ Shows the “facing” direction
Attaching Normals to polygons
¡ñ Where can we store information in a polygon?
¡ñ Triangles have no data storage!
¡ñ It has to be in the vertices!

Normals attached to Vertices
Normals, Vertices and Triangles
¡ñ Normal data in vertices makes sense
¡ñ Frag shaders can already use vertex data
¡ñ Normals can be generated using triangle data
¡ñ Specific normals can be stored in the vertex attributes

Making Normals
Generating Normals from Triangles
¡ñ Cross Product of triangle edges
¡ñ Triangle winding order is now important!
¡ñ If we create vectors between vertices
¡ñ 1->3 x 1->2 is different from 1->2 x 1->3
¡ñ Counter-clockwise is the convention for
the “front” of a polygon
Image credit: learnopengl.com

Directions shouldn’t have magnitude
¡ñ Vectors can have a direction and magnitude
¡ñ If we’re using them purely as a direction . . .
¡ñ They should be length 1
¡ñ Lighting calculations will rely on all directions being length 1 vectors
¡ñ When in doubt, normalise!

Diffuse Lighting

Diffuse Lighting Equation
Calculated per light per fragment
¡ñ Id = kd * (L.N) * id
¡ñ Id Final intensity of diffuse light
¡ñ kd Diffuse reflectivity of fragment
¡ñ L Direction to light from fragment
¡ñ N Surface Normal
¡ñ id Diffuse intensity of light source

Breaking down the Diffuse Equation
What’s the L.N bit?
¡ñ A dot product of two direction vectors
¡ñ dot product results in a single number
¡ñ Scale of the difference in the vectors’ directions
¡ð 90¡ã, dot product = 0
¡ð 0¡ã, dot product = 1
¡ñ The light is the brightest when the light shines directly at the surface
¡ñ There is no light if the light is shining across the surface

Diffuse Lighting
The Equation Explained
¡ñ Take the intensity of the light
¡ñ and the diffuse reflectivity of the surface (surface colour)
¡ñ Multiply them (just like ambient lighting)
¡ñ Then multiply that colour by 0.0-1.0
¡ñ This number is a representation of how directly the surface normal is
aiming at the light source
¡ñ We find that out by using dot product of the two normalised vectors

Diffuse Lighting Result
Image credit: learnopengl.com

What did we learn today?
Lighting
¡ñ The difference between real and virtual
¡ñ Possibilities for simulation of light
¡ñ , an approximation of light
¡ñ Beginning to look in detail at the lighting algorithm