-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
executable file
·376 lines (332 loc) · 8.53 KB
/
utils.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2011-2012 Michal Hozza ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
#include <png.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int Utils::sgn(float v)
{
if (v<0) return -1;
if (v>0) return 1;
return 0;
}
bool Utils::loadPngImage(const char *name, float &outWidth, float &outHeight, bool &outHasAlpha, GLubyte **outData)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
//int color_type, interlace_type;
FILE *fp;
if ((fp = fopen(name, "rb")) == NULL)
return false;
/* Create and initialize the png_struct
* with the desired error handler
* functions. If you want to use the
* default stderr and longjump method,
* you can supply NULL for the last
* three parameters. We also supply the
* the compiler header file version, so
* that we know if the application
* was compiled with a compatible version
* of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return false;
}
/* Allocate/initialize the memory
* for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return false;
}
/* Set error handling if you are
* using the setjmp/longjmp method
* (this is the normal method of
* doing things with libpng).
* REQUIRED unless you set up
* your own error handlers in
* the png_create_read_struct()
* earlieb.
*/
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated
* with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
/* If we get here, we had a
* problem reading the file */
return false;
}
/* Set up the output control if
* you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already
* read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/*
* If you have enough memory to read
* in the entire image at once, and
* you need to specify only
* transforms that can be controlled
* with one of the PNG_TRANSFORM_*
* bits (this presently excludes
* dithering, filling, setting
* background, and doing gamma
* adjustment), then you can read the
* entire image (including pixels)
* into the info structure with this
* call
*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to
* expand a palette into RGB
*/
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, png_voidp_NULL);
outWidth = (float)info_ptr->width;
outHeight = (float)info_ptr->height;
switch (info_ptr->color_type) {
case PNG_COLOR_TYPE_RGBA:
outHasAlpha = true;
break;
case PNG_COLOR_TYPE_RGB:
outHasAlpha = false;
break;
default:
std::cout << "Color type " << info_ptr->color_type << " not supported" << std::endl;
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return false;
}
unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
*outData = (unsigned char*) malloc(row_bytes * outHeight);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < outHeight; i++) {
// note that png is ordered top to
// bottom, but OpenGL expect it bottom to top
// so the order or swapped
memcpy(*outData+(row_bytes * ((int)outHeight-1-i)), row_pointers[i], row_bytes);
}
/* Clean up after the read,
* and free any memory allocated */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
/* Close the file */
fclose(fp);
/* That's it */
return true;
}
float Utils::getDistance(float x1, float y1, float z1, float x2, float y2, float z2)
{
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
}
void Utils::hsv2rgb(float h, float s, float v, float &outR, float &outG, float &outB)
{
int var_i;
float var_1,var_2, var_3,var_h;
if ( s == 0 ) //HSV from 0 to 1
{
outR = outG = outB = v;
return;
}
else
{
var_h = h * 6;
if ( var_h == 6 )
var_h = 0; //H must be < 1
var_i = floor( var_h ); //Or ... var_i = floor( var_h )
var_1 = v * ( 1 - s );
var_2 = v * ( 1 - s * ( var_h - var_i ) );
var_3 = v * ( 1 - s * ( 1 - ( var_h - var_i ) ) );
switch (var_i)
{
case 0:
outR = v;
outG = var_3;
outB = var_1;
break;
case 1:
outR = var_2;
outG = v;
outB = var_1;
break;
case 2:
outR = var_1;
outG = v;
outB = var_3;
break;
case 3:
outR = var_1;
outG = var_2;
outB = v;
break;
case 4:
outR = var_3;
outG = var_1;
outB = v;
break;
case 5:
outR = v;
outG = var_1;
outB = var_2;
break;
}
}
}
MyVertex::MyVertex()
{
setParams(0,0,0,0,0,0,0,0);
}
MyVertex::MyVertex(float x, float y, float z, float tx, float ty)
{
setParams(x,y,z,tx,ty,x,y,z);
}
void MyVertex::setParams(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}
void MyVertex::setParams(float x, float y, float z, float tx, float ty)
{
setParams(x,y,z);
this->tx = tx;
this->ty = ty;
}
void MyVertex::setParams(float x, float y, float z, float tx, float ty, float nx, float ny, float nz)
{
setParams(x,y,z,tx,ty);
this->nx = nx;
this->ny = ny;
this->nz = nz;
}
void MyVertex::makeTexturePoints(float w, float h, CoordMapType xmap, CoordMapType ymap)
{
float xval = 0, yval = 0;
switch(xmap)
{
case MAP_X:
xval = x;
break;
case MAP_Y:
xval = y;
break;
case MAP_Z:
xval = z;
break;
}
switch(ymap)
{
case MAP_X:
yval = x;
break;
case MAP_Y:
yval = y;
break;
case MAP_Z:
yval = z;
break;
}
if(w!=0)
this->tx = xval/w;
if(h!=0)
this->ty = 1-yval/h;
}
void MyVertex::makeNormals(float xoffset, float yoffset, float zoffset)
{
nx = x+xoffset;
ny = y+yoffset;
nz = z+zoffset;
}
bool Collisions::block2sphere(Block b, float x, float y, float z, float radius)
{
Point c = closest(b,x,y,z);
float distance = Utils::getDistance(c.x, c.y, c.z, x, y, z);
if(distance<radius)
{
return true;
}
return false;
}
bool Collisions::block2block(Block b1, Block b2)
{
Point center1, center2;
center1.x = b1.x+b1.width/2;
center1.y = b1.y+b1.height/2;
center1.z = b1.z+b1.depth/2;
center2.x = b2.x+b2.width/2;
center2.y = b2.y+b2.height/2;
center2.z = b2.z+b2.depth/2;
Point closest1 = Collisions::closest(b1,center2.x,center2.y,center2.z);
Point closest2 = Collisions::closest(b2,center1.x, center1.y, center1.z);
if(Utils::sgn(center1.x-center2.x)!= Utils::sgn(closest1.x-closest2.x) &&
Utils::sgn(center1.y-center2.y)!= Utils::sgn(closest1.y-closest2.y) &&
Utils::sgn(center1.z-center2.z)!= Utils::sgn(closest1.z-closest2.z))
{
//cout << "collision" << endl;
return true;
}
return false;
}
Point Collisions::closest(Block b, float x, float y, float z)
{
Point c(0,0,0);
if(x < b.x)
{
c.x = b.x;
}
else if(x > b.x + b.width)
{
c.x = b.x + b.width;
}
else
{
c.x = x;
}
if(y < b.y)
{
c.y = b.y;
}
else if(y > b.y + b.height)
{
c.y = b.y + b.height;
}
else
{
c.y = y;
}
if(z < b.z)
{
c.z = b.z;
}
else if(z > b.z + b.depth)
{
c.z = b.z + b.depth;
}
else
{
c.z = z;
}
return c;
}