/
tinylib.h
5717 lines (4646 loc) · 227 KB
/
tinylib.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
--------------------------------------------------------------------------------
oooo
`888
oooo d8b .ooooo. oooo ooo 888 oooo oooo
`888""8P d88' `88b `88b..8P' 888 `888 `888
888 888 888 Y888' 888 888 888
888 888 888 .o8"'88b 888 888 888
d888b `Y8bod8P' o88' 888o o888o `V88V"V8P'
www.roxlu.com
www.apollomedia.nl
--------------------------------------------------------------------------------
Tiny library with a couple of handy functions for opengl based applications.
Send changes to: https://github.com/roxlu/tinylib
You can find more in depth documentation on [Read The Docs](http://tiny-lib.readthedocs.org/en/latest/index.html)
TODO:
-----
- <del>we're using GLXW to include GL headers now, because default windows headers are GL 1</del>
instead we use glad: https://github.com/Dav1dde/glad
- implement orthonormal basis from direction vector: http://orbit.dtu.dk/fedora/objects/orbit:113874/datastreams/file_75b66578-222e-4c7d-abdf-f7e255100209/content
By Jeppe Revall Frisvad, http://www.imm.dtu.dk/~jerf/,
USAGE
------
The tinylib.h file contains the function definitions and declarations. In your
header files you only use ROXLU_USE_{ALL, OPENGL, PNG, etc..} but in your main.cpp
file you use all the defines that you use in your app (e.g. ROXLU_USE_ALL) +
ROXLU_IMPLEMENTATION. When you add ROXLU_IMPLEMENTATION, which is only allowed in a
.cpp file, all the source code will be added to so your application can link with it.
#define ROXLU_USE_ALL - to include all code
#define ROXLU_USE_OPENGL - to use the opengl code
#define ROXLU_USE_PNG - to use the png loader and saver (needs libpng)
#define ROXLU_USE_JPG - add support for jpg loading
#define ROXLU_USE_MATH - to use the vec2, vec3, vec4, mat4 classes
#define ROXLU_USE_FONT - to make the PixelFont font available
#define ROXLU_USE_AUDIO - to use AudioPlayer for simple 44100,2-channel audio playback (need libcubeb)
#define ROXLU_USE_CURL - enable some curl helpers
#define ROXLU_USE_LOG - use the logging features
MACROS
===================================================================================
HALF_PI
PI
TWO_PI
FOUR_PI
DEG_TO_RAD
RAD_TO_DEG
LOWEST(a, b)
HIEGHEST(a, b)
CLAMP(val, min, max)
ABS(x)
DX(i, j, w)
IS_INSIDE(mousex, mousey, x, y, w, h)
SLEEP_MILLIS(ms)
OPENGL - define `ROXLU_USE_OPENGL` before including
===================================================================================
rx_create_shader(GL_VERTEX_SHADER, source_char_p); - create a shader, pass type
rx_create_shader_from_file(GL_VERTEX_SHADER, path); - create a shader from a file, give a full path, returns -1 on error
rx_create_program(vert, frag, link); - create a program, linkes when link == true
rx_create_program_with_attribs(vert, frag, 2, attr); - create a program with attribs + LINKS THE PROGRAM
rx_print_shader_link_info(prog) - print the program link info
rx_print_shader_compile_info(vert) - print the shader compile info
rx_create_texture(filepath) - loads a png and creates a texture (only when png is enabled)
rx_get_uniform_location(prog, name) - returns the location of the uniform or returns -1 and logs something in debug mode
rx_uniform_1i(prog, name, value) - set a 1i value - make sure to activate the shader before calling, e.g. glUseProgram(prog)
rx_uniform_1f(prog, name, value) - set a 1f value - make sure to activate the shader before calling, e.g. glUseProgram(prog)
rx_uniform_mat4fv(prog, name, count, trans, ptr) - set a mat4fv - make sure to activate the shader before calling, e.g. glUseProgram(prog)
rx_create_png_screenshot(filepath) - create a png file from the current framebuffer (must have PNG enabled)
rx_create_jpg_screenshot(filepath, quality) - create a jpg file from the current framebuffer (must have JPG enabled)
Shader - represents a GL shader; only works with files!
Shader.load(type, filepath) - load the source for the shader from file
Shader.compile() - compiles the shader
Program - represents a GL program
Program.add(Shader) - add a already compiled shader to the program
Program.create(type, filepath, extra) - create a Shader for the given filepath. extra is extra source code that gets prepended.
Program.link(nattribs, char** attribs, int nfrags, const char** frags) - link + bind the attrib locations and/or fragment locations
Program.recompile() - recompiles the shader and links the program again
VertexP - vertex type for position data
VertexPC - vertex type for position and color (4) data
VertexPT - vertex type for position and texture coord data
VertexPT3 - vertex type for position and texture coord with 3 elements (a q for projective mapping)
VertexPTN - vertex type for position, texture and normal
VertexPN - vertex type for position and normals
OBJ - class to load OBJ files
OBJ.load(filepath) - load the .obj file, returns boolean
OBJ.hasNormals() - returns true if the loaded obj has normals
OBJ.hasTexCoords() - returns true if the loaded obj had texcoords
OBJ.copy(std::vector<VertexPT>&) - copy the loaded vertices
Painter - simple helper to draw lines, circles, rectangles, textures with GL 3.x
Painter.init() - must be called to ininitialize the GL-objects.
Painter.clear() - clear all added elements, resets the canvas
Painter.draw() - draw the added shapes
Painter.rect(x, y, w, h) - draw a rectangle
Painter.circle(x, y, radius) - draw a circle at x/y with radius
Painter.resolution(6) - sets the circle resolution
Painter.line(x0, y0, x1, y1) - draw one line
Painter.texture(texid, x, y, w, h) - draw a texture
Painter.color(r,g,b,a) - set the draw color
Painter.fill() - draw filled shapes
Painter.nofill() - draw only outlines
-
Painter.resize(w,h) - call this when the viewport size changes
Painter.width() - return the last set or queried viewport width
Painter.height() - return the last set of queried viewport height
-
Painter.begin(GL_LINE_STRIP) - begin drawing a type
Painter.vertex(x, y) - add a vertex to the "begin/end" section
Painter.end() - flush added vertices and make sure their drawn
-
FONT = define `ROXLU_USE_FONT` before include
===================================================================================
PixelFont font; - creates an instance of the FreePixel font.
font.clear() - clear all previously added text (make sure to clear() when you want to draw text that changes every time)
font.write(10, 10, "Lorem ipsum_dolor sit") - write some text at 10,10
font.draw() - draws all the batched writes
font.color(r, g, b, a) - set the font color
font.resize(w, h) - when the viewport size changes you must call this.
IMAGES - define `ROXLU_USE_PNG` before including - - see https://gist.github.com/roxlu/9b9d555cf784385d67ba for some loading examples
===================================================================================
Both the jpg and png loaders can reallocate the given pixel buffer so that
it will be big enough to load the image. For this to work you need to pass
the current capacity of the buffer and make sure that *pixels is not NULL.
RX_FLAG_LOAD_AS_RGBA
You can load a png and convert it directly to 4 channels, which helps you
when you want to upload pixels to the GPU using the optimal transfer path.
pass the flag, RX_FLAG_LOAD_AS_RGBA
bool rx_save_png("filename.png", pixels, 640, 480, 3, flip); - writes a png using lib png, set flip = true if you want to flip horizontally
int rx_load_png("filepath.png", &pix, width, height, nchannels) - load the pixels, width, height and nchannels for the given filepath. make sure to delete pix (which is unsigned char*)
int rx_load_png("filepath.png", &pix, width, height, nchannels, &allocated, flags) - load the pixels, the allocated param should point to an integer that holds the number of bytes in the *pix buffer. It will try to reuse or reallocate this buffer. Returns number of bytes in image buffer. You can pass RX_FLAG_LOAD_AS_RGBA if you want to force the output as RGBA
int rx_load_jpg("filepath.jpg", &pix, width, height, nchannels) - loads an jpg file, allocates the buffer that you need to free, will return the number of bytes (int)
int rx_load_jpg("filepath.jpg", &pix, width, height, nchannels, &allocated) - loads an jpg file. the allocated should point to an integer that holds the number of bytes in the *pix buffer. It will try to reuse this or reallocate the buffer if needed. this will return the number of allocated bytes
bool rx_save_jpg("filepath.jpg", pixels, 640, 480, 3); - save a jpg file
UTILS
===================================================================================
rx_to_float("0.15"); - convert a string to float
rx_to_int("10"); - convert a string to integer
rx_int_to_string(15); - convert an integer to string
rx_float_to_string(5.5); - convert a float to string
rx_string_id(std::string& v); - generate an mostly unique uint32_t id for the given string.
rx_get_exe_path(); - returns the path to the exe
rx_read_file("filepath.txt"); - returns the contents of the filepath.
rx_set_data_path("/path/to/data/") - used to set a custom data path.
rx_to_data_path("filename.txt") - convert the given filename to the data dir
rx_is_dir("path") - returns true when the path is a dir
rx_strip_filename("/path/filename") - removes the filename from the given path
rx_strip_file_ext("/path/filename") - removes the extension from the given filename, including the dot. e.g. image.jpg becomes "image"
rx_strip_dir("/path/filename") - removes the path from the given path leaving only the filename
rx_create_dir("/path/to") - creates the given directory
rx_create_path("/a/b/c") - creates the path. all subdirectories too)
rx_norm_path("/some/path/") - creates a normalized, cross platform path. always pass in forward slashes; on windows you'll get backslashes
rx_get_files("/path/", "jpg") - returns a std::vector<std::string> with the files found in the given dir which have the given extension.
rx_get_files("/path/", "*") - returns all the files in the path.
rx_get_file_ext("/file/path.jpg") - returns the extension of a file ("jpg", "gif", etc..)
rx_split("string", '/') - splits a string on the given character returning std::vector<std::string>
rx_string_replace("string", "replace", "with") - replaces characters or string from one to another
rx_to_lower("String With Caps") - returns the lowercased version of the given string.
rx_hrtime() - high resolution timer (time in nano sec)
rx_millis() - returns the elapsed millis since the first call as float, 1000 millis returns 1.0
rx_strftime("%Y/%m%d/") - strftime wrapper
rx_get_time_string() - get a unique string with the current date time: 2014.01.16_13.12_883 yyyy.mm.dd.hh.ss.iii (ii = millis)
rx_get_date_string() - get a unique string with the current date 2014.01.16
rx_get_year() - get the current year as int, e.g. 2014
rx_get_month() - get the current month as int [00-11]
rx_get_day() - get the day of the month [00-31]
rx_get_hour() - get the hour of day [00-23]
rx_get_minute() - get the minuts of the hours, [00-59]
rx_rgb_to_hsv(r,g,b,h,s,v) - convert rgb in range 0-1 to hsv in the same range. h,s,v are references
rx_rgb_to_hsv(rgb, hsv) - convert given vector, hsv will be set (reference)
rx_rgb_to_hsv(rgb, float*) - "", different typed parameters
rx_rgb_to_hsv(float*, float*) - "", ""
rx_hsv_to_rgb(h,s,v,r,g,b) - convert rgb to hsv all in 0-1 range. r,g,b will be set, are references
rx_hsv_to_rgb(hsv, rgb) - convert rgb to hsv all in 0-1 range. rgb will be set, is a reference
rx_hsv_to_rgb(hsv, float*) - "", ""
rx_hsv_to_rgb(float,* float*) - "", ""
LOG
===================================================================================
rx_log_init(std::string path = "") - Initialize logging, creates a log file like: log-[date-string].log in the same directory of the executable when path is not given.
rx_log_disable_stdout() - Disable output to stdout
rx_log_enable_stdout() - Enable output to stdout
rx_log_set_level(int level) - Set the log level, RX_LOG_LEVEL_{ALL, ERROR, WARNING, VERBOSE}
RX_VERBOSE("%s %s", "Hello", "world"); - Log a verbose message
RX_WARNING("Warning, wrong input"); - Log a warning
RX_ERROR("Log an error: %s", strerror(errno)); - Log an erorr.
MATH - define `ROXLU_USE_MATH` before including.
===================================================================================
utils
-----------------------------------------------------------------------------------
float rx_random(max) - generate a random value but limit to max
float rx_random(min, max) - generate a random value between min and max
bool rx_is_power_of_two(int n); - returns true if the given number is a power of two.
float rx_map(val, inmin, inmax, outmin, outmax, clamp = true) - map one range to another one and clamp if necessary (true by default)
vec2, vec3, vec4
-----------------------------------------------------------------------------------
float length(v) - get the length of the vector
float dot(a, b) - get the dot product aka squared root
vec2 max(a) - get the biggest component value
vec2 min(a) - get the lowest component value
vec2 max(a,b) - get the biggest vector
vec2 nmin(a,b) - get the smallest vector
vec2 floor(a) - floor the components
vec2 ceil(a) - ceil the compoments
vec2 fract(a) - get the decimal part
vec2 normalized(v) - get the normalized vector
void print() - print the x and y
vec3 cross(a,b) - cross product (vec3)
vec3
-----------------------------------------------------------------------------------
vec3 perpendicular(a) - get a perpendicular vector from the given vec, this vector doesn't have to be normalized!, based on http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts
bool intersect(a,b,c,d, &result) - checks if two lines intersect line (b-a) and (d-c). resturns true when they intersect and it will set result to the intersection point
mat4
-----------------------------------------------------------------------------------
mat4& mat4.rotateX(rad)
mat4& mat4.rotateY(rad)
mat4& mat4.rotateZ(rad)
mat4& mat4.rotate(rad, x, y, z)
mat4& mat4.scale(x, y, z)
mat4& mat4.scale(s)
mat4& mat4.translate(x, y, z)
mat4& mat4.translate(vec3 v)
mat4& mat4.position(vec3 v)
mat4& mat4.position(x, y, z)
mat4& mat4.ortho(l, r, b, t, n , f) - pm.ortho(0, w, h, 0, 0.0f, 100.0f);
mat4& mat4.frustum(l, r, b, t, n, f)
mat4& mat4.perspective(fov, aspect, near, far) - create a perspective projection matrix
mat4& mat4.lookat(eye, pos, up)
void mat4.print()
float* mat4.ptr() - get a pointer to the data
<example>
// create an ortho matrix with 0,0 at the top left
mat4 m;
m.ortho(0.0f, w, h, 0, 0.0f, 100.0f);
</example>
Spline<T> - catmull rom interpolation (MAKE SURE TO USE AT LEAST 4 POINTS!)
-----------------------------------------------------------------------------------
Spline<T>.size() - returns the number of elements added
Spline<T>.clear() - removes all added elements
Spline<T>.push_back(T) - add an element
Spline<T>.assign(begin, end) - assign multiple values
Spline<T>.at(float t) - get the interpolated value at this point
<example>
Spline<float> spline;
spline.push_back(1.0);
spline.push_back(3.0);
spline.push_back(6.0);
spline.push_back(5.0);
int num = 10;
for(int i = 0; i <= num; ++i) {
float p = float(i)/num;
printf("%d: %f (perc: %f)\n",i, spline.at(p), p);
}
</example>
Perlin
-----------------------------------------------------------------------------------
Perlin noise, thanks to Ken P.
The class is based on: http://www.flipcode.com/archives/Perlin_Noise_Class.shtml
with some super tiny changes. Thanks guys!
octaves: use a value between 1 - 16, 1 = smooth, 16 = noisy, values between 4 - 8 give conventional noise results
freq: use a value between 1 - 8, which will give reasanoble results (you can use any value you want)
ampl: a value of 1, will result in values between -1 and 1
seed: random seed, eg. 94
Perlin(octaves, freq, amplitude, seed) - constructor, see values in the description above
Perlin.get(x) - get the value for this `x` range
Perlin.get(x, y) - 2d perlin
CURL - define `ROXLU_USE_CURL`
===================================================================================
rx_fetch_url(std::string url, std::string& result) - downloads an url into the given result.
rx_download_file(std::string url, std::string filepath) - downloads a file to the given filepath.
AUDIO - define `ROXLU_USE_AUDIO` (will need libcubeb: https://github.com/kinetiknz/cubeb and libsndfile, tested with cubeb version 4bc13035b143ac299a58b8467b87212fc1f5958c )
===================================================================================
AudioPlayer player; - creates a audio player
player.add(0, "bleep.wav"); - add the bleep.wav file (only 44100, 2 channel support atm)
player.add(1, "boing.wav"); - add the boing.wav with name '1'.
player.play(0, 1.0); - playback audio file 0 with volume 1.0
*/
#if defined(ROXLU_USE_ALL)
# define ROXLU_USE_OPENGL
# define ROXLU_USE_PNG
# define ROXLU_USE_MATH
# define ROXLU_USE_JPG
# define ROXLU_USE_FONT
# define ROXLU_USE_CURL
# define ROXLU_USE_AUDIO
#endif
// ------------------------------------------------------------------------------------
// T I N Y L I B
// ------------------------------------------------------------------------------------
#include <assert.h>
#include <iostream>
#include <cmath>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#if defined(_WIN32)
# include <direct.h> /* _mkdir */
# include <Shlwapi.h>
# include <stdint.h>
# include <time.h>
# include <sys/stat.h> /* _stat() */
# include <tchar.h> /* TEXT() */
# include <strsafe.h> /* TEXT() */
# if defined(ROXLU_USE_OPENGL)
//# include <GLXW/glxw.h>
# endif
#elif defined(__APPLE__)
# if defined(ROXLU_USE_OPENGL)
//# include <OpenGL/gl3.h>
# endif
/* For JPEG 9a, we need to fix this */
# if defined(ROXLU_USE_JPG)
# define TRUE 1
# define FALSE 0
# include <jpeglib.h>
# endif
# include <libgen.h> /* dirname */
# include <CoreFoundation/CFRunLoop.h>
# include <mach/mach.h>
# include <mach/mach_time.h>
# include <mach-o/dyld.h> /* _NSGetExecutablePath */
# include <sys/resource.h>
# include <sys/sysctl.h>
# include <sys/stat.h> /* stat() */
# include <unistd.h> /* sysconf */
# include <dirent.h> /* DIR */
# include <errno.h> /* errno */
# include <stdint.h>
#elif defined(__linux)
# include <string.h> /* strlen() */
# include <dirent.h> /* stat() */
# include <errno.h> /* errno */
# include <unistd.h> /* readlink(), getcwd() */
# include <sys/time.h> /* timeofday() */
# include <libgen.h> /* dirname() */
# include <stdint.h> /* uint*_t types */
# include <sys/stat.h>
# include <stdarg.h>
# if defined(ROXLU_USE_OPENGL)
//# include <GLXW/glxw.h>
# endif
# define MAX_PATH 4096
#endif
#if defined(ROXLU_USE_PNG)
# include <png.h>
#endif
#ifndef ROXLU_TINYLIB_H
#define ROXLU_TINYLIB_H
#ifndef PI
# define PI 3.14159265358979323846
#endif
#ifndef TWO_PI
# define TWO_PI 6.28318530717958647693
#endif
#ifndef M_TWO_PI
# define M_TWO_PI 6.28318530717958647693
#endif
#ifndef FOUR_PI
#define FOUR_PI 12.56637061435917295385
#endif
#ifndef HALF_PI
# define HALF_PI 1.57079632679489661923
#endif
#ifndef DEG_TO_RAD
# define DEG_TO_RAD (PI/180.0)
#endif
#ifndef RAD_TO_DEG
# define RAD_TO_DEG (180.0/PI)
#endif
#ifndef LOWEST
# define LOWEST(x,y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef HEIGHEST
# define HEIGHEST(x,y) (((x) > (y)) ? (x) : (y))
#endif
#ifndef CLAMP
# define CLAMP(val,min,max) (HEIGHEST(LOWEST(val,max),min))
#endif
#ifndef ABS
# define ABS(x) (((x) < 0) ? -(x) : (x))
#endif
#ifndef DX
# define DX(i,j,w)((j)*(w))+(i)
#endif
#ifndef IS_INSIDE /* x, y is top left corner. */
# define IS_INSIDE(mousex, mousey, x, y, w, h) ((mousex >= x) && (mousex <= (x+w)) && (mousey >= y) && (mousey <= (y+h)))
#endif
#ifndef SLEEP_MILLIS
# if defined(_WIN32)
# define SLEEP_MILLIS(n) Sleep(n);
# else
# define SLEEP_MILLIS(n) usleep(n * 1e3)
# endif
#endif
#define RX_FLAG_NONE 0x0000 /* default flag */
#define RX_FLAG_LOAD_AS_RGBA 0x0001 /* can be used by image loading functions to convert loaded data directory to RGBA. See the rx_load_png function. */
extern std::string rx_data_path;
/* file utils */
extern void rx_set_data_path(const std::string path);
extern std::string rx_get_data_path();
extern std::string rx_get_exe_path();
extern std::string rx_to_data_path(const std::string filename);
extern uint64_t rx_get_file_mtime(std::string filepath);
extern bool rx_is_dir(std::string filepath);
extern bool rx_file_exists(std::string filepath);
extern std::string rx_strip_filename(std::string path);
extern std::string rx_strip_file_ext(std::string path);
extern std::string rx_strip_dir(std::string path);
extern bool rx_create_dir(std::string path);
extern bool rx_create_path(std::string path);
extern std::string rx_get_file_ext(std::string filename);
extern std::vector<std::string> rx_get_files(std::string path, std::string ext = "");
extern std::string rx_norm_path(std::string path);
extern std::string rx_read_file(std::string filepath);
/* string and conversion utils */
extern std::string rx_string_replace(std::string, char from, char to);
extern std::string rx_string_replace(std::string, std::string from, std::string to);
extern int rx_to_int(const std::string& v);
extern uint32_t rx_string_id(const std::string& v);
extern float rx_to_float(const std::string& v);
extern std::string rx_int_to_string(const int& v);
extern std::string rx_float_to_string(const float& v);
extern std::vector<std::string> rx_split(std::string str, char delim);
extern std::string rx_to_lower(std::string str);
/* time utils */
extern uint64_t rx_hrtime();
extern float rx_millis();
extern std::string rx_strftime(const std::string fmt);
extern std::string rx_get_time_string();
extern std::string rx_get_date_string();
extern int rx_get_year();
extern int rx_get_month();
extern int rx_get_day();
extern int rx_get_hour();
extern int rx_get_minute();
#endif // ROXLU_TINYLIB_H
// ------------------------------------------------------------------------------------
// R O X L U _ U S E _ M A T H
// ------------------------------------------------------------------------------------
#if defined(ROXLU_USE_MATH)
# ifndef ROXLU_USE_MATH_H
# define ROXLU_USE_MATH_H
template<class T>
class Vec2 {
public:
Vec2();
Vec2(T x, T y);
Vec2(const Vec2<T>& o);
Vec2(T f);
void set(T vx, T vy);
T* ptr();
T& operator [](const unsigned int dx);
Vec2<T> operator + () const;
Vec2<T> operator - () const;
Vec2<T> operator + (const Vec2<T>& o) const;
Vec2<T> operator - (const Vec2<T>& o) const;
Vec2<T> operator * (const Vec2<T>& o) const;
Vec2<T> operator / (const Vec2<T>& o) const;
Vec2<T> operator + (float s) const;
Vec2<T> operator - (float s) const;
Vec2<T> operator * (float s) const;
Vec2<T> operator / (float s) const;
Vec2<T>& operator += (const Vec2<T>& o);
Vec2<T>& operator -= (const Vec2<T>& o);
Vec2<T>& operator *= (const Vec2<T>& o);
Vec2<T>& operator /= (const Vec2<T>& o);
Vec2<T>& operator += (float s);
Vec2<T>& operator -= (float s);
Vec2<T>& operator *= (float s);
Vec2<T>& operator /= (float s);
bool operator == (const Vec2<T>& o) const;
bool operator != (const Vec2<T>& o) const;
void print();
public:
T x;
T y;
}; // Vec2<T>
template<class T> inline Vec2<T>::Vec2() : x(), y() {}
template<class T> inline Vec2<T>::Vec2(T x, T y) : x(x), y(y) {}
template<class T> inline Vec2<T>::Vec2(const Vec2<T>& o) : x(o.x), y(o.y) {}
template<class T> inline Vec2<T>::Vec2(T f) : x(f), y(f) {}
template<class T> inline void Vec2<T>::set(T vx, T vy) { x = vx; y = vy; }
template<class T> inline T* Vec2<T>::ptr() { return &x; }
template<class T> inline T& Vec2<T>::operator [](const unsigned int dx) { return *(&x + dx); }
template<class T> inline Vec2<T> Vec2<T>::operator + () const { return Vec2<T>(+x, +y); };
template<class T> inline Vec2<T> Vec2<T>::operator - () const { return Vec2<T>(-x, -y); };
template<class T> inline Vec2<T> Vec2<T>::operator + (const Vec2<T>& o) const { return Vec2<T>(x + o.x, y + o.y); }
template<class T> inline Vec2<T> Vec2<T>::operator - (const Vec2<T>& o) const { return Vec2<T>(x - o.x, y - o.y); }
template<class T> inline Vec2<T> Vec2<T>::operator * (const Vec2<T>& o) const { return Vec2<T>(x * o.x, y * o.y); }
template<class T> inline Vec2<T> Vec2<T>::operator / (const Vec2<T>& o) const { return Vec2<T>(x / o.x, y / o.y); }
template<class T> inline Vec2<T> Vec2<T>::operator + (float s) const { return Vec2<T>(x + s, y + s); }
template<class T> inline Vec2<T> Vec2<T>::operator - (float s) const { return Vec2<T>(x - s, y - s); }
template<class T> inline Vec2<T> Vec2<T>::operator * (float s) const { return Vec2<T>(x * s, y * s); }
template<class T> inline Vec2<T> Vec2<T>::operator / (float s) const { return Vec2<T>(x / s, y / s); }
template<class T> inline Vec2<T> operator + (float s, const Vec2<T>& o) { return Vec2<T>(s + o.x, s + o.y); }
template<class T> inline Vec2<T> operator - (float s, const Vec2<T>& o) { return Vec2<T>(s - o.x, s - o.y); }
template<class T> inline Vec2<T> operator * (float s, const Vec2<T>& o) { return Vec2<T>(s * o.x, s * o.y); }
template<class T> inline Vec2<T> operator / (float s, const Vec2<T>& o) { return Vec2<T>(s / o.x, s / o.y); }
template<class T> inline Vec2<T>& Vec2<T>::operator += (const Vec2<T>& o) { return *this = *this + o; }
template<class T> inline Vec2<T>& Vec2<T>::operator -= (const Vec2<T>& o) { return *this = *this - o; }
template<class T> inline Vec2<T>& Vec2<T>::operator *= (const Vec2<T>& o) { return *this = *this * o; }
template<class T> inline Vec2<T>& Vec2<T>::operator /= (const Vec2<T>& o) { return *this = *this / o; }
template<class T> inline Vec2<T>& Vec2<T>::operator += (float s) { return *this = *this + s; }
template<class T> inline Vec2<T>& Vec2<T>::operator -= (float s) { return *this = *this - s; }
template<class T> inline Vec2<T>& Vec2<T>::operator *= (float s) { return *this = *this * s; }
template<class T> inline Vec2<T>& Vec2<T>::operator /= (float s) { return *this = *this / s; }
template<class T> inline bool Vec2<T>::operator == (const Vec2<T>& o) const { return x == o.x && y == o.y; }
template<class T> inline bool Vec2<T>::operator != (const Vec2<T>& o) const { return x != o.x || y == o.y; }
template<class T> inline float length(const Vec2<T>& o) { return sqrtf(o.x * o.x + o.y * o.y); }
template<class T> inline float dot(const Vec2<T> &a, const Vec2<T> &b) { return a.x * b.x + a.y * b.y; }
template<class T> inline float heighest(const Vec2<T> &v) { return fmaxf(v.x, v.y); }
template<class T> inline float lowest(const Vec2<T> &v) { return fminf(v.x, v.y); }
template<class T> inline Vec2<T> lowest(const Vec2<T> &a, const Vec2<T> &b) { return Vec2<T>(fmaxf(a.x, b.x), fmaxf(a.y, b.y)); }
template<class T> inline Vec2<T> heighest(const Vec2<T> &a, const Vec2<T> &b) { return Vec2<T>(fminf(a.x, b.x), fminf(a.y, b.y)); }
template<class T> inline Vec2<T> floor(const Vec2<T> &v) { return Vec2<T>(floorf(v.x), floorf(v.y)); }
template<class T> inline Vec2<T> ceil(const Vec2<T> &v) { return Vec2<T>(ceilf(v.x), ceilf(v.y)); }
template<class T> inline Vec2<T> abs(const Vec2<T> &v) { return Vec2<T>(fabsf(v.x), fabsf(v.y)); }
template<class T> inline Vec2<T> fract(const Vec2<T> &v) { return v - floor(v); }
template<class T> inline Vec2<T> normalized(const Vec2<T> &v) { T l = length(v); if(!l) { return T(0); } else return v / l; }
template<class T> inline void Vec2<T>::print() { printf("x: %f, y: %f\n", x, y); }
template<class T>
class Vec3 {
public:
Vec3();;
Vec3(T x, T y, T z);
Vec3(const Vec3<T>& o);
Vec3(T f);
void set(const float xv, const float yv, const float zv);
T* ptr();
T& operator [](const unsigned int dx);
Vec3<T> operator + () const;
Vec3<T> operator - () const;
Vec3<T> operator + (const Vec3<T>& o) const;
Vec3<T> operator - (const Vec3<T>& o) const;
Vec3<T> operator * (const Vec3<T>& o) const;
Vec3<T> operator / (const Vec3<T>& o) const;
Vec3<T> operator + (float s) const;
Vec3<T> operator - (float s) const;
Vec3<T> operator * (float s) const;
Vec3<T> operator / (float s) const;
Vec3<T>& operator += (const Vec3<T>& o);
Vec3<T>& operator -= (const Vec3<T>& o);
Vec3<T>& operator *= (const Vec3<T>& o);
Vec3<T>& operator /= (const Vec3<T>& o);
Vec3<T>& operator += (float s);
Vec3<T>& operator -= (float s);
Vec3<T>& operator *= (float s);
Vec3<T>& operator /= (float s);
bool operator == (const Vec3<T>& o) const;
bool operator != (const Vec3<T>& o) const;
void print();
public:
T x, y, z;
}; // Vec3<T>
template<class T> inline Vec3<T>::Vec3() : x(), y(), z() {}
template<class T> inline Vec3<T>::Vec3(T x, T y, T z) : x(x), y(y), z(z) {}
template<class T> inline Vec3<T>::Vec3(const Vec3<T>& o) : x(o.x), y(o.y), z(o.z) { }
template<class T> inline Vec3<T>::Vec3(T f) : x(f), y(f), z(f) {}
template<class T> inline void Vec3<T>::set(const float xv, const float yv, const float zv) { x = xv; y = yv; z = zv; }
template<class T> inline T* Vec3<T>::ptr() { return &x; }
template<class T> inline T& Vec3<T>::operator [](const unsigned int dx) { return *(&x + dx); }
template<class T> inline Vec3<T> Vec3<T>::operator + () const { return Vec3<T>(+x, +y, +z); };
template<class T> inline Vec3<T> Vec3<T>::operator - () const { return Vec3<T>(-x, -y, -z); };
template<class T> inline Vec3<T> Vec3<T>::operator + (const Vec3<T>& o) const { return Vec3<T>(x + o.x, y + o.y, z + o.z); }
template<class T> inline Vec3<T> Vec3<T>::operator - (const Vec3<T>& o) const { return Vec3<T>(x - o.x, y - o.y, z - o.z); }
template<class T> inline Vec3<T> Vec3<T>::operator * (const Vec3<T>& o) const { return Vec3<T>(x * o.x, y * o.y, z * o.z); }
template<class T> inline Vec3<T> Vec3<T>::operator / (const Vec3<T>& o) const { return Vec3<T>(x / o.x, y / o.y, z / o.z); }
template<class T> inline Vec3<T> Vec3<T>::operator + (float s) const { return Vec3<T>(x + s, y + s, z + s); }
template<class T> inline Vec3<T> Vec3<T>::operator - (float s) const { return Vec3<T>(x - s, y - s, z - s); }
template<class T> inline Vec3<T> Vec3<T>::operator * (float s) const { return Vec3<T>(x * s, y * s, z * s); }
template<class T> inline Vec3<T> Vec3<T>::operator / (float s) const { return Vec3<T>(x / s, y / s, z / s); }
template<class T> inline Vec3<T> operator + (float s, const Vec3<T>& o) { return Vec3<T>(s + o.x, s + o.y, s + o.z); }
template<class T> inline Vec3<T> operator - (float s, const Vec3<T>& o) { return Vec3<T>(s - o.x, s - o.y, s - o.z); }
template<class T> inline Vec3<T> operator * (float s, const Vec3<T>& o) { return Vec3<T>(s * o.x, s * o.y, s * o.z); }
template<class T> inline Vec3<T> operator / (float s, const Vec3<T>& o) { return Vec3<T>(s / o.x, s / o.y, s / o.z); }
template<class T> inline Vec3<T>& Vec3<T>::operator += (const Vec3<T>& o) { return *this = *this + o; }
template<class T> inline Vec3<T>& Vec3<T>::operator -= (const Vec3<T>& o) { return *this = *this - o; }
template<class T> inline Vec3<T>& Vec3<T>::operator *= (const Vec3<T>& o) { return *this = *this * o; }
template<class T> inline Vec3<T>& Vec3<T>::operator /= (const Vec3<T>& o) { return *this = *this / o; }
template<class T> inline Vec3<T>& Vec3<T>::operator += (float s) { return *this = *this + s; }
template<class T> inline Vec3<T>& Vec3<T>::operator -= (float s) { return *this = *this - s; }
template<class T> inline Vec3<T>& Vec3<T>::operator *= (float s) { return *this = *this * s; }
template<class T> inline Vec3<T>& Vec3<T>::operator /= (float s) { return *this = *this / s; }
template<class T> inline bool Vec3<T>::operator == (const Vec3<T>& o) const { return x == o.x && y == o.y && z == o.z; }
template<class T> inline bool Vec3<T>::operator != (const Vec3<T>& o) const { return x != o.x || y == o.y || z == o.z; }
template<class T> inline float length(const Vec3<T>& o) { return sqrtf(o.x * o.x + o.y * o.y + o.z * o.z); }
template<class T> inline float dot(const Vec3<T> &a, const Vec3<T> &b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
template<class T> inline float heighest(const Vec3<T> &v) { return fmaxf(fmaxf(v.x, v.y), v.z); }
template<class T> inline float lowest(const Vec3<T> &v) { return fminf(fminf(v.x, v.y), v.z); }
template<class T> inline Vec3<T> heighest(const Vec3<T> &a, const Vec3<T> &b) { return Vec3<T>(fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z)); }
template<class T> inline Vec3<T> lowest(const Vec3<T> &a, const Vec3<T> &b) { return Vec3<T>(fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z)); }
template<class T> inline Vec3<T> floor(const Vec3<T> &v) { return Vec3<T>(floorf(v.x), floorf(v.y), floorf(v.z)); }
template<class T> inline Vec3<T> ceil(const Vec3<T> &v) { return Vec3<T>(ceilf(v.x), ceilf(v.y), ceilf(v.z)); }
template<class T> inline Vec3<T> abs(const Vec3<T> &v) { return Vec3<T>(fabsf(v.x), fabsf(v.y), fabsf(v.z)); }
template<class T> inline Vec3<T> fract(const Vec3<T> &v) { return v - floor(v); }
template<class T> inline Vec3<T> normalized(const Vec3<T> &v) { T l = length(v); if(!l) { return T(0); } else return v / l; }
template<class T> inline Vec3<T> cross(const Vec3<T> &a, const Vec3<T> &b) { return Vec3<T>(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
template<class T> inline Vec3<T> perpendicular(const Vec3<T>& v) { return abs(v.x) > abs(v.z) ? Vec3<T>(-v.y, v.x, 0.0) : Vec3<T>(0.0, -v.z, v.y); }
template<class T> inline void Vec3<T>::print() { printf("x: %f, y: %f, z: %f\n", x, y, z); }
// lines must be in the xy-plane; only done in 2d
template<class T>
inline bool intersect(const Vec3<T>& p0, const Vec3<T>& p1, const Vec3<T>& p2, const Vec3<T>& p3, Vec3<T>& result) {
Vec3<T> s1 = p1 - p0;
Vec3<T> s2 = p3 - p2;
float s, t;
s = (-s1.y * (p0.x - p2.x) + s1.x * (p0.y - p2.y)) / (-s2.x * s1.y + s1.x * s2.y);
t = ( s2.x * (p0.y - p2.y) - s2.y * (p0.x - p2.x)) / (-s2.x * s1.y + s1.x * s2.y);
if(s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f) {
result.x = p0.x + (t * s1.x);
result.y = p0.y + (t * s1.y);
return true;
}
return false;
}
template<class T>
class Vec4 {
public:
Vec4();
Vec4(T x, T y, T z, T w);
Vec4(const Vec4<T>& o);
Vec4(T f);
void set(const float xv, const float yv, const float zv, const float wv);
T* ptr();
T& operator [](const unsigned int dx);
Vec4<T> operator + () const;
Vec4<T> operator - () const;
Vec4<T> operator + (const Vec4<T>& o) const;
Vec4<T> operator - (const Vec4<T>& o) const;
Vec4<T> operator * (const Vec4<T>& o) const;
Vec4<T> operator / (const Vec4<T>& o) const;
Vec4<T> operator + (float s) const;
Vec4<T> operator - (float s) const;
Vec4<T> operator * (float s) const;
Vec4<T> operator / (float s) const;
Vec4<T>& operator += (const Vec4<T>& o);
Vec4<T>& operator -= (const Vec4<T>& o);
Vec4<T>& operator *= (const Vec4<T>& o);
Vec4<T>& operator /= (const Vec4<T>& o);
Vec4<T>& operator += (float s);
Vec4<T>& operator -= (float s);
Vec4<T>& operator *= (float s);
Vec4<T>& operator /= (float s);
bool operator == (const Vec4<T>& o) const;
bool operator != (const Vec4<T>& o) const;
void print();
public:
T x, y, z, w;
}; // Vec4<T>
template<class T> inline Vec4<T>::Vec4() : x(), y(), z(), w() {}
template<class T> inline Vec4<T>::Vec4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
template<class T> inline Vec4<T>::Vec4(const Vec4<T>& o) : x(o.x), y(o.y), z(o.z), w(o.w) {}
template<class T> inline Vec4<T>::Vec4(T f) : x(f), y(f), z(f), w(f) {}
template<class T> inline void Vec4<T>::set(const float xv, const float yv, const float zv, const float wv) { x = xv; y = yv; z = zv; w = wv; }
template<class T> inline T* Vec4<T>::ptr() { return &x; }
template<class T> inline T& Vec4<T>::operator [](const unsigned int dx) { return *(&x + dx); }
template<class T> inline Vec4<T> Vec4<T>::operator + () const { return Vec4<T>(+x, +y, +z, +w); };
template<class T> inline Vec4<T> Vec4<T>::operator - () const { return Vec4<T>(-x, -y, -z, -w); };
template<class T> inline Vec4<T> Vec4<T>::operator + (const Vec4<T>& o) const { return Vec4<T>(x + o.x, y + o.y, z + o.z, w + o.w); }
template<class T> inline Vec4<T> Vec4<T>::operator - (const Vec4<T>& o) const { return Vec4<T>(x - o.x, y - o.y, z - o.z, w - o.w); }
template<class T> inline Vec4<T> Vec4<T>::operator * (const Vec4<T>& o) const { return Vec4<T>(x * o.x, y * o.y, z * o.z, w * o.w); }
template<class T> inline Vec4<T> Vec4<T>::operator / (const Vec4<T>& o) const { return Vec4<T>(x / o.x, y / o.y, z / o.z, w / o.w); }
template<class T> inline Vec4<T> Vec4<T>::operator + (float s) const { return Vec4<T>(x + s, y + s, z + s, w + s); }
template<class T> inline Vec4<T> Vec4<T>::operator - (float s) const { return Vec4<T>(x - s, y - s, z - s, w - s); }
template<class T> inline Vec4<T> Vec4<T>::operator * (float s) const { return Vec4<T>(x * s, y * s, z * s, w * s); }
template<class T> inline Vec4<T> Vec4<T>::operator / (float s) const { return Vec4<T>(x / s, y / s, z / s, w / s); }
template<class T> inline Vec4<T> operator + (float s, const Vec4<T>& o) { return Vec4<T>(s + o.x, s + o.y, s + o.z, s + o.w); }
template<class T> inline Vec4<T> operator - (float s, const Vec4<T>& o) { return Vec4<T>(s - o.x, s - o.y, s - o.z, s - o.w); }
template<class T> inline Vec4<T> operator * (float s, const Vec4<T>& o) { return Vec4<T>(s * o.x, s * o.y, s * o.z, s * o.w); }
template<class T> inline Vec4<T> operator / (float s, const Vec4<T>& o) { return Vec4<T>(s / o.x, s / o.y, s / o.z, s / o.w); }
template<class T> inline Vec4<T>& Vec4<T>::operator += (const Vec4<T>& o) { return *this = *this + o; }
template<class T> inline Vec4<T>& Vec4<T>::operator -= (const Vec4<T>& o) { return *this = *this - o; }
template<class T> inline Vec4<T>& Vec4<T>::operator *= (const Vec4<T>& o) { return *this = *this * o; }
template<class T> inline Vec4<T>& Vec4<T>::operator /= (const Vec4<T>& o) { return *this = *this / o; }
template<class T> inline Vec4<T>& Vec4<T>::operator += (float s) { return *this = *this + s; }
template<class T> inline Vec4<T>& Vec4<T>::operator -= (float s) { return *this = *this - s; }
template<class T> inline Vec4<T>& Vec4<T>::operator *= (float s) { return *this = *this * s; }
template<class T> inline Vec4<T>& Vec4<T>::operator /= (float s) { return *this = *this / s; }
template<class T> inline bool Vec4<T>::operator == (const Vec4<T>& o) const { return x == o.x && y == o.y && z == o.z && w == o.w; }
template<class T> inline bool Vec4<T>::operator != (const Vec4<T>& o) const { return x != o.x || y == o.y || z == o.z || w == o.w; }
template<class T> inline float length(const Vec4<T>& o) { return sqrtf(o.x * o.x + o.y * o.y + o.z * o.z + o.w * o.w); }
template<class T> inline float dot(const Vec4<T> &a, const Vec4<T> &b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * a.w; }
template<class T> inline float heighest(const Vec4<T> &v) { return fmaxf(fmaxf(v.x, v.y), fmaxf(v.z, v.w)); }
template<class T> inline float lowest(const Vec4<T> &v) { return fminf(fminf(v.x, v.y), fminf(v.z, v.w)); }
template<class T> inline Vec4<T> heighest(const Vec4<T> &a, const Vec4<T> &b) { return Vec4<T>(fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z), fmaxf(a.w, b.w)); }
template<class T> inline Vec4<T> lowest(const Vec4<T> &a, const Vec4<T> &b) { return Vec4<T>(fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z), fminf(a.w, b.w)); }
template<class T> inline Vec4<T> floor(const Vec4<T> &v) { return Vec4<T>(floorf(v.x), floorf(v.y), floorf(v.z), floorf(v.w)); }
template<class T> inline Vec4<T> ceil(const Vec4<T> &v) { return Vec4<T>(ceilf(v.x), ceilf(v.y), ceilf(v.z), ceilf(v.w)); }
template<class T> inline Vec4<T> abs(const Vec4<T> &v) { return Vec4<T>(fabsf(v.x), fabsf(v.y), fabsf(v.z), fabsf(v.w)); }
template<class T> inline Vec4<T> fract(const Vec4<T> &v) { return v - floor(v); }
template<class T> inline Vec4<T> normalized(const Vec4<T> &v) { return v / length(v); }
template<class T> inline void Vec4<T>::print() { printf("x: %f, y: %f, z: %f, w: %f\n", x, y, z, w); }
template<class T>
class Matrix4 {
public:
Matrix4();
Matrix4<T>& rotateX(T rad);
Matrix4<T>& rotateY(T rad);
Matrix4<T>& rotateZ(T rad);
Matrix4<T>& rotate(T rad, T x, T y, T z);
Matrix4<T>& rotate(T rad, const Vec3<T>& v);
Matrix4<T>& translate(T x, T y, T z);
Matrix4<T>& translate(const Vec3<T>& v);
Matrix4<T>& position(const Vec3<T>& v);
Matrix4<T>& position(T x, T y, T z);
Matrix4<T>& scale(T x, T y, T z);
Matrix4<T>& scale(T s);
Matrix4<T>& perspective(T fovDegrees, T aspect, T n, T f);
Matrix4<T>& ortho(T l, T r, T b, T t, T n, T f);
Matrix4<T>& frustum(T l, T r, T b, T t, T n, T f);
Matrix4<T>& identity();
Matrix4<T>& lookat(Vec3<T> pos, Vec3<T> target, Vec3<T> up);
T* ptr() { return &m[0]; }
Matrix4<T> rotation(T rad, T x, T y, T z);
Matrix4<T>& operator *=(const Matrix4<T>& o);
Matrix4<T> operator * (const Matrix4<T>& o) const;
T& operator [] (const unsigned int dx) { return m[dx]; }
void print();
public:
T m[16];
}; // Matrix4<T>
template<class T>
Matrix4<T>::Matrix4() {
identity();
}
template<class T>
Matrix4<T>& Matrix4<T>::identity() {
memset(m, 0x00, sizeof(T) * 16);
m[0] = (T)1.0f;
m[5] = (T)1.0f;
m[10] =(T)1.0f;
m[15] = (T)1.0f;
return *this;
}
template<class T>
Matrix4<T>& Matrix4<T>::frustum(T l, T r, T b, T t, T n, T f) {
m[1] = T(0);
m[2] = T(0);
m[3] = T(0);
m[4] = T(0);
m[6] = T(0);
m[7] = T(0);
m[12] = T(0);
m[13] = T(0);
m[0] = T(2) * n / (r-l);
m[5] = T(2) * n / (t-b);
m[8] = (r+l) / (r-l);
m[9] = (t+b) / (t-b);
m[10] = - (f+n) / (f-n);
m[11] = - T(1);
m[14] = - T(2) * f * n / (f-n);
m[15] = T(0);
return *this;
}
template<class T>
Matrix4<T>& Matrix4<T>::ortho(T l, T r, T b, T t, T n, T f) {
m[1] = T(0);
m[2] = T(0);
m[3] = T(0);
m[4] = T(0);
m[6] = T(0);
m[7] = T(0);
m[8] = T(0);
m[9] = T(0);
m[11] = T(0);
m[15] = T(1);
float rml = r - l;
float fmn = f - n;
float tmb = t - b;
m[0] = T(2) / rml;
m[5] = T(2) / tmb;
m[10] = -T(2) / fmn;
m[12] = -(r+l)/rml;
m[13] = -(t+b)/tmb;
m[14] = -(f+n)/fmn;
return *this;
}
template<class T>
Matrix4<T>& Matrix4<T>::perspective(T fovDegrees, T aspect, T n, T f) {
T tan_hfov = tan( (fovDegrees * DEG_TO_RAD) * T(0.5) );
m[1] = T(0);
m[2] = T(0);
m[3] = T(0);
m[4] = T(0);
m[6] = T(0);
m[7] = T(0);
m[12] = T(0);
m[13] = T(0);
m[15] = T(0);
m[0] = T(1) / (aspect * tan_hfov);
m[5] = T(1) / (tan_hfov);
m[10] = - (f + n) / (f - n);
m[11] = - T(1);
m[14] = - (T(2) * f * n) / (f - n);
return *this;
}
template<class T>
void Matrix4<T>::print() {
printf("%f, %f, %f, %f\n", m[0], m[4], m[8], m[12]);
printf("%f, %f, %f, %f\n", m[1], m[5], m[9], m[13]);
printf("%f, %f, %f, %f\n", m[2], m[6], m[10], m[14]);
printf("%f, %f, %f, %f\n", m[3], m[7], m[11], m[15]);
}
template<class T>
Matrix4<T>& Matrix4<T>::rotate(T rad, T x, T y, T z) {
Matrix4<T> rot = rotation(rad, x, y, z);
*this *= rot;
return *this;
}
template<class T>
Matrix4<T>& Matrix4<T>::rotateX(T rad) {
return rotate(rad, T(1), T(0), T(0));
}
template<class T>
Matrix4<T>& Matrix4<T>::rotateY(T rad) {
return rotate(rad, T(0), T(1), T(0));
}
template<class T>
Matrix4<T>& Matrix4<T>::rotateZ(T rad) {
return rotate(rad, T(0), T(0), T(1));
}
template<class T>
Matrix4<T>& Matrix4<T>::rotate(T rad, const Vec3<T>& v) {
return rotate(rad, v.x, v.y, v.z);
}
template<class T>
Matrix4<T> Matrix4<T>::rotation(T rad, T x, T y, T z) {
Matrix4<T> mat;
float c = cos(rad);
float s = sin(rad);
float t = 1.0f - c;
Vec3<T> ax(x,y,z);
ax = normalized(ax);
float tx = t * ax.x;
float ty = t * ax.y;
float tz = t * ax.z;
float sx = s * ax.x;
float sy = s * ax.y;
float sz = s * ax.z;
float txy = tx * ax.y;
float tyz = tx * ax.z;
float txz = tx * ax.z;
mat.m[0] = tx * ax.x + c;
mat.m[4] = txy - sz;
mat.m[8] = txz + sy;
mat.m[12] = 0.0f;
mat.m[1] = txy + sz;
mat.m[5] = ty * ax.y + c;
mat.m[9] = tyz - sx;
mat.m[13] = 0.0f;
mat.m[2] = txz - sy;
mat.m[6] = tyz + sx;
mat.m[10] = tz * ax.z + c;
mat.m[14] = 0.0f;
mat.m[3] = 0.0f;
mat.m[7] = 0.0f;
mat.m[11] = 0.0f;
mat.m[15] = 1.0f;
return mat;
}