added getNormalAt() to Shape & Sphere, trying to get basic lighting

git-svn-id: svn://anubis/fart/trunk@44 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-23 18:09:27 +00:00
parent 801f61f48c
commit 761925220b
6 changed files with 23 additions and 7 deletions

View File

@ -132,9 +132,16 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
if (intersections.numResults > 0)
{
pixel[BMP_RED] = 0xFF;
pixel[BMP_GREEN] = 0xFF;
pixel[BMP_BLUE] = 0xFF;
Vector local_intersection_point =
transformed_ray[intersections.results[0]];
Vector normal = (*it)->getNormalAt(local_intersection_point);
double dot = -(normal % transformed_ray.getDirection());
cout << "got dot " << dot << endl;
if (dot < 0.0)
dot = 0.0;
pixel[BMP_RED] = (unsigned char) (0xFF * dot);
pixel[BMP_GREEN] = (unsigned char) (0xFF * dot);
pixel[BMP_BLUE] = (unsigned char) (0xFF * dot);
}
else
{

View File

@ -4,6 +4,7 @@
#include "util/Solver.h"
#include "util/Ray.h"
#include "util/Vector.h"
#include "util/Transform.h"
class Shape
@ -12,6 +13,7 @@ class Shape
virtual Solver::Result intersect(const Ray & ray) = 0;
void setTransform(const Transform & t) { m_transform = t; }
Transform & getTransform() { return m_transform; }
virtual Vector getNormalAt(const Vector & pt) = 0;
protected:
Transform m_transform;

View File

@ -34,3 +34,9 @@ Solver::Result Sphere::intersect(const Ray & ray)
}
return res;
}
Vector Sphere::getNormalAt(const Vector & pt)
{
Vector normal = pt;
return normal.normalize();
}

View File

@ -9,6 +9,7 @@ class Sphere : public Shape
public:
Sphere(double radius);
Solver::Result intersect(const Ray & ray);
Vector getNormalAt(const Vector & pt);
private:
double m_radius;

View File

@ -26,14 +26,14 @@ Vector & Vector::normalize()
return *this;
}
double Vector::mag()
double Vector::mag() const
{
return sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2]);
}
double Vector::mag2()
double Vector::mag2() const
{
return m_array[0] * m_array[0]
+ m_array[1] * m_array[1]

View File

@ -12,8 +12,8 @@ class Vector
double & operator[](int idx) { return m_array[idx]; }
double operator[](int idx) const { return m_array[idx]; }
Vector & normalize();
double mag();
double mag2();
double mag() const;
double mag2() const;
private:
double m_array[3];