This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
fisheye.c
2452 lines (2065 loc) · 74.7 KB
/
fisheye.c
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
/*
FISHEYE.C
=========
This is a fisheye addon based on Fisheye Quake. It renders up to 6 camera views
per frame, and melds them together to allow a Field of View (FoV) greater than 180 degrees:
---------
| |
| UP | -----------------------------
| | |\-- UP --/|
--------- | \--- ---/ |
--------- --------- --------- --------- | \-----------/ |
| | | | | | | | | | | |
| LEFT | | FRONT | | RIGHT | | BACK | ---> | LEFT | FRONT | RIGHT |
| | | | | | | | | | | |
--------- --------- --------- --------- | /-----------\ |
^ --------- | /--- ---\ |
| | | |/-- DOWN --\|
90º | DOWN | -----------------------------
| | | <---------- +180º ---------->
v ---------
<--90º-->
(a GLOBE controls the separate (a LENS controls how the
camera views to render) views are melded together)
To enable this fisheye rendering, enter the command:
```
] fisheye 1
```
To resume the standard view, enter the command:
```
] fisheye 0
```
GLOBES
------
The multiple camera views are controlled by a "globe" script. It contains a
"plates" array, with each element containing a single camera's forward vector,
up vector, and fov. Together, the plates should form a complete globe around
the player.
Coordinate System:
+Y = up
^
|
|
| / +Z = forward
| /
| /
| /
0------------------> +X = right
NOTE: Plate coordinates are relative to the camera's frame. They are NOT absolute coordinates.
For example, this is the default globe (globes/cube.lua):
```
plates = {
{ { 0, 0, 1 }, { 0, 1, 0 }, 90 }, -- front
{ { 1, 0, 0 }, { 0, 1, 0 }, 90 }, -- right
{ { -1, 0, 0 }, { 0, 1, 0 }, 90 }, -- left
{ { 0, 0, -1 }, { 0, 1, 0 }, 90 }, -- back
{ { 0, 1, 0 }, { 0, 0, -1 }, 90 }, -- top
{ { 0, -1, 0 }, { 0, 0, 1 }, 90 } -- bottom
}
```
---------
| |
| UP |
| |
---------
--------- --------- --------- ---------
| | | | | | | |
| LEFT | | FRONT | | RIGHT | | BACK |
| | | | | | | |
--------- --------- --------- ---------
^ ---------
| | |
90º | DOWN |
| | |
v ---------
<--90º-->
To use this globe, enter the command:
```
] f_globe cube
```
There are other included globes:
- trism: a triangular prism with 5 views
- tetra: a tetrahedron with 4 views
- fast: 2 overlaid views in the same direction (90 and 160 degrees)
LENSES
------
The camera views are melded together by a "lens" script.
This is done with either a "forward" or "inverse" mapping.
---------
| |
| | -----------------------------
| | |\-- --/|
--------- FORWARD | \--- ---/ |
--------- --------- --------- --------- ------> | \-----------/ |
| | | | | | | | | | | |
| | | | | | | | | | | |
| | | | | | | | | | | |
--------- --------- --------- --------- <------ | /-----------\ |
--------- INVERSE | /--- ---\ |
| | |/-- --\|
| | -----------------------------
| |
---------
A "FORWARD" mapping does GLOBE -> LENS.
An "INVERSE" mapping does LENS -> GLOBE (this is faster!).
GLOBE COORDINATES:
(you can use any of the following coord systems to get a globe pixel):
- direction vector
+Y = up
^
|
|
| / +Z = forward
| /
| /
| /
0------------------> +X = right
- latitude/longitude (spherical degrees)
+latitude (degrees up)
^
|
|
|
|
|
|
0------------------> +longitude (degrees right)
- plate index & uv (e.g. plate=1, u=0.5, v=0.5 to get the center pixel of plate 1)
0----------> +u (max 1)
| ---------
| | |
| | |
| | |
| ---------
V
+v (max 1)
LENS COORDINATES:
+Y
^
|
|
|
|
|
|
0----------------> +X
ZOOMING
-------
To control how much of the resulting lens image we can see on screen,
we scale it such that the screen aligns with certain points on the lens' axes.
For example, suppose we have a LENS image below.
The (X) corresponds to the point at longitude=(FOV/2)º latitude=0º.
We flush the screen edge to this point to achieve the desired FOV zoom.
-------------------------------------------------------------------------
| LENS IMAGE ^ |
| | |
| | |
| ------------------|------------------- |
| | SCREEN | | |
| | (90º FOV) | | |
| | | | |
| | 0------------------X--------------> |
| | |\ |
| | | \ |
| | | \ point at |
| -------------------------------------- lon = 45º |
| lat = 0º |
| |
| |
-------------------------------------------------------------------------
The process is similar when we want a vertical FOV:
-------------------------------------------------------------------------
| LENS IMAGE ^ |
| | |
| | |
| ------------------X------------------- |
| | SCREEN |\ point at | |
| | (90º vertical) | \ lon = 0º | |
| | | lat = 45º | |
| | 0---------------------------------> |
| | | |
| | | |
| | | |
| -------------------------------------- |
| |
| |
| |
-------------------------------------------------------------------------
We can also zoom the lens image such that its BOUNDARIES are flush with the screen.
LUA DETAILS
-----------
Variables/Functions you provide:
Globe:
- plates (array of [forward, up, fov] objects)
- globe_plate (function (x,y,z) -> index)
Lens:
MAPPING FUNCTIONS
- lens_forward (function (x,y,z) -> (x,y))
- lens_inverse (function (x,y) -> (x,y,z))
BOUNDARIES
- lens_width (double)
- lens_height (double)
- max_fov (int)
- max_vfov (int)
(optional command to be called when lens is loaded)
- onload (string)
Variables/Functions provided to you:
- numplates (int)
- latlon_to_ray (function (lat,lon) -> (x,y,z))
- ray_to_latlon (function (x,y,z) -> (lat,lon))
- plate_to_ray (function (i,u,v) -> (x,y,z))
*/
#include "bspfile.h"
#include "client.h"
#include "cmd.h"
#include "console.h"
#include "cvar.h"
#include "draw.h"
#include "fisheye.h"
#include "host.h"
#include "mathlib.h"
#include "quakedef.h"
#include "r_local.h"
#include "screen.h"
#include "sys.h"
#include "view.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <time.h>
// --------------------------------------------------------------------------------
// | |
// | VARIABLES |
// | |
// --------------------------------------------------------------------------------
// This is a globally accessible variable that determines if these fisheye features
// should be on. It is used by other files for modifying behaviors that fisheye
// depends on (e.g. square refdef, disabling water warp, hooking renderer).
qboolean fisheye_enabled;
qboolean shortcutkeys_enabled;
// This is a globally accessible variable that is used to set the fov of each
// camera view that we render.
double fisheye_plate_fov;
// Lens computation is slow, so we don't want to block the game while its busy.
// Instead of dealing with threads, we are just limiting the time that the
// lens builder can work each frame. It keeps track of its work between frames
// so it can resume without problems. This allows the user to watch the lens
// pixels become visible as they are calculated.
static struct _lens_builder
{
qboolean working;
int start_time;
float seconds_per_frame;
struct _inverse_state
{
int ly;
} inverse_state;
struct _forward_state
{
int *top;
int *bot;
int plate_index;
int py;
} forward_state;
} lens_builder;
// the Lua state pointer
static lua_State *lua;
// lua reference indexes (for reference lua functions)
static struct _lua_refs {
int lens_forward;
int lens_inverse;
int globe_plate;
} lua_refs;
static struct _globe {
// name of the current globe
char name[50];
// indicates if the current globe is valid
qboolean valid;
// indiciates if the lens has changed and needs updating
qboolean changed;
// the environment map
// a large array of pixels that hold all rendered views
byte *pixels;
// retrieves a pointer to a pixel in the platemap
#define GLOBEPIXEL(plate,x,y) (globe.pixels + (plate)*(globe.platesize)*(globe.platesize) + (x) + (y)*(globe.platesize))
// globe plates
#define MAX_PLATES 6
struct {
vec3_t forward;
vec3_t right;
vec3_t up;
vec_t fov;
vec_t dist;
byte palette[256];
int display;
} plates[MAX_PLATES];
// number of plates used by the current globe
int numplates;
// size of each rendered square plate in the vid buffer
int platesize;
// set when we want to save each globe plate
// (make sure they are visible (i.e. current lens is using all plates))
struct {
qboolean should;
int with_margins;
char name[32];
} save;
} globe;
static struct _lens {
// boolean signaling if the lens is properly loaded
qboolean valid;
// boolean signaling if the lens has changed and needs updating
qboolean changed;
// name of the current lens
char name[50];
// the type of map projection (inverse/forward)
enum { MAP_NONE, MAP_INVERSE, MAP_FORWARD } map_type;
// size of the lens image in its arbitrary units
double width, height;
// controls the zoom of the lens image
// (scale = units per pixel)
double scale;
// pixel size of the lens view (it is equal to the screen size below):
// ------------------
// | |
// | ---------- ^ |
// | | | | |
// | | screen | h |
// | | | | |
// | ---------- v |
// | <---w----> |
// |----------------|
// | status bar |
// |----------------|
int width_px, height_px;
// array of pointers (*) to plate pixels
// (the view constructed by the lens)
//
// ************************** ^
// ************************** |
// ************************** |
// ************************** height_px
// ************************** |
// ************************** |
// ************************** v
//
// <------- width_px ------->
//
byte **pixels;
// retrieves a pointer to a lens pixel
#define LENSPIXEL(x,y) (lens.pixels + (x) + (y)*lens.width_px)
// a color tint index (i) for each pixel (255 = no filter)
// (new color = globe.plates[i].palette[old color])
// (used for displaying transparent colored overlays over certain pixels)
//
// iiiiiiiiiiiiiiiiiiiiiiiiii ^
// iiiiiiiiiiiiiiiiiiiiiiiiii |
// iiiiiiiiiiiiiiiiiiiiiiiiii |
// iiiiiiiiiiiiiiiiiiiiiiiiii height_px
// iiiiiiiiiiiiiiiiiiiiiiiiii |
// iiiiiiiiiiiiiiiiiiiiiiiiii |
// iiiiiiiiiiiiiiiiiiiiiiiiii v
//
// <------- width_px ------->
//
byte *pixel_tints;
// retrieves a pointer to a lens pixel tint
#define LENSPIXELTINT(x,y) (lens.pixel_tints + (x) + (y)*lens.width_px)
} lens;
static struct _zoom {
qboolean changed;
enum { ZOOM_NONE, ZOOM_FOV, ZOOM_VFOV, ZOOM_COVER, ZOOM_CONTAIN } type;
// desired FOV in degrees
int fov;
// maximum FOV width and height of the current lens in degrees
int max_vfov, max_fov;
} zoom;
static struct _rubix {
// boolean signaling if rubix should be drawn
qboolean enabled;
int numcells;
double cell_size;
double pad_size;
// We color a plate like the side of a rubix cube so we
// can see how the lens distorts the plates. (indicatrix)
//
// EXAMPLE:
//
// numcells = 3
// cell_size = 2
// pad_size = 1
//
// A globe plate is split into a grid of "units"
// The squares that are colored below are called "cells".
// You can see below that there are 3x3 colored cells,
// since numcells=3.
//
// You can see below that each cell is 2x2 units large,
// since cell_size=2.
//
// Finally, you can see the cells have 1 unit of padding,
// since pad_size=1.
//
// ---------------------------------------------------
// | | | | | | | | | | |
// | | | | | | | | | | |
// |----|----|----|----|----|----|----|----|----|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|XXXXXXXXX|----|XXXXXXXXX|----|XXXXXXXXX|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|----|----|----|----|----|----|----|----|----|
// | | | | | | | | | | |
// | | | | | | | | | | |
// |----|----|----|----|----|----|----|----|----|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|XXXXXXXXX|----|XXXXXXXXX|----|XXXXXXXXX|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|----|----|----|----|----|----|----|----|----|
// | | | | | | | | | | |
// | | | | | | | | | | |
// |----|----|----|----|----|----|----|----|----|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|XXXXXXXXX|----|XXXXXXXXX|----|XXXXXXXXX|----|
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// | |XXXXXXXXX| |XXXXXXXXX| |XXXXXXXXX| |
// |----|----|----|----|----|----|----|----|----|----|
// | | | | | | | | | | |
// | | | | | | | | | | |
// ---------------------------------------------------
} rubix;
// --------------------------------------------------------------------------------
// | |
// | FUNCTION DECLARATIONS |
// | |
// --------------------------------------------------------------------------------
// public main functions
void F_Init(void);
void F_Shutdown(void);
void F_WriteConfig(FILE* f);
void F_RenderView(void);
// console commands
static void cmd_fisheye(void);
static void cmd_help(void);
static void cmd_lens(void);
static void cmd_globe(void);
static void cmd_fov(void);
static void cmd_vfov(void);
static void cmd_dumppal(void);
static void cmd_rubix(void);
static void cmd_rubixgrid(void);
static void cmd_cover(void);
static void cmd_contain(void);
static void cmd_saveglobe(void);
static void cmd_shortcutkeys(void);
// console autocomplete helpers
static struct stree_root * cmdarg_lens(const char *arg);
static struct stree_root * cmdarg_globe(const char *arg);
// lens builder timing functions
static void start_lens_builder_clock(void);
static qboolean is_lens_builder_time_up(void);
// palette functions
static int find_closest_pal_index(int r, int g, int b);
static void create_palmap(void);
// lua initializer
static void init_lua(void);
// c->lua (c functions for use in lua)
static int CtoLUA_latlon_to_ray(lua_State *L);
static int CtoLUA_ray_to_latlon(lua_State *L);
static int CtoLUA_plate_to_ray(lua_State *L);
// lua->c (lua functions for use in c)
static int LUAtoC_lens_inverse(double x, double y, vec3_t ray);
static int LUAtoC_lens_forward(vec3_t ray, double *x, double *y);
static int LUAtoC_globe_plate(vec3_t ray, int *plate);
// functions to manage the data and functions in the Lua interpreter state
static qboolean LUA_load_lens(void);
static qboolean LUA_load_globe(void);
static void LUA_clear_lens(void);
static void LUA_clear_globe(void);
// lua helpers
static qboolean lua_func_exists(const char* name);
// zoom functions
static qboolean calc_zoom(void);
static void clear_zoom(void);
static void print_zoom(void);
// lens pixel setters
static void set_lensmap_grid(int lx, int ly, int px, int py, int plate_index);
static void set_lensmap_from_plate(int lx, int ly, int px, int py, int plate_index);
static void set_lensmap_from_plate_uv(int lx, int ly, double u, double v, int plate_index);
static void set_lensmap_from_ray(int lx, int ly, double sx, double sy, double sz);
// globe plate getters
static int ray_to_plate_index(vec3_t ray);
static qboolean ray_to_plate_uv(int plate_index, vec3_t ray, double *u, double *v);
// pure coordinate convertors
static void latlon_to_ray(double lat, double lon, vec3_t ray);
static void ray_to_latlon(vec3_t ray, double *lat, double *lon);
static void plate_uv_to_ray(int plate_index, double u, double v, vec3_t ray);
// forward map getter/setter helpers
static int uv_to_screen(int plate_index, double u, double v, int *lx, int *ly);
static void draw_quad(int *tl, int *tr, int *bl, int *br, int plate_index, int px, int py);
// lens builder resumers
static void resume_lensmap(void);
static qboolean resume_lensmap_inverse(void);
static qboolean resume_lensmap_forward(void);
// lens creators
static void create_lensmap_inverse(void);
static void create_lensmap_forward(void);
static void create_lensmap(void);
// renderers
static void render_lensmap(void);
static void render_plate(int plate_index, vec3_t forward, vec3_t right, vec3_t up);
// globe saver functions
static void WritePCXplate(char *filename, int plate_index, int with_margins);
static void save_globe(void);
// retrieves a pointer to a pixel in the video buffer
#define VBUFFER(x,y) (vid.buffer + (x) + (y)*vid.rowbytes)
// --------------------------------------------------------------------------------
// | |
// | PUBLIC MAIN FUNCTIONS |
// | |
// --------------------------------------------------------------------------------
void F_Init(void)
{
lens_builder.working = false;
lens_builder.seconds_per_frame = 1.0f / 60;
rubix.enabled = false;
init_lua();
Cmd_AddCommand("fisheye", cmd_fisheye);
Cmd_AddCommand("f_help", cmd_help);
Cmd_AddCommand("f_dumppal", cmd_dumppal);
Cmd_AddCommand("f_rubix", cmd_rubix);
Cmd_AddCommand("f_rubixgrid", cmd_rubixgrid);
Cmd_AddCommand("f_cover", cmd_cover);
Cmd_AddCommand("f_contain", cmd_contain);
Cmd_AddCommand("f_fov", cmd_fov);
Cmd_AddCommand("f_vfov", cmd_vfov);
Cmd_AddCommand("f_lens", cmd_lens);
Cmd_SetCompletion("f_lens", cmdarg_lens);
Cmd_AddCommand("f_globe", cmd_globe);
Cmd_SetCompletion("f_globe", cmdarg_globe);
Cmd_AddCommand("f_saveglobe", cmd_saveglobe);
Cmd_AddCommand("f_shortcutkeys", cmd_shortcutkeys);
// defaults
Cmd_ExecuteString("fisheye 1", src_command);
Cmd_ExecuteString("f_globe cube", src_command);
Cmd_ExecuteString("f_lens panini", src_command);
Cmd_ExecuteString("f_fov 180", src_command);
Cmd_ExecuteString("f_rubixgrid 10 4 1", src_command);
// create palette maps
create_palmap();
}
void F_Shutdown(void)
{
lua_close(lua);
}
void F_WriteConfig(FILE* f)
{
fprintf(f,"fisheye %d\n", fisheye_enabled);
fprintf(f,"f_lens \"%s\"\n", lens.name);
fprintf(f,"f_globe \"%s\"\n", globe.name);
fprintf(f,"f_rubixgrid %d %f %f\n", rubix.numcells, rubix.cell_size, rubix.pad_size);
switch (zoom.type) {
case ZOOM_FOV: fprintf(f,"f_fov %d\n", zoom.fov); break;
case ZOOM_VFOV: fprintf(f,"f_vfov %d\n", zoom.fov); break;
case ZOOM_COVER: fprintf(f,"f_cover\n"); break;
case ZOOM_CONTAIN: fprintf(f,"f_contain\n"); break;
default: break;
}
}
void F_RenderView(void)
{
static int pwidth = -1;
static int pheight = -1;
// update screen size
lens.width_px = scr_vrect.width;
lens.height_px = scr_vrect.height;
#define MIN(a,b) ((a) < (b) ? (a) : (b))
int platesize = globe.platesize = MIN(lens.height_px, lens.width_px);
int area = lens.width_px * lens.height_px;
int sizechange = (pwidth!=lens.width_px) || (pheight!=lens.height_px);
// allocate new buffers if size changes
if(sizechange)
{
if(globe.pixels) free(globe.pixels);
if(lens.pixels) free(lens.pixels);
if(lens.pixel_tints) free(lens.pixel_tints);
globe.pixels = (byte*)malloc(platesize*platesize*MAX_PLATES*sizeof(byte));
lens.pixels = (byte**)malloc(area*sizeof(byte*));
lens.pixel_tints = (byte*)malloc(area*sizeof(byte));
// the rude way
if(!globe.pixels || !lens.pixels || !lens.pixel_tints) {
Con_Printf("Quake-Lenses: could not allocate enough memory\n");
exit(1);
}
}
// recalculate lens
if (sizechange || zoom.changed || lens.changed || globe.changed) {
memset(lens.pixels, 0, area*sizeof(byte*));
memset(lens.pixel_tints, 255, area*sizeof(byte));
// load lens again
// (NOTE: this will be the second time this lens will be loaded in this frame if it has just changed)
// (I'm just trying to force re-evaluation of lens variables that are dependent on globe variables (e.g. "lens_width = numplates" in debug.lua))
lens.valid = LUA_load_lens();
if (!lens.valid) {
strcpy(lens.name,"");
Con_Printf("not a valid lens\n");
}
create_lensmap();
}
else if (lens_builder.working) {
resume_lensmap();
}
// get the orientations required to render the plates
vec3_t forward, right, up;
AngleVectors(r_refdef.viewangles, forward, right, up);
// do not do this every frame?
extern int sb_lines;
extern vrect_t scr_vrect;
vrect_t vrect;
vrect.x = 0;
vrect.y = 0;
vrect.width = vid.width;
vrect.height = vid.height;
R_SetVrect(&vrect, &scr_vrect, sb_lines);
// render plates
int i;
for (i=0; i<globe.numplates; ++i)
{
if (globe.plates[i].display) {
// set view to change plate FOV
fisheye_plate_fov = globe.plates[i].fov;
R_ViewChanged(&vrect, sb_lines, vid.aspect);
// compute absolute view vectors
// right = x
// top = y
// forward = z
vec3_t r = { 0,0,0};
VectorMA(r, globe.plates[i].right[0], right, r);
VectorMA(r, globe.plates[i].right[1], up, r);
VectorMA(r, globe.plates[i].right[2], forward, r);
vec3_t u = { 0,0,0};
VectorMA(u, globe.plates[i].up[0], right, u);
VectorMA(u, globe.plates[i].up[1], up, u);
VectorMA(u, globe.plates[i].up[2], forward, u);
vec3_t f = { 0,0,0};
VectorMA(f, globe.plates[i].forward[0], right, f);
VectorMA(f, globe.plates[i].forward[1], up, f);
VectorMA(f, globe.plates[i].forward[2], forward, f);
render_plate(i, f, r, u);
}
}
// save plates upon request from the "saveglobe" command
if (globe.save.should) {
save_globe();
}
// render our view
Draw_TileClear(0, 0, vid.width, vid.height);
render_lensmap();
// store current values for change detection
pwidth = lens.width_px;
pheight = lens.height_px;
// reset change flags
lens.changed = globe.changed = zoom.changed = false;
}
// --------------------------------------------------------------------------------
// | |
// | LENS BUILD TIMING FUNCTIONS |
// | |
// --------------------------------------------------------------------------------
static void start_lens_builder_clock(void) {
lens_builder.start_time = clock();
}
static qboolean is_lens_builder_time_up(void) {
clock_t time = clock() - lens_builder.start_time;
float s = ((float)time) / CLOCKS_PER_SEC;
return (s >= lens_builder.seconds_per_frame);
}
// --------------------------------------------------------------------------------
// | |
// | PALLETE FUNCTIONS |
// | |
// --------------------------------------------------------------------------------
// find closest pallete index for color
static int find_closest_pal_index(int r, int g, int b)
{
int i;
int mindist = 256*256*256;
int minindex = 0;
byte* pal = host_basepal;
for (i=0; i<256; ++i)
{
int dr = (int)pal[0]-r;
int dg = (int)pal[1]-g;
int db = (int)pal[2]-b;
int dist = dr*dr+dg*dg+db*db;
if (dist < mindist)
{
mindist = dist;
minindex = i;
}
pal += 3;
}
return minindex;
}
static void create_palmap(void)
{
int i,j;
int percent = 256/6;
int tint[3];
for (j=0; j<MAX_PLATES; ++j)
{
tint[0] = tint[1] = tint[2] = 0;
switch (j)
{
case 0:
tint[0] = tint[1] = tint[2] = 255;
break;
case 1:
tint[2] = 255;
break;
case 2:
tint[0] = 255;
break;
case 3:
tint[0] = tint[1] = 255;
break;
case 4:
tint[0] = tint[2] = 255;
break;
case 5:
tint[1] = tint[2] = 255;
break;
}
byte* pal = host_basepal;
for (i=0; i<256; ++i)
{
int r = pal[0];
int g = pal[1];
int b = pal[2];
r += percent * (tint[0] - r) >> 8;
g += percent * (tint[1] - g) >> 8;
b += percent * (tint[2] - b) >> 8;
if (r < 0) r=0; if (r > 255) r=255;
if (g < 0) g=0; if (g > 255) g=255;
if (b < 0) b=0; if (b > 255) b=255;
globe.plates[j].palette[i] = find_closest_pal_index(r,g,b);
pal += 3;
}
}
}
// --------------------------------------------------------------------------------
// | |
// | CONSOLE COMMANDS |
// | |
// --------------------------------------------------------------------------------
static void cmd_dumppal(void)
{
int i;
byte *pal = host_basepal;
FILE *pFile = fopen("palette","w");
if (NULL == pFile) {
Con_Printf("could not open \"palette\" for writing\n");
return;
}
for (i=0; i<256; ++i) {
fprintf(pFile, "%d, %d, %d,\n",
pal[0],pal[1],pal[2]);
pal+=3;
}
fclose(pFile);
}
static void cmd_rubix(void)
{
rubix.enabled = !rubix.enabled;
Con_Printf("Rubix is %s\n", rubix.enabled ? "ON" : "OFF");
}
static void cmd_rubixgrid(void)
{
if (Cmd_Argc() == 4) {
rubix.numcells = Q_atof(Cmd_Argv(1));
rubix.cell_size = Q_atof(Cmd_Argv(2));
rubix.pad_size = Q_atof(Cmd_Argv(3));
lens.changed = true; // need to recompute lens to update grid
}
else {
Con_Printf("RubixGrid <numcells> <cellsize> <padsize>\n");
Con_Printf(" numcells (default 10) = %d\n", rubix.numcells);
Con_Printf(" cellsize (default 4) = %f\n", rubix.cell_size);
Con_Printf(" padsize (default 1) = %f\n", rubix.pad_size);
}
}
static void cmd_cover(void)
{
clear_zoom();
zoom.type = ZOOM_COVER;
}
static void cmd_contain(void)
{
clear_zoom();
zoom.type = ZOOM_CONTAIN;
}
static void cmd_fisheye(void)
{
if (Cmd_Argc() < 2) {
Con_Printf("Currently: ");
Con_Printf("fisheye %d\n", fisheye_enabled);
Con_Printf("\nTry F_HELP for more options and commands.\n");
return;
}
fisheye_enabled = Q_atoi(Cmd_Argv(1)); // will return 0 if not valid
vid.recalc_refdef = true;
}
static void cmd_shortcutkeys(void)
{
shortcutkeys_enabled = !shortcutkeys_enabled;
if (shortcutkeys_enabled) {
Con_Printf("Enabled Fisheye shortcut keys: 1-9 = Lenses, Y,U,I,O,P = Globes\n");
Cmd_ExecuteString("bind 1 \"f_lens panini\"", src_command);
Cmd_ExecuteString("bind 2 \"f_lens stereographic\"", src_command);
Cmd_ExecuteString("bind 3 \"f_lens hammer\"", src_command);
Cmd_ExecuteString("bind 4 \"f_lens winkeltripel\"", src_command);
Cmd_ExecuteString("bind 5 \"f_lens fisheye1\"", src_command);
Cmd_ExecuteString("bind 6 \"f_lens mercator\"", src_command);
Cmd_ExecuteString("bind 7 \"f_lens quincuncial\"", src_command);
Cmd_ExecuteString("bind 8 \"f_lens cube\"", src_command);
Cmd_ExecuteString("bind 9 \"f_lens debug\"", src_command);
Cmd_ExecuteString("bind y \"f_globe cube\"", src_command);
Cmd_ExecuteString("bind u \"f_globe cube_edge\"", src_command);
Cmd_ExecuteString("bind i \"f_globe trism\"", src_command);
Cmd_ExecuteString("bind o \"f_globe tetra\"", src_command);
Cmd_ExecuteString("bind p \"f_globe fast\"", src_command);
}
else {
Con_Printf("Disabled Fisheye shortcut keys\n");