Texture.hh

00001 /**********************************************************************
00002  * blaRAY -- photon mapper/raytracer
00003  * (C) 2008 by Tomasz bla Fortuna <bla@thera.be>, <bla@af.gliwice.pl>
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * any later version.
00009  *
00010  * See Docs/LICENSE
00011  *********************/
00012 
00013 #ifndef _TEXTURE_H_
00014 #define _TEXTURE_H_
00015 
00016 #include <iostream>
00017 #include <string>
00018 
00019 #include <cmath>
00020 
00021 #include "Math/Point.hh"
00022 #include "World/Color.hh"
00023 
00024 namespace World {
00025 
00033         class Texture {
00034         protected:
00036                 const Double SizeU, SizeV;
00038                 const Bool Tiled; 
00041                 Texture(const Double SizeU=1.0,
00042                         const Double SizeV=1.0,
00043                         const Bool Tiled=true)
00044                         : SizeU(SizeU), SizeV(SizeV), Tiled(Tiled) {}
00045 
00047                 void Tile(Math::Point &UV) const
00048                 {
00049                         Double u = UV.GetU();
00050                         Double v = UV.GetV();
00051                         UV.SetU(u - std::floor(u / SizeU) * SizeU);
00052                         UV.SetV(v - std::floor(v / SizeV) * SizeV);
00053                 }
00054 
00056                 virtual std::string Dump() const;
00057 
00058         public:
00059                 virtual ~Texture() 
00060                 {
00061                 }
00062 
00064                 virtual Color Get(Math::Point UV) const = 0;
00065 
00067                 friend std::ostream &operator<<(std::ostream &os, const Texture &T);
00068         };
00069 
00075         namespace TexLib {
00077                 class Plain : public Texture {
00078                 protected:
00080                         const Color C;
00081 
00082                         virtual std::string Dump() const;
00083                 public:
00085                         Plain(const Color &C) : C(C) {}
00086 
00087                         virtual Color Get(Math::Point UV) const {
00088                                 return C;
00089                         }
00090                 };
00091 
00093                 class Checked : public Texture {
00094                 protected:
00096                         const Color A, B;
00099                         virtual std::string Dump() const;
00100                 public:
00102                         Checked(
00103                                 const Color &A = ColLib::White(),
00104                                 const Color &B = ColLib::Black(),
00105                                 const Double SizeU = 1.0,
00106                                 const Double SizeV = 1.0,
00107                                 const Bool Tiled = true)
00108                                 : Texture(SizeU, SizeV, Tiled),
00109                                   A(A), B(B) {}
00110 
00111                         virtual Color Get(Math::Point UV) const {
00112                                 const Double HalfU = SizeU / 2;
00113                                 const Double HalfV = SizeV / 2;
00114 
00115                                 if (Tiled) Tile(UV);
00116                                 else {
00117                                         if (UV.GetU() > SizeU ||
00118                                             UV.GetV() > SizeV)
00119                                                 return ColLib::Black();
00120                                 }
00121                                 if ((UV.GetU() < HalfU && UV.GetV() < HalfV) ||
00122                                     (UV.GetU() > HalfU && UV.GetV() > HalfV))
00123                                         return A;
00124                                 return B;
00125                         }
00126                 };
00127 
00129                 const Texture &Red();
00130                 const Texture &Green();
00131                 const Texture &Blue();
00132                 const Texture &White();
00133                 const Texture &Black();
00134                 const Texture &Gray();
00137         };
00138 }
00139 #endif

Generated on Wed Mar 12 00:34:58 2008 for blaRAY by  doxygen 1.5.5