www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

chose.cpp (5324B)


      1 #include "all_includes.hh"
      2 
      3 Chose::Chose() : seed(initialSeed), children() {
      4 }
      5 
      6 void Chose::clearChildren() {
      7 	std::vector<Chose*>::iterator it;
      8 	for (it = children.begin(); it != children.end(); it++)
      9 		delete *it;
     10 	children.clear();
     11 }
     12 
     13 void Chose::clearTriangles() {
     14 	std::vector<GPUTriangle*>::iterator it;
     15 	for (it = triangles.begin(); it != triangles.end(); it++)
     16 		delete *it;
     17 	triangles.clear();
     18 }
     19 
     20 Chose::~Chose() {
     21 	clearChildren();
     22 	clearTriangles();
     23 }
     24 
     25 void Chose::addChild(Chose* c) {
     26 	children.push_back(c);
     27 }
     28 
     29 void Chose::merge() {
     30 	clearChildren();
     31 }
     32 
     33 void Chose::addGPUTriangle(Vertex left, Vertex top, Vertex right, unsigned int rgb) {
     34 	triangles.push_back(new GPUTriangle(left, top, right, Couleurs::r(rgb), Couleurs::g(rgb), Couleurs::b(rgb)));
     35 }
     36 
     37 void Chose::addGPUTriangle(Triangle t, unsigned int rgb) {
     38 	addGPUTriangle(t[LEFT], t[TOP], t[RIGHT], rgb);
     39 }
     40 
     41 void Chose::addGPUQuad(Vertex ne, Vertex se, Vertex sw, Vertex nw, unsigned int rgb) {
     42 	this->addGPUTriangle(nw, ne, se, rgb);
     43 	this->addGPUTriangle(se, sw, nw, rgb);
     44 }
     45 
     46 void Chose::addGPUQuad(Quad q, unsigned int rgb) {
     47 	addGPUQuad(q[NE], q[SE], q[SW], q[NW], rgb);
     48 }
     49 
     50 void Chose::addGPUFourQuads(Quad q, Quad qh, unsigned int rgb) {
     51 	for (int i = 0; i < 4; i++)
     52 		addGPUQuad(Quad(qh[NE+i], q[NE+i], q[SE+i], qh[SE+i]), rgb);
     53 }
     54 
     55 void Chose::addGPUThreeQuads(Triangle t, Triangle th, unsigned int rgb) {
     56 	for (int i = 0; i < 3; i++)
     57 		addGPUQuad(Quad(th[LEFT+i], t[LEFT+i], t[TOP+i], th[TOP+i]), rgb);
     58 }
     59 
     60 void Chose::addGPUOcto(Vertex ne, Vertex se, Vertex sw, Vertex nw,
     61 		Vertex neh, Vertex seh, Vertex swh, Vertex nwh, unsigned int rgb) {
     62 	addGPUOcto(Quad(ne,se,sw,nw), Quad(neh,seh,swh,nwh), rgb);
     63 }
     64 
     65 void Chose::addGPUOcto(Quad q, Quad qh, unsigned int rgb) {
     66 	this->addGPUQuad(q[SE], q[NE], q[NW], q[SW], rgb);
     67 	this->addGPUQuad(qh[NE], qh[SE], qh[SW], qh[NW], rgb);
     68 	for (int i = 0; i < 4; i++)
     69 		this->addGPUQuad(q[NE+i], q[SE+i], qh[SE+i], qh[NE+i], rgb);
     70 }
     71 
     72 void Chose::display() {
     73 	if (children.size() > 0) {
     74 		std::vector<Chose*>::iterator it;
     75 		for (it = children.begin(); it != children.end(); ++it) {
     76 			(*it)->display();
     77 		}
     78 	} else {
     79 		std::vector<GPUTriangle*>::iterator it;
     80 		for (it = triangles.begin(); it != triangles.end(); ++it) {
     81 			(*it)->display();
     82 		}
     83 	}
     84 }
     85 
     86 void Chose::displayNormals() {
     87 	if (children.size() > 0) {
     88 		std::vector<Chose*>::iterator it;
     89 		for (it = children.begin(); it != children.end(); ++it) {
     90 			(*it)->displayNormals();
     91 		}
     92 	} else {
     93 		std::vector<GPUTriangle*>::iterator it;
     94 		for (it = triangles.begin(); it != triangles.end(); ++it) {
     95 			(*it)->displayNormal();
     96 		}
     97 	}
     98 }
     99 
    100 void Chose::addBBPoint(const Vertex v) {
    101 	if (lod.firstBBPoint) {
    102 		lod.firstBBPoint = false;
    103 		lod.aabb[0] = v.x;
    104 		lod.aabb[1] = v.x;
    105 		lod.aabb[2] = v.y;
    106 		lod.aabb[3] = v.y;
    107 		lod.aabb[4] = v.z;
    108 		lod.aabb[5] = v.z;
    109 	} else {
    110 		lod.aabb[0] = std::min(lod.aabb[0], v.x);
    111 		lod.aabb[1] = std::max(lod.aabb[1], v.x);
    112 		lod.aabb[2] = std::min(lod.aabb[2], v.y);
    113 		lod.aabb[3] = std::max(lod.aabb[3], v.y);
    114 		lod.aabb[4] = std::min(lod.aabb[4], v.z);
    115 		lod.aabb[5] = std::max(lod.aabb[5], v.z);
    116 	}
    117 }
    118 
    119 void Chose::addBBPoints(const Triangle t) {
    120 	addBBPoint(t[LEFT]);
    121 	addBBPoint(t[TOP]);
    122 	addBBPoint(t[RIGHT]);
    123 }
    124 
    125 void Chose::addBBPoints(const Triangle t, float height) {
    126 	addBBPoints(t);
    127 	addBBPoints(t.offsetNormal(height));
    128 }
    129 
    130 void Chose::addBBPoints(const Quad q) {
    131 	addBBPoint(q[NE]);
    132 	addBBPoint(q[SE]);
    133 	addBBPoint(q[SW]);
    134 	addBBPoint(q[NW]);
    135 }
    136 
    137 void Chose::addBBPoints(const Quad q, float height) {
    138 	addBBPoints(q);
    139 	addBBPoints(q.offsetNormal(height));
    140 }
    141 
    142 void Chose::updateAABB() {
    143 	float thisSplitFactor = Dimensions::splitFactor * LODFactor();
    144 	// TODO : adapt mergeFactor to Camera::moveSensitivity
    145 	float thisMergeFactor = thisSplitFactor * std::max(1.01f, Dimensions::mergeFactor);
    146 	float nonFacingFactor = 2.f/3.f;
    147 	lod.firstBBPoint = true;
    148 	getBoundingBoxPoints();
    149 	float size[3];
    150 	float center[3];
    151 	for (int i = 0; i < 3; i++) {
    152 		size[i] = lod.aabb[2*i+1] - lod.aabb[2*i];
    153 		center[i] = (lod.aabb[2*i] + lod.aabb[2*i+1])/2.f;
    154 	}
    155 	float areaFacing[3];
    156 	for (int i = 0; i < 3; i++)
    157 		areaFacing[i] = size[(i+1)%3]*size[(i+1)%3];
    158 	for (int i = 0; i < 3; i++) {
    159 		float pseudoLength = std::max(size[i]/2.f, std::sqrt(areaFacing[i] + areaFacing[(i+1)%3] * nonFacingFactor + areaFacing[(i+1)%3] * nonFacingFactor));
    160 		float splitDistance = thisSplitFactor * pseudoLength;
    161 		float mergeDistance = thisMergeFactor * pseudoLength;
    162 		lod.splitBox[2*i]   = center[i] - splitDistance;
    163 		lod.splitBox[2*i+1] = center[i] + splitDistance;
    164 		lod.mergeBox[2*i]   = center[i] - mergeDistance;
    165 		lod.mergeBox[2*i+1] = center[i] + mergeDistance;
    166 	}
    167 }
    168 
    169 float Chose::LODFactor() {
    170 	return 1.f;
    171 }
    172 
    173 // DEBUG
    174 void Chose::drawAABB() {
    175 	addGPUOcto(
    176 			Vertex(lod.splitBox[0], lod.splitBox[2], lod.splitBox[4]),
    177 			Vertex(lod.splitBox[1], lod.splitBox[2], lod.splitBox[4]),
    178 			Vertex(lod.splitBox[1], lod.splitBox[3], lod.splitBox[4]),
    179 			Vertex(lod.splitBox[0], lod.splitBox[3], lod.splitBox[4]),
    180 			Vertex(lod.splitBox[0], lod.splitBox[2], lod.splitBox[5]),
    181 			Vertex(lod.splitBox[1], lod.splitBox[2], lod.splitBox[5]),
    182 			Vertex(lod.splitBox[1], lod.splitBox[3], lod.splitBox[5]),
    183 			Vertex(lod.splitBox[0], lod.splitBox[3], lod.splitBox[5]),
    184 			hash2(seed, 42) & 0xffffff
    185 	);
    186 }
    187 
    188 unsigned int Chose::initialSeed = random_seed();