Solution

Files changed (1) hide show
  1. lab4-shading/shading.frag +12 -3
lab4-shading/shading.frag CHANGED
@@ -61,11 +61,20 @@ vec3 calculateDirectIllumiunation(vec3 wo, vec3 n, vec3 base_color)
61
61
  // to the light. If the light is backfacing the triangle,
62
62
  // return vec3(0);
63
63
  ///////////////////////////////////////////////////////////////////////////
64
+ const float distance_to_light = length(viewSpaceLightPosition - viewSpacePosition);
65
+ const float falloff_factor = 1.0 / (distance_to_light * distance_to_light);
66
+ vec3 Li = point_light_intensity_multiplier * point_light_color * falloff_factor;
67
+ vec3 wi = normalize(viewSpaceLightPosition - viewSpacePosition);
68
+ if(dot(wi, n) <= 0.0)
69
+ return vec3(0.0);
64
70
 
65
71
  ///////////////////////////////////////////////////////////////////////////
66
72
  // Task 1.3 - Calculate the diffuse term and return that as the result
67
73
  ///////////////////////////////////////////////////////////////////////////
68
74
  // vec3 diffuse_term = ...
75
+ float ndotwi = dot(n, wi);
76
+ vec3 diffuse_term = (1.0 / PI) * base_color * ndotwi * Li;
77
+ direct_illum = diffuse_term;
69
78
 
70
79
  ///////////////////////////////////////////////////////////////////////////
71
80
  // Task 2 - Calculate the Torrance Sparrow BRDF and return the light
@@ -102,8 +111,8 @@ void main()
102
111
  // Task 1.1 - Fill in the outgoing direction, wo, and the normal, n. Both
103
112
  // shall be normalized vectors in view-space.
104
113
  ///////////////////////////////////////////////////////////////////////////
105
- vec3 wo = vec3(0.0);
106
- vec3 n = vec3(0.0);
114
+ vec3 wo = -normalize(viewSpacePosition);
115
+ vec3 n = normalize(viewSpaceNormal);
107
116
 
108
117
  vec3 base_color = material_color;
109
118
  if(has_color_texture == 1)
@@ -124,7 +133,7 @@ void main()
124
133
  ///////////////////////////////////////////////////////////////////////////
125
134
  // Task 1.4 - Make glowy things glow!
126
135
  ///////////////////////////////////////////////////////////////////////////
127
- vec3 emission_term = vec3(0.0);
136
+ vec3 emission_term = material_emission;
128
137
 
129
138
  vec3 final_color = direct_illumination_term + indirect_illumination_term + emission_term;
130
139