1 #ifndef PROCEDURALWOODTILES_HXX
  2 #define PROCEDURALWOODTILES_HXX
  3 
  4 #include "ProceduralWood.hxx"
  5 #include "Noise.hxx"
  6 
  7 // This class takes three wood procedurals and creates tiles out of it, where
  8 // adjacent tiles are never 'colored' equally.
  9 class ProceduralWoodTiles : public Procedural
 10 {
 11 private:
 12 	ProceduralWood *pwood[3];
 13 	Vec3f          OrientationX,
 14 	               OrientationY,
 15 	               DarkColor;
 16 	float          TileSizeX,
 17 	               TileSizeY,
 18 	               Offset,
 19 	               GrooveWidthX,
 20 	               GrooveWidthY;
 21 
 22 	inline float modulo(float a, float b)
 23 	{
 24 		return a - floor(a / b) * b;
 25 	}
 26 
 27 	int Tiles(Vec3f position)
 28 	{
 29 		// This function is only responsible to identify the plank we are currently on.
 30 		// Coloring or Bumpmapping this plank is done by the respective wood procedural.
 31 
 32 		// First get the position in dependance of the orientation vectors (the coordinate
 33 		// system of the wooden floor)
 34 		Vec3f LocalOrientation(Dot(position, OrientationX), Dot(position, OrientationY), 0);
 35 
 36 		// Get tile number in x dimension (modulo 9, this identifies the shift of planks)
 37 		float fx = LocalOrientation.x / TileSizeX;
 38 		int    x = static_cast<int>(floor(fx));
 39 		
 40 		if (fx - static_cast<float>(x) < GrooveWidthX)
 41 			return -1;
 42 
 43 		x %= 9;
 44 		if (LocalOrientation.x < 0)
 45 			x += 9;//8;
 46 
 47 		// Get tile number in y dimension (shifted according to x position, modulo 3)
 48 		float fy = (LocalOrientation.y + static_cast<float>(x) * Offset) / TileSizeY;
 49 		int    y = static_cast<int>(floor(fy));
 50 
 51 		if (fy - static_cast<float>(y) < GrooveWidthY)
 52 			return -1;
 53 
 54 		y %= 3;
 55 		if (y < 0)
 56 			y += 3;//2;
 57 
 58 		return y;
 59 	}
 60 
 61 public:
 62 	ProceduralWoodTiles(bool  useTexture,     bool useBumpMap,
 63 	                    Vec3f OrientationX,   Vec3f OrientationY,
 64 	                    float TileSizeX,      float TileSizeY,
 65 	                    Vec3f TrunkLocation1, Vec3f GrainDir1, float RingWidth1,
 66 	                    Vec3f TrunkLocation2, Vec3f GrainDir2, float RingWidth2,
 67 	                    Vec3f TrunkLocation3, Vec3f GrainDir3, float RingWidth3,
 68 	                    float GrooveWidth = 0.0f, float Chaos = 1.0f,
 69 	                    Vec3f LightColor = Vec3f(0.75,0.45,0.20), Vec3f DarkColor = Vec3f(0.55,0.30,0.15))
 70 		: Procedural(useTexture, useBumpMap, true, true), OrientationX(OrientationX), OrientationY(OrientationY),
 71 		  DarkColor(DarkColor), TileSizeX(TileSizeX), TileSizeY(TileSizeY),
 72 		  GrooveWidthX(GrooveWidth/TileSizeX), GrooveWidthY(GrooveWidth/TileSizeY)
 73 	{
 74 		// Initialize the three wooden procedurals
 75 		pwood[0] = new ProceduralWood(useTexture, useBumpMap, TrunkLocation1, GrainDir1, RingWidth1, 0.1, Chaos, LightColor, DarkColor);
 76 		pwood[1] = new ProceduralWood(useTexture, useBumpMap, TrunkLocation2, GrainDir2, RingWidth2, 0.1, Chaos, LightColor, DarkColor);
 77 		pwood[2] = new ProceduralWood(useTexture, useBumpMap, TrunkLocation3, GrainDir3, RingWidth3, 0.1, Chaos, LightColor, DarkColor);
 78 
 79 		Offset = TileSizeY * 4.0f / 3.0f;
 80 
 81 		Normalize(OrientationX);
 82 		Normalize(OrientationY);
 83 	};
 84 
 85 	virtual ~ProceduralWoodTiles()
 86 	{
 87 		for (int i = 0; i < 3; ++i)
 88 			delete pwood[i];
 89 	};
 90 
 91 	// Return the brownish color texture
 92 	virtual Vec3f GetColor (Vec3f coordinate)
 93 	{
 94 		int parts = Tiles(coordinate);
 95 
 96 		if (parts < 0)
 97 			return DarkColor;
 98 
 99 		return pwood[parts]->GetColor(coordinate);
100 	};
101 
102 	// Perturb the normal, s.t. darker regions (made of denser material) come out of the structure
103 	virtual void BumpMap(Vec3f &normal, Vec3f coordinate)
104 	{
105 		int parts = Tiles(coordinate);
106 
107 		if (parts < 0)
108 			return;
109 
110 		pwood[parts]->BumpMap(normal, coordinate);
111 	};
112 };
113 
114 #endif


syntax highlighted by Code2HTML, v. 0.9.1