CS 476: Computer Graphics Class Exercise - Lambertian Shading (1.5 Points)
Graphics content developed by Chris Tralie. Module autograding ecosystem designed by Chris Tralie and Bill Mongan.
Exercise Goals
The goals of this exercise are:- Explore how Lambertian illumination can be implemented with vertex shaders
- Explore how ideas from physics/optics can be implemented in code
Scene
{
"name":"localilluminationscene",
"materials":{
"green":{
"ka":[0.0, 0.4, 0.0],
"kd":[0.0, 1.0, 0.0],
"ks":[0.8, 0.0, 0.0]
},
"red":{
"kd":[1.0, 0.0, 0.0],
"ka":[0.5, 0.0, 0.0]
},
"yellow":{
"kd":[1.0, 1.0, 0.0],
"ka":[0.5, 0.5, 0.0]
}
},
"lights":[
{
"pos":[0, 2, 0],
"color":[1, 1, 1]
}
],
"cameras":[
{
"pos": [0.00, 1.50, 5.00],
"rot": [0.00, 0.00, 0.00, 1.00]
}
],
"children":[
{
"transform":[1, 0, 0, 0,
0, 1, 0, 0.5,
0, 0, 1, 0,
0, 0, 0, 1],
"shapes":[
{
"type":"box",
"length":1,
"width":1,
"height":1,
"center":[0, 0, 0],
"material":"red"
}
]
},
{
"transform":[20, 0, 0, 0,
0, 20, 0, 0,
0, 0, 20, 0,
0, 0, 0, 1],
"shapes":[
{
"type":"mesh",
"filename":"../assets/js/ggslac/meshes/square.off",
"material":"green"
}
]
},
{
"transform":[2, 0, 0, -2.5,
0, 2, 0, 1,
0, 0, 2, 0,
0, 0, 0, 1],
"shapes":[
{
"type":"mesh",
"filename":"../assets/js/ggslac/meshes/homer.off",
"material":"yellow"
}
]
},
{
"transform":[2, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1],
"shapes":[
{
"type":"sphere",
"radius":0.5,
"center":[0, 0, -10],
"material":"green"
}
]
}
]
}
Vertex Shader
precision mediump float;
#define MAX_LIGHTS 10
struct Light {
vec3 pos;
vec3 color;
vec3 atten;
};
// Material properties
uniform vec3 uKa; // Ambient color for material
uniform vec3 uKd; // Diffuse color for material
uniform vec3 uKs; // Specular color for material
uniform float uShininess; // Specular exponent for material
// Transformation/projection matrices
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 tMatrix;
uniform mat3 uNMatrix;
// Light properties
uniform int numLights;
uniform Light lights[MAX_LIGHTS];
// Camera properties
uniform vec3 uEye;
// Per-vertex attributes
attribute vec3 vPos;
attribute vec3 vNormal;
attribute vec3 vColor;
// Stuff to send to fragment shader
varying vec3 color;
void main(void) {
// Transformed position of vertex in homogenous coordinates
vec4 tpos = tMatrix*vec4(vPos, 1.0);
// Transformed normal of vertex
vec3 NT = normalize(uNMatrix*vNormal);
// Viewing window position, taking into consideration the camera
gl_Position = uPMatrix*uMVMatrix*tpos;
vec3 LPos = lights[0].pos; // Position of light
vec3 VPos = tpos.xyz; // Position of the vertex in world coordinates
// Diffuse coefficient
// TODO: Change the diffuse coefficient to be the dot product between
// the normal NT and the *normalized* direction from the vertex to the light
vec3 dummy = vec3(0.0, 0.0, 0.0);
float kdCoeff = dot(NT, dummy);
color = uKa + lights[0].color*(kdCoeff*uKd*vColor);
}
Fragment Shader
precision mediump float;
varying vec3 color;
void main(void) {
gl_FragColor = vec4(color, 1.0);
}
Enter your Ursinus netid before clicking Check/Submit. This is not your ID number or your email. For example, my netid is ctralie
(non Ursinus students can simply enter their name to perform the correctness check, but they won't get an e-mail record or any form of credit)
Netid |