// $Header: /home/abe/school/manual/ray-project/RCS/cc.html,v 1.1 2010/06/05 16:29:47 abe Exp abe $
#include
#include
class Vect3 {
public:
Vect3();
Vect3(const Vect3 &);
Vect3(const double, const double, const double);
~Vect3();
Vect3 operator +(const Vect3);
Vect3 operator -(const Vect3);
Vect3 operator *(const double);
void normalize(void);
double innerproduct(const Vect3);
double x, y, z;
};
Vect3::Vect3()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
Vect3::Vect3(const Vect3& v)
{
x = v.x;
y = v.y;
z = v.z;
}
Vect3::Vect3(const double x1, const double y1, const double z1)
{
x = x1;
y = y1;
z = z1;
}
Vect3::~Vect3(){}
void Vect3::normalize(void)
{
double d = sqrt(x*x + y*y + z*z);
x /= d;
y /= d;
z /= d;
}
double Vect3::innerproduct(const Vect3 v)
{
return x * v.x + y * v.y + z * v.z;
}
Vect3 Vect3::operator +(const Vect3 v)
{
return Vect3(x + v.x, y + v.y, z + v.z);
}
Vect3 Vect3::operator -(const Vect3 v)
{
return Vect3(x - v.x, y - v.y, z - v.z);
}
Vect3 Vect3::operator *(const double d)
{
return Vect3(d*x, d*y, d*z);
}
class Color
{
public:
Color();
Color(const Color &);
Color(const double, const double, const double);
~Color();
int intR(void);
int intG(void);
int intB(void);
double r, g, b;
};
Color::Color()
{
r = 0.0;
g = 0.0;
b = 0.0;
}
Color::Color(const Color& c)
{
r = c.r;
g = c.g;
b = c.b;
}
Color::Color(const double red, const double green, const double blue)
{
r = red;
g = green;
b = blue;
}
Color::~Color(){}
int Color::intR(void)
{
int ir = (int)(255 * r);
ir = ir > 255 ? 255 : ir;
return ir < 0 ? 0 : ir;
}
int Color::intG(void)
{
int ig = (int)(255 * g);
ig = ig > 255 ? 255 : ig;
return ig < 0 ? 0 : ig;
}
int Color::intB(void)
{
int ib = (int)(255 * b);
ib = ib > 255 ? 255 : ib;
return ib < 0 ? 0 : ib;
}
class Sphere
{
public:
Sphere(const double, const Vect3, const Color);
~Sphere();
bool isIntersect(Vect3 , Vect3 , Vect3&);
double r; // radius
Vect3 p; // position
Color c; // color
};
Sphere::Sphere(const double rad, const Vect3 pos, const Color col)
{
r = rad;
p = Vect3(pos);
c = Color(col);
}
Sphere::~Sphere(){}
bool Sphere::isIntersect(Vect3 from, Vect3 to, Vect3& v)
{
Vect3 vec = from - p;
double b = to.innerproduct(vec);
double c = vec.innerproduct(vec) - r * r;
double d = b * b - c;
if(d < 0)
return false;
double det = sqrt(d);
double t = -b + det;
if(t < 0)
return false;
Vect3 v1 = to * t + from;
v1 = v1 - p;
v1.normalize();
v = v1;
return true;
}
class Light
{
public:
Light();
Light(Light&);
Light(double, double, double);
~Light();
Vect3 d;
};
Light::Light(){}
Light::Light(Light& l)
{
d = l.d;
}
Light::Light(double x, double y, double z)
{
d = Vect3(x, y, z);
d.normalize();
}
Light::~Light(){}
class Shader {
public:
Shader(Light&);
~Shader();
void addLight(const Light);
void shading(const Vect3, Color&);
Light light;
double ambient = 0.3;
};
Shader::Shader(Light& l)
{
light = Light(l);
}
Shader::~Shader(){}
void Shader::shading(const Vect3 v, Color& c)
{
c.r = light.d.innerproduct(v) + ambient;
c.g = 0.0;
c.b = 0.0;
}
class Camera
{
public:
Camera(const Vect3, const Vect3);
~Camera();
void start(void);
void next(void);
bool end(void);
Vect3 from;
Vect3 to;
int resolutionX = 200;
int resolutionY = 200;
};
Camera::Camera(const Vect3 pos, const Vect3 direc)
{
from = Vect3(pos);
to = Vect3(direc);
}
Camera::~Camera(){}
void Camera::start(void)
{
from.x = -1.0;
from.y = -1.0;
}
void Camera::next(void)
{
from.x += 2.0/resolutionX;
if(from.x >= 1.0)
{
from.x = -1.0;
from.y += 2.0/resolutionY;
}
}
bool Camera::end(void)
{
return from.y < 1.0;
}
class PicFile {
public:
PicFile(int, int);
~PicFile();
void init(void);
void printCol(Color);
int resolutionX;
int resolutionY;
};
PicFile::PicFile(int resx, int resy)
{
resolutionX = resx;
resolutionY = resy;
}
PicFile::~PicFile(){}
void PicFile::init(void)
{
cout << "P3" << endl << resolutionX << " " << resolutionY << endl << "255"<< endl ;
}
void PicFile::printCol(Color col)
{
cout << col.intR() << " " << col.intG() << " " << col.intB() << endl;
}
int
main(int argc, char *argv[])
{
Vect3 p = Vect3(0.0, 0.0, 0.0);
Color c = Color(1.0, 0.0, 0.0);
Sphere s = Sphere(0.5, p, c);
Light l = Light(1.0, -1.0, -1.0);
Shader shader = Shader(l);
Vect3 from = Vect3(0.0, 0.0, 3.0);
Vect3 to = Vect3(0.0, 0.0, -1.0);
Camera cam = Camera(from, to);
PicFile pic = PicFile(cam.resolutionX, cam.resolutionY);
Color bcol = Color(0.5, 0.5, 0.5);
pic.init();
for(cam.start(); cam.end(); cam.next())
{
Vect3 v;
Color col;
if(s.isIntersect(cam.from, cam.to, v))
{
shader.shading(v, col);
pic.printCol(col);
}
else
pic.printCol(bcol);
}
return 0;
}