Bubba-3D  0.9.0
Awesome game engine!
Triangle.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "Triangle.h"
18 
19 using namespace chag;
20 
21 Triangle::Triangle(float3 p1, float3 p2, float3 p3)
22  :p1(p1), p2(p2), p3(p3)
23 {
24  box = calculateBoundingBox();
25 }
26 
27 Triangle::~Triangle(void) {
28 
29 }
30 
31 BoundingBox* Triangle::getBoundingBox(){
32  return &box;
33 }
34 
35 BoundingBox Triangle::calculateBoundingBox(){
36  float maxX = fmax(fmax(p1.x, p2.x), p3.x);
37  float maxY = fmax(fmax(p1.y, p2.y), p3.y);
38  float maxZ = fmax(fmax(p1.z, p2.z), p3.z);
39 
40  float minX = fmin(fmin(p1.x, p2.x), p3.x);
41  float minY = fmin(fmin(p1.y, p2.y), p3.y);
42  float minZ = fmin(fmin(p1.z, p2.z), p3.z);
43 
44  BoundingBox b;
45 
46  b.points[0] = make_vector(maxX, maxY, maxZ);
47  b.points[1] = make_vector(maxX, minY, maxZ);
48  b.points[2] = make_vector(minX, maxY, maxZ);
49  b.points[3] = make_vector(minX, minY, maxZ);
50 
51  b.points[4] = make_vector(maxX, maxY, minZ);
52  b.points[5] = make_vector(maxX, minY, minZ);
53  b.points[6] = make_vector(minX, maxY, minZ);
54  b.points[7] = make_vector(minX, minY, minZ);
55 
56  return b;
57 }