Bubba-3D  0.9.0
Awesome game engine!
Utils.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 <linmath/float3x3.h>
18 #include "assimp/material.h"
19 #include "Utils.h"
20 #include "ShaderProgram.h"
21 #include "GameObject.h"
22 
23 
24 
25 chag::float4x4 convertAiMatrixToFloat4x4(aiMatrix4x4 fromMatrix) {
26  chag::float4x4 newMatrix;
27  newMatrix.c1 = chag::make_vector(fromMatrix.a1, fromMatrix.b1, fromMatrix.c1, fromMatrix.d1);
28  newMatrix.c2 = chag::make_vector(fromMatrix.a2, fromMatrix.b2, fromMatrix.c2, fromMatrix.d2);
29  newMatrix.c3 = chag::make_vector(fromMatrix.a3, fromMatrix.b3, fromMatrix.c3, fromMatrix.d3);
30  newMatrix.c4 = chag::make_vector(fromMatrix.a4, fromMatrix.b4, fromMatrix.c4, fromMatrix.d4);
31  return newMatrix;
32 }
33 
34 chag::float3x3 convertAiMatrixToFloat3x3(aiMatrix3x3 fromMatrix) {
35  chag::float3x3 newMatrix;
36  newMatrix.c1 = chag::make_vector(fromMatrix.a1, fromMatrix.b1, fromMatrix.c1);
37  newMatrix.c2 = chag::make_vector(fromMatrix.a2, fromMatrix.b2, fromMatrix.c2);
38  newMatrix.c3 = chag::make_vector(fromMatrix.a3, fromMatrix.b3, fromMatrix.c3);
39  return newMatrix;
40 }
41 
42 bool fequals(double a, double b) {
43  return fabs(a - b) < 0.01f;
44 }
45 
46 float degreeToRad(const float degree) {
47  return (float) (degree * M_PI / 180);
48 }
49 
50 float radToDegree(const float rad) {
51  return (float) (rad * 180 / M_PI);
52 }
53 
54 float getRand(const float min, const float max) {
55  //srand(time(NULL));
56  const float range = max - min;
57  return (((float) rand() / (float) RAND_MAX) * range) + min;
58 }
59 
60 template <typename T, unsigned S>
61 T getRandomElem(const T (&ts)[S])
62 {
63  if (S > 0) {
64  int ix = (int) getRand(0.0f, S - 0.1f);
65  return ts[ix];
66  } else {
67  return NULL;
68  }
69 }
70 
71 void updateMinAndMax(const float x, const float y, const float z, chag::float3* minV, chag::float3* maxV) {
72  if (x < minV->x) {
73  minV->x = x;
74  }
75 
76  if (y < minV->y) {
77  minV->y = y;
78  }
79 
80  if (z < minV->z) {
81  minV->z = z;
82  }
83 
84  if (x > maxV->x) {
85  maxV->x = x;
86  }
87 
88  if (y > maxV->y) {
89  maxV->y = y;
90  }
91 
92  if (z > maxV->z) {
93  maxV->z = z;
94  }
95 }
96 
97 chag::float3 createRandomVector(const float minValue, const float maxValue){
98  return chag::make_vector(getRand(minValue,maxValue), getRand(minValue,maxValue), getRand(minValue,maxValue));
99 }