arbre.cpp (3990B)
1 #include "all_includes.hh" 2 3 void Arbre::initPlane(Vertex _start, Triangle plane) { 4 start = _start; 5 type = ARBRE; 6 addEntropy(start); 7 addEntropy(plane); 8 9 Vertex h = plane.normalizedNormal(); 10 Vertex l = (plane[TOP] - plane[LEFT]).normalize(); 11 Vertex u = h * l; 12 13 rotation = Angle3D(h, l, u); 14 rotation = rotation.rotateH(floatInRange(seed, -3, 0, 2*Angle::Pi)); 15 rotation = rotation.rotateU(floatInRange(seed, -4, Angle::d2r(-10), Angle::d2r(10))); 16 length = floatInRange(seed, -5, 3*100, 4*100); 17 } 18 19 Arbre::Arbre(Vertex _start, Triangle plane) { 20 initPlane(_start, plane); 21 } 22 23 Arbre::Arbre(Vertex _start, Quad plane) { 24 initPlane(_start, Triangle(plane[NE], plane[SE], plane[SW])); 25 } 26 27 Arbre::Arbre(Vertex _start, Angle3D _rotation, float _length, Type _type) : start(_start), rotation(_rotation), length(_length), type(_type) { 28 addEntropy(start, rotation.h, rotation.l, rotation.u); 29 addEntropyf(length); 30 addEntropy((int)(type)); 31 } 32 33 void Arbre::split() { 34 if (type == ARBRE && length > floatInRange(seed, -1, 10, 20)) { 35 int nbBranches = 2 + (hash2(seed, -2) % 3); 36 for (int i = 0; i < nbBranches; i++) { 37 Vertex bStart = end(floatInRange(seed, 4*i, 0.7f, 0.9f)); 38 Angle3D rot = rotation; 39 rot = rot.rotateH(Angle::d2r(floatInRange(seed, 4*i+1, 25.f, 37.f) + (float)i*(360.f / (float)nbBranches))); 40 rot = rot.rotateU(Angle::d2r(floatInRange(seed, 4*i+2, 35.f, 55.f))); 41 float len = length * floatInRange(seed, 4*i+3, tauxMax()*2.f/3.f, tauxMax()); 42 addChild(new Arbre(bStart, rot, len, ARBRE)); 43 } 44 addChild(new Arbre(start, rotation, length, TRONC)); 45 } 46 } 47 48 void Arbre::triangulation() { 49 if (type == ARBRE || type == TRONC) tronc(); 50 if (type == ARBRE) feuille(); 51 } 52 53 void Arbre::getBoundingBoxPoints() { 54 Vertex u = rotation.u * limitLength() / 2.f; 55 Vertex l = rotation.l * limitLength() / 2.f; 56 Quad c(start +u +l, start -u +l, start -u -l, start +u -l); 57 addBBPoints(c, length + limitLength()); 58 } 59 60 float Arbre::LODFactor() { 61 return 4.f; 62 } 63 64 Vertex Arbre::end(float position) const { 65 return (start + rotation.h * length * position); 66 } 67 68 float Arbre::tauxMax() { 69 return 0.6f; 70 } 71 const float Arbre::limitLengthFactor = calcLimitLengthFactor(); 72 float Arbre::calcLimitLengthFactor() { 73 float limit = 0; 74 for (float i = 1; i > 0.001; i = i * tauxMax()) 75 limit += i; 76 return limit - 1; 77 } 78 79 float Arbre::limitLength() const { 80 return length * limitLengthFactor; 81 } 82 83 float Arbre::maxRadius(float length) { 84 return length * (1+limitLengthFactor); 85 } 86 87 void Arbre::tronc() { 88 float radius = length/16; 89 Vertex hTronc = end(1.f) - start; 90 Vertex uTronc = rotation.u * radius; 91 Vertex lTronc = rotation.l * radius; 92 Quad cTronc(start +uTronc +lTronc, start -uTronc +lTronc, start -uTronc -lTronc, start +uTronc -lTronc); 93 addGPUQuad(cTronc + hTronc, Couleurs::tronc); 94 addGPUFourQuads(cTronc, cTronc + hTronc, Couleurs::tronc); 95 } 96 97 void Arbre::feuille() { 98 Vertex hFeuillage = rotation.h * limitLength(); 99 Vertex uFeuillage = rotation.u * limitLength() / 2.f; 100 Vertex lFeuillage = rotation.l * limitLength() / 2.f; 101 Vertex startFeuillage = end(1.f); 102 103 unsigned int c = Couleurs::feuillage; 104 if (length < 20 && proba(seed, 12345, 0.04f)) 105 c = Couleurs::pomme; 106 107 Quad cFeuillage(startFeuillage +uFeuillage +lFeuillage, startFeuillage -uFeuillage +lFeuillage, startFeuillage -uFeuillage -lFeuillage, startFeuillage +uFeuillage -lFeuillage); 108 addGPUOcto(cFeuillage, cFeuillage + hFeuillage, c); 109 if (length < 20 && proba(seed, 12346, 0.1f)) { 110 Triangle tPapillon1(startFeuillage +uFeuillage +lFeuillage, startFeuillage -uFeuillage +lFeuillage, startFeuillage + hFeuillage * floatInRange(seed, 23448, -1, 0.3)); 111 Triangle tPapillon2(startFeuillage -uFeuillage -lFeuillage, startFeuillage +uFeuillage -lFeuillage, startFeuillage + hFeuillage * floatInRange(seed, 23448, -1, 0.3)); 112 addGPUTriangle(tPapillon1 + (hFeuillage * floatInRange(seed, 12347, 3, 6)), Couleurs::papillon); 113 addGPUTriangle(tPapillon2 + (hFeuillage * floatInRange(seed, 12347, 3, 6)), Couleurs::papillon); 114 } 115 }