title: glossy reflection in ray tracing
body: <p>I am doing a project about ray tracing, right now I can do some basic rendering.</p>
  
  <p>The image below have: </p>
  
  <p>mirror reflection,
  refraction,
  texture mapping 
  and shadow.</p>
  
  <p><img src="https://i.stack.imgur.com/wUZ86.png" alt="enter image description here"></p>
  
  <p>I am trying to do the glossy reflection, so far this is what I am getting.
  Could anyone tell me if there is any problem in this glossy reflection image?</p>
  
  <p><img src="https://i.stack.imgur.com/1LvIN.png" alt="enter image description here"></p>
  
  <p>In comparison, the image below is from the mirror reflection</p>
  
  <p><img src="https://i.stack.imgur.com/rCWSp.png" alt="enter image description here"></p>
  
  <p>This is my code about glossy reflection, basically, once a primary ray intersect with an object.
  From this intersection, it will randomly shot another 80 rays, and take the average of this 80 rays color. The problem I am having with this code is the magnitude of x and y, I have to divide them by some value, in this case 16, such that the glossy reflect ray wouldn't be too random. Is there anything wrong with this logic?</p>
  
  <pre><code>             Colour c(0, 0, 0);
  
                       for (int i = 0; i &lt; 80; i++) {
  
                            Ray3D testRay;
                            double a = rand() / (double) RAND_MAX;
                            double b = rand() / (double) RAND_MAX;
                            double theta = acos(pow((1 - a), ray.intersection.mat-&gt;reflectivity));
                            double phi = 2 * M_PI * b;
                            double x = sin(phi) * cos(theta)/16;
                            double y = sin(phi) * sin(theta)/16;
                            double z = cos(phi);
                            Vector3D u = reflect.dir.cross(ray.intersection.normal);
                            Vector3D v = reflect.dir.cross(u);
                            testRay.dir = x  * u + y * v  + reflect.dir;
                            testRay.dir.normalize();
  
                            testRay.origin = reflect.origin;
                            testRay.nbounces = reflect.nbounces;
                            c = c + (ray.intersection.mat-&gt;reflectivity)*shadeRay(testRay);
                       }
                       col = col + c / 80;
  </code></pre>