include/scene.h
branchpyrit
changeset 46 6493fb65f0b1
parent 44 3763b26244f0
child 47 320d5d466864
equal deleted inserted replaced
45:76b254ce92cf 46:6493fb65f0b1
    53 	Vector3 o, dir;
    53 	Vector3 o, dir;
    54 	Ray(const Vector3 &ao, const Vector3 &adir):
    54 	Ray(const Vector3 &ao, const Vector3 &adir):
    55 		o(ao), dir(adir) {};
    55 		o(ao), dir(adir) {};
    56 };
    56 };
    57 
    57 
       
    58 /**
       
    59  * sample
       
    60  */
       
    61 class Sample
       
    62 {
       
    63 public:
       
    64 	Float x,y;
       
    65 };
       
    66 
       
    67 /**
       
    68  * A abstract sampler.
       
    69  * It generates screen samples in coordinates between [-1..1] for height
       
    70  * and [-w/h..w/h] for width. It works in phases: initSampleSet returns
       
    71  * number of samples for each phase, then samples can be generated using
       
    72  * nextSample method. The resulting colour of each sample should be returned
       
    73  * via saveSample method. The sampler should save the results to given buffer
       
    74  * and decide if other phase is needed. When the picture is complete,
       
    75  * initSampleSet returns zero and picture can be read from buffer.
       
    76  */
       
    77 class Sampler
       
    78 {
       
    79 public:
       
    80 	Float *buffer;
       
    81 	int w,h;
       
    82 
       
    83 	Sampler(Float *abuffer, int &aw, int &ah): buffer(abuffer), w(aw), h(ah) {};
       
    84 	void resetBuffer(Float *abuffer, int &aw, int &ah) { buffer = abuffer; w = aw; h = ah; };
       
    85 	virtual void init() = 0;
       
    86 	virtual int initSampleSet() = 0;
       
    87 	virtual Sample *nextSample(Sample *prev) = 0;
       
    88 	virtual void saveSample(Sample *samp, Colour &col) = 0;
       
    89 };
       
    90 
       
    91 /**
       
    92  * default sample
       
    93  */
       
    94 class DefaultSample: public Sample
       
    95 {
       
    96 	friend class DefaultSampler;
       
    97 	int sx,sy;
       
    98 };
       
    99 
       
   100 /**
       
   101  * Default sampler.
       
   102  */
       
   103 class DefaultSampler: public Sampler
       
   104 {
       
   105 	int phase;
       
   106 public:
       
   107 	DefaultSampler(Float *abuffer, int &aw, int &ah): Sampler(abuffer, aw, ah), phase(-1) {};
       
   108 	void init() { phase = 0; };
       
   109 	int initSampleSet();
       
   110 	Sample *nextSample(Sample *prev);
       
   111 	void saveSample(Sample *samp, Colour &col);
       
   112 };
       
   113 
       
   114 /**
       
   115  * a camera
       
   116  */
    58 class Camera
   117 class Camera
    59 {
   118 {
    60 public:
   119 public:
    61 	Vector3 eye, p, u, v;
   120 	Vector3 eye, p, u, v;
    62 	Float f;
   121 	Float f;
    66 		eye(C), p(ap), u(au), v(av), f(3.14/4.0) {};
   125 		eye(C), p(ap), u(au), v(av), f(3.14/4.0) {};
    67 	void setEye(const Vector3 &aeye) { eye = aeye; };
   126 	void setEye(const Vector3 &aeye) { eye = aeye; };
    68 	void setFocalLength(const Float af) { f = af; };
   127 	void setFocalLength(const Float af) { f = af; };
    69 	void rotate(const Quaternion &q);
   128 	void rotate(const Quaternion &q);
    70 	void move(const Float fw, const Float left, const Float up);
   129 	void move(const Float fw, const Float left, const Float up);
    71 };
   130 
    72 
   131 	Ray makeRay(Sample *samp);
    73 /* axis-aligned bounding box */
   132 };
       
   133 
       
   134 /**
       
   135  * axis-aligned bounding box
       
   136  */
    74 class BBox
   137 class BBox
    75 {
   138 {
    76 public:
   139 public:
    77 	Vector3 L;
   140 	Vector3 L;
    78 	Vector3 H;
   141 	Vector3 H;
    82 	Float h() { return H.y-L.y; };
   145 	Float h() { return H.y-L.y; };
    83 	Float d() { return H.z-L.z; };
   146 	Float d() { return H.z-L.z; };
    84 	bool intersect(const Ray &ray, Float &a, Float &b);
   147 	bool intersect(const Ray &ray, Float &a, Float &b);
    85 };
   148 };
    86 
   149 
       
   150 /**
       
   151  * light object
       
   152  */
    87 class Light
   153 class Light
    88 {
   154 {
    89 public:
   155 public:
    90 	Vector3 pos;
   156 	Vector3 pos;
    91 	Colour colour;
   157 	Colour colour;
    96 	Light(const Vector3 &position, const Colour &acolour):
   162 	Light(const Vector3 &position, const Colour &acolour):
    97 		pos(position), colour(acolour), cast_shadows(true) {};
   163 		pos(position), colour(acolour), cast_shadows(true) {};
    98 	void castShadows(bool cast) { cast_shadows = cast; };
   164 	void castShadows(bool cast) { cast_shadows = cast; };
    99 };
   165 };
   100 
   166 
       
   167 /**
       
   168  * texture
       
   169  */
   101 class Texture
   170 class Texture
   102 {
   171 {
   103 public:
   172 public:
   104 	virtual Colour evaluate(Vector3 point) = 0;
   173 	virtual Colour evaluate(Vector3 point) = 0;
   105 };
   174 };
   106 
   175 
       
   176 /**
       
   177  * material
       
   178  */
   107 class Material
   179 class Material
   108 {
   180 {
   109 public:
   181 public:
   110 	Colour colour;
   182 	Colour colour;
   111 	Float ambient, diffuse, specular, shininess; // Phong constants
   183 	Float ambient, diffuse, specular, shininess; // Phong constants
   129 	void setReflectivity(const Float refl) { reflectivity = refl; };
   201 	void setReflectivity(const Float refl) { reflectivity = refl; };
   130 	void setTransmissivity(const Float trans, const Float rindex)
   202 	void setTransmissivity(const Float trans, const Float rindex)
   131 		{ transmissivity = trans; refract_index = rindex; };
   203 		{ transmissivity = trans; refract_index = rindex; };
   132 };
   204 };
   133 
   205 
       
   206 /**
       
   207  * shape
       
   208  */
   134 class Shape
   209 class Shape
   135 {
   210 {
   136 public:
   211 public:
   137 	Material *material;
   212 	Material *material;
   138 	Shape() {};
   213 	Shape() {};
   151 	virtual const Vector3 normal(const Vector3 &P) const = 0;
   226 	virtual const Vector3 normal(const Vector3 &P) const = 0;
   152 
   227 
   153 	virtual BBox get_bbox() const = 0;
   228 	virtual BBox get_bbox() const = 0;
   154 };
   229 };
   155 
   230 
       
   231 /**
       
   232  * list of shapes
       
   233  */
   156 class ShapeList: public vector<Shape*>
   234 class ShapeList: public vector<Shape*>
   157 {
   235 {
   158 };
   236 };
   159 
   237 
       
   238 /**
       
   239  * sphere shape
       
   240  */
   160 class Sphere: public Shape
   241 class Sphere: public Shape
   161 {
   242 {
   162 	Float sqr_radius;
   243 	Float sqr_radius;
   163 	Float inv_radius;
   244 	Float inv_radius;
   164 public:
   245 public:
   173 	bool intersect_bbox(const BBox &bbox) const;
   254 	bool intersect_bbox(const BBox &bbox) const;
   174 	const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; };
   255 	const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; };
   175 	BBox get_bbox() const;
   256 	BBox get_bbox() const;
   176 };
   257 };
   177 
   258 
       
   259 /**
       
   260  * box shape
       
   261  */
   178 class Box: public Shape
   262 class Box: public Shape
   179 {
   263 {
   180 	Vector3 L;
   264 	Vector3 L;
   181 	Vector3 H;
   265 	Vector3 H;
   182 public:
   266 public:
   192 	bool intersect_bbox(const BBox &bbox) const;
   276 	bool intersect_bbox(const BBox &bbox) const;
   193 	const Vector3 normal(const Vector3 &P) const;
   277 	const Vector3 normal(const Vector3 &P) const;
   194 	BBox get_bbox() const { return BBox(L, H); };
   278 	BBox get_bbox() const { return BBox(L, H); };
   195 };
   279 };
   196 
   280 
       
   281 /**
       
   282  * triangle vertex
       
   283  */
   197 class Vertex
   284 class Vertex
   198 {
   285 {
   199 public:
   286 public:
   200 	Vector3 P;
   287 	Vector3 P;
   201 	Vertex(const Vector3 &aP): P(aP) {};
   288 	Vertex(const Vector3 &aP): P(aP) {};
   202 };
   289 };
   203 
   290 
       
   291 /**
       
   292  * triangle vertex with normal
       
   293  */
   204 class NormalVertex: public Vertex
   294 class NormalVertex: public Vertex
   205 {
   295 {
   206 public:
   296 public:
   207 	Vector3 N;
   297 	Vector3 N;
   208 	NormalVertex(const Vector3 &aP): Vertex(aP) {};
   298 	NormalVertex(const Vector3 &aP): Vertex(aP) {};
   209 	NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {};
   299 	NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {};
   210 	const Vector3 &getNormal() { return N; };
   300 	const Vector3 &getNormal() { return N; };
   211 	void setNormal(const Vector3 &aN) { N = aN; };
   301 	void setNormal(const Vector3 &aN) { N = aN; };
   212 };
   302 };
   213 
   303 
   214 
   304 /**
       
   305  * triangle shape
       
   306  */
   215 class Triangle: public Shape
   307 class Triangle: public Shape
   216 {
   308 {
   217 #ifdef TRI_BARI_PRE
   309 #ifdef TRI_BARI_PRE
   218 	Float nu, nv, nd;
   310 	Float nu, nv, nd;
   219 	int k; // dominant axis
   311 	int k; // dominant axis