src/scene.h
branchpyrit
changeset 0 3547b885df7e
child 7 bf17f9f84c91
equal deleted inserted replaced
-1:000000000000 0:3547b885df7e
       
     1 /*
       
     2  * C++ RayTracer
       
     3  * file: scene.h
       
     4  *
       
     5  * Radek Brich, 2006
       
     6  */
       
     7 
       
     8 #ifndef SCENE_H
       
     9 #define SCENE_H
       
    10 
       
    11 #include <vector>
       
    12 
       
    13 #include "noise.h"
       
    14 
       
    15 #include "vector.h"
       
    16 
       
    17 using namespace std;
       
    18 
       
    19 class Ray
       
    20 {
       
    21 public:
       
    22 	Vector3 a, dir;
       
    23 	Ray(const Vector3 &aa, const Vector3 &adir):
       
    24 		a(aa), dir(adir) {};
       
    25 };
       
    26 
       
    27 class Light
       
    28 {
       
    29 public:
       
    30 	Vector3 pos;
       
    31 	Colour colour;
       
    32 	int shadows;
       
    33 
       
    34 	Light(const Vector3 &position, const Colour &acolour):
       
    35 		pos(position), colour(acolour), shadows(1) {};
       
    36 	void castshadows(bool ashadows) { shadows = ashadows; };
       
    37 };
       
    38 
       
    39 class Texture
       
    40 {
       
    41 public:
       
    42 	Colour colour;
       
    43 	Colour evaluate(Vector3 point)
       
    44 	{
       
    45 		float sum = 0.0;
       
    46 		for (int i = 1; i < 5; i++)
       
    47 			sum += fabsf(perlin(point.x*i, point.y*i, point.z*i))/i;
       
    48 		float value = sinf(point.x + sum)/2 + 0.5;
       
    49 		return Colour(value*colour.r, value*colour.g, value*colour.b);
       
    50 	};
       
    51 };
       
    52 
       
    53 class Material
       
    54 {
       
    55 public:
       
    56 	float ambient, diffuse, specular, shininess; // Phong constants
       
    57 	float reflection; // how much reflectife is the surface
       
    58 	float refraction; // refraction index
       
    59 	float transmitivity;
       
    60 	Texture texture;
       
    61 
       
    62 	Material(const Colour &acolour) {
       
    63 		texture.colour = acolour;
       
    64 		ambient = 0.1;
       
    65 		diffuse = 0.5;
       
    66 		specular = 1.0;
       
    67 		shininess = 20.0;
       
    68 		reflection = 0.5;
       
    69 	}
       
    70 };
       
    71 
       
    72 class Shape
       
    73 {
       
    74 public:
       
    75 	Material *material;
       
    76 	Shape() {};
       
    77 	virtual ~Shape() {};
       
    78 
       
    79 	// first intersection point
       
    80 	virtual bool intersect(const Ray &ray, float &dist) = 0;
       
    81 
       
    82 	// all intersections (only for CSG)
       
    83 	virtual bool intersect_all(const Ray &ray, float dist, vector<float> &allts) = 0;
       
    84 
       
    85 	// normal at point P
       
    86 	virtual Vector3 normal(Vector3 &P) = 0;
       
    87 };
       
    88 
       
    89 class Sphere: public Shape
       
    90 {
       
    91 	float sqr_radius;
       
    92 	float inv_radius;
       
    93 public:
       
    94 	Vector3 center;
       
    95 	float radius;
       
    96 
       
    97 	Sphere(const Vector3 &acenter, const float aradius, Material *amaterial):
       
    98 		sqr_radius(aradius*aradius), inv_radius(1.0f/aradius),
       
    99 		center(acenter), radius(aradius) { material = amaterial; }
       
   100 	bool intersect(const Ray &ray, float &dist);
       
   101 	bool intersect_all(const Ray &ray, float dist, vector<float> &allts);
       
   102 	Vector3 normal(Vector3 &P) { return (P - center) * inv_radius; };
       
   103 };
       
   104 
       
   105 class Plane: public Shape
       
   106 {
       
   107 public:
       
   108 	Vector3 N;
       
   109 	float d;
       
   110 
       
   111 	Plane(const Vector3 &normal, const float ad, Material *amaterial):
       
   112 		N(normal), d(ad) { material = amaterial; };
       
   113 	Plane(const Vector3 &normal, const Vector3 &point):
       
   114 		N(normal), d(0) { /*TODO*/};
       
   115 	bool intersect(const Ray &ray, float &dist);
       
   116 	bool intersect_all(const Ray &ray, float dist, vector<float> &allts) {return false;};
       
   117 	Vector3 normal(Vector3 &) { return N; };
       
   118 };
       
   119 
       
   120 class Triangle: public Shape
       
   121 {
       
   122 	int k; // dominant axis
       
   123 	float nu, nv, nd;
       
   124 	float bnu, bnv;
       
   125 	float cnu, cnv;
       
   126 public:
       
   127 	Vector3 A, B, C, N;
       
   128 
       
   129 	Triangle(const Vector3 &aA, const Vector3 &aB, const Vector3 &aC, Material *amaterial);
       
   130 	bool intersect(const Ray &ray, float &dist);
       
   131 	bool intersect_all(const Ray &ray, float dist, vector<float> &allts) {return false;};
       
   132 	Vector3 normal(Vector3 &) { return N; };
       
   133 };
       
   134 
       
   135 #endif