-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_functions.cpp
More file actions
206 lines (144 loc) · 3.59 KB
/
helper_functions.cpp
File metadata and controls
206 lines (144 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* \file helper_functions.cpp
* \author Thomas R. Carrel
*
* \brief Defines some useful helper functions.
*/
#include<string>
#include<sstream>
using std::stringstream;
#include<fstream>
#include<cctype>
#include<glm/glm.hpp>
#include<SOIL/SOIL.h>
#include "helper_functions.h"
/**
* Converts a hex integer to a vec4 color.
* @param color The hex value of the desired color, RGBA-format, 8-bits per
* field.
*/
glm::vec4 to_vec_color( uint32_t color )
{
float rgba[4];
rgba[0] = (float) ((color & 0xFF000000 ) >> 24 );
rgba[1] = (float) ((color & 0x00FF0000 ) >> 16 );
rgba[2] = (float) ((color & 0x0000FF00 ) >> 8 );
rgba[3] = (float) (color & 0x000000FF );
return glm::vec4(
rgba[0] / 255.0f,
rgba[1] / 255.0f,
rgba[2] / 255.0f,
rgba[3] / 255.0f );
}
/** Converts and integer to a string.
* @param i The integer.
*/
string numtoa( const uint32_t& i )
{
stringstream S;
S << i;
return S.str();
}
/** Skip whitespace at the beginning of a stream.
* @param in The stream to remove whitespace from.
*/
void skip_whitespace( istream& in )
{
while( isspace( in.peek() ) )
{
in.ignore();
}
}
/** Converts a shader program filename to an internal shader name.
* @param fname The filename.
* @return The internally used shader name.
*/
string shader_filename_to_struct_name( const string& fname )
{
string name = fname.substr( 0, fname.find_first_of('.') ) + "_";
for( unsigned i = 0; i < name.length(); i++ )
{
if( 'a' <= name[i] && name[i] <= 'z' )
{
name[i] -= ('a' - 'A');
}
}
name += fname.substr( fname.find_first_of('.') )[1];
return name;
}
/** Get the raw binary data from a float in a type that can be used with
* bitwise operations.
* @param f The float to be copied.
* @return An unsigned int that has the same binary representation as the
* float that had been passed in.
*/
inline unsigned get_bits( const float& f )
{
return *((int*) &f);
}
/** Check an image for errors from the SOIL library.
* @param image The pointer to the image data that was supplied from the SOIL
* function for image loading.
* @param file The filename.
*/
void check_SOIL_error( unsigned char* image, const string& file )
{
if( image )
{
return;
}
fprintf(
stderr,
"Image Load Error < %s > %s\n",
file.c_str(),
SOIL_last_result()
);
}
/** Get the refractive index (n) of common materials.
* @param material The name of the material being requested. No error
* checking is done, so this should be all caps.
* @return The refractive index of the material.
*/
float get_refractive_index( const string& material )
{
if( material == "AIR" )
{
return 1.000277f;
}
if( material == "WATER" )
{
return 1.33f;
}
if( material == "ICE" )
{
return 1.309f;
}
if( material == "GLASS" )
{
return 1.52f;
}
if( material == "DIAMOND" )
{
return 2.417f;
}
if( material == "OIL" )
{
return 1.518f;
}
if( material == "GERMANIUM" )
{
return 4.05f;
}
//Vacuum
return 1.00f;
}
/** Get the ratio of refractivity.
* @param from The name of the material the object is meant to be 'submerged'
* in.
* @param to The name of the material the object is meant to be made of.
* @return from/to
*/
float get_refraction_ratio( const string& from, const string& to )
{
return get_refractive_index( from ) / get_refractive_index( to );
}