# Week 8

MathFirst Images

Plane

Plane Images

Diffuse

Diffuse Images

Diffuse Fix

Fixed Image

### Math

**Renderer::buildPixArray()**Fixed the order of my for loops so that iteration is done from top left corner to bottom right

`int pixIndex = 0; for( int y = 0; y < _height; y++ ) { double nY = (y + 0.5)/h; for( int x = 0; x < _width; x++ ) { double nX = (x + 0.5)/w; _pixel[pixIndex] = theTracer.Trace( nX, nY ); pixIndex++; } }`

**Tracer::_width**I was not including the distance of the camera forward vector into the width calculation

`_camForward = _lookat - _eye; double dist = _camForward.magnitude(); _width = dist * 2 * tan( radFOV / 2 );`

**Tracer::Trace(double nx, double ny)**Validated the calculation of my view vector. I think i was over complicating the math here which made it difficult to visualize in my head what was ACTUALLY happening. This updated version is easier to read.

`Point3d s; s = _lookat; //move out to render plane s += _height * ( 0.5 - ny ) * _camUp; s += _width * ( 0.5 - nx ) * _camRight * -1;`

**Sphere::detectHit**My Sphere intersection calculation was wrong. I read about it in the text book:

*Fundementals of Computer Graphics 2nd ed.*to get the correct math working`double a = view.dot(view); double b = 2*view.dot(eye - _position); double c = ( eye - _position ).dot( eye - _position ) - ( _radius * _radius );`

### First Images

*above: one sphere with a position (-1, 0, 0)*

*above: two spheres intersecting*

*above: two spheres intersecting with color*

### Plane

I got the plane intersection working. I used this siggraph reference: Ray-Plane Intersection. My first attemts were wrong. Turns out my comprehension of the paper was a bit off. I reversed my dot products.

```
Vector3d Rd = view;
Normal3d Pn = _normal.getNormalized();
double t = -1;
double Vd = Pn.dot( Rd );
if( Vd < 0 )
{ //normal of plane is facing camera
Point3d origin(0,0,0);
Vector3d toPlane = _position - origin;
double dist = toPlane.magnitude();
double V0 = (Pn.dot( eye ) + dist) * -1;
t = V0 / Vd;
}
return t;
```

### Plane Images

*above: first attempt at plane intersection. incorrect dot product calculations*

*above: correct plane intersection calculation*

### Diffuse

I built my material classes. The flat shader went pretty smooth. Getting Diffuse to look right is proving to be somewhat of a challenge.

```
double diffuse;
list< Light* >::iterator iter;
list< Light* > *lights = Scene::getInstance()->getLightList();
for( iter = lights->begin(); iter != lights->end(); iter++ )
{
double dotProd = (*iter)->getDirection( hit ).dot( hit.normal );
if( dotProd < 0 )
diffuse *= dotProd;
}
return( (_color * diffuse).clamp() );
```

### Diffuse Images

*above: hmmmmm.... first render with diffuse. Not too sure what's going on here.*

### Diffuse Fix

**Backwards light vector**Recalculated so that it points towards the light

**Multiplying instead of Adding**Diffuse factor is the sum of the dot product values not the product. The result color is the product of the diffuse factor and the base color

```
Color3d Diffuse::getColor( const HitRecord &hit )
{
double diffuse = 0.0;
list< Light* >::iterator iter;
list< Light* > *lights = Scene::getInstance()->getLightList();
for( iter = lights->begin(); iter != lights->end(); iter++ )
{
Normal3d Nn = hit.normal;
Nn.normalize();
Normal3d Ll = (*iter)->getDirection( hit );
Ll.normalize();
double dotProd = Nn.dot( Ll );
if( dotProd > 0 )
diffuse += dotProd;
}
Color3d result = _color * diffuse;
result.clamp();
return( result );
}
```

### Fixed Image

*above: correct render with diffuse on all surfaces.*