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:
parent
801f61f48c
commit
761925220b
@ -132,9 +132,16 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
|||||||
|
|
||||||
if (intersections.numResults > 0)
|
if (intersections.numResults > 0)
|
||||||
{
|
{
|
||||||
pixel[BMP_RED] = 0xFF;
|
Vector local_intersection_point =
|
||||||
pixel[BMP_GREEN] = 0xFF;
|
transformed_ray[intersections.results[0]];
|
||||||
pixel[BMP_BLUE] = 0xFF;
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "util/Solver.h"
|
#include "util/Solver.h"
|
||||||
#include "util/Ray.h"
|
#include "util/Ray.h"
|
||||||
|
#include "util/Vector.h"
|
||||||
#include "util/Transform.h"
|
#include "util/Transform.h"
|
||||||
|
|
||||||
class Shape
|
class Shape
|
||||||
@ -12,6 +13,7 @@ class Shape
|
|||||||
virtual Solver::Result intersect(const Ray & ray) = 0;
|
virtual Solver::Result intersect(const Ray & ray) = 0;
|
||||||
void setTransform(const Transform & t) { m_transform = t; }
|
void setTransform(const Transform & t) { m_transform = t; }
|
||||||
Transform & getTransform() { return m_transform; }
|
Transform & getTransform() { return m_transform; }
|
||||||
|
virtual Vector getNormalAt(const Vector & pt) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Transform m_transform;
|
Transform m_transform;
|
||||||
|
@ -34,3 +34,9 @@ Solver::Result Sphere::intersect(const Ray & ray)
|
|||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector Sphere::getNormalAt(const Vector & pt)
|
||||||
|
{
|
||||||
|
Vector normal = pt;
|
||||||
|
return normal.normalize();
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class Sphere : public Shape
|
|||||||
public:
|
public:
|
||||||
Sphere(double radius);
|
Sphere(double radius);
|
||||||
Solver::Result intersect(const Ray & ray);
|
Solver::Result intersect(const Ray & ray);
|
||||||
|
Vector getNormalAt(const Vector & pt);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double m_radius;
|
double m_radius;
|
||||||
|
@ -26,14 +26,14 @@ Vector & Vector::normalize()
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Vector::mag()
|
double Vector::mag() const
|
||||||
{
|
{
|
||||||
return sqrt(m_array[0] * m_array[0]
|
return sqrt(m_array[0] * m_array[0]
|
||||||
+ m_array[1] * m_array[1]
|
+ m_array[1] * m_array[1]
|
||||||
+ m_array[2] * m_array[2]);
|
+ m_array[2] * m_array[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
double Vector::mag2()
|
double Vector::mag2() const
|
||||||
{
|
{
|
||||||
return m_array[0] * m_array[0]
|
return m_array[0] * m_array[0]
|
||||||
+ m_array[1] * m_array[1]
|
+ m_array[1] * m_array[1]
|
||||||
|
@ -12,8 +12,8 @@ class Vector
|
|||||||
double & operator[](int idx) { return m_array[idx]; }
|
double & operator[](int idx) { return m_array[idx]; }
|
||||||
double operator[](int idx) const { return m_array[idx]; }
|
double operator[](int idx) const { return m_array[idx]; }
|
||||||
Vector & normalize();
|
Vector & normalize();
|
||||||
double mag();
|
double mag() const;
|
||||||
double mag2();
|
double mag2() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double m_array[3];
|
double m_array[3];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user