forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphics.cpp
More file actions
2458 lines (1928 loc) · 69.9 KB
/
graphics.cpp
File metadata and controls
2458 lines (1928 loc) · 69.9 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
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
//
//
#include "graphics.h"
#include "scripting/api/objs/camera.h"
#include "scripting/api/objs/color.h"
#include "scripting/api/objs/enums.h"
#include "scripting/api/objs/font.h"
#include "scripting/api/objs/model.h"
#include "scripting/api/objs/movie_player.h"
#include "scripting/api/objs/object.h"
#include "scripting/api/objs/particle.h"
#include "scripting/api/objs/streaminganim.h"
#include "scripting/api/objs/subsystem.h"
#include "scripting/api/objs/texture.h"
#include "scripting/api/objs/vecmath.h"
#include "scripting/global_hooks.h"
#include <asteroid/asteroid.h>
#include <camera/camera.h>
#include <debris/debris.h>
#include <freespace.h>
#include <globalincs/systemvars.h>
#include <graphics/2d.h>
#include <graphics/openxr.h>
#include <graphics/material.h>
#include <graphics/matrix.h>
#include <hud/hudbrackets.h>
#include <hud/hudtarget.h>
#include <jumpnode/jumpnode.h>
#include <model/modelrender.h>
#include <parse/parselo.h>
#include <particle/ParticleEffect.h>
#include <particle/volumes/ConeVolume.h>
#include <render/3d.h>
#include <render/3dinternal.h>
#include <render/batching.h>
#include <scripting/scripting.h>
#include <ship/ship.h>
#include <weapon/weapon.h>
#include <cmath>
namespace {
static const int NextDrawStringPosInitial[] = {0, 0};
static int NextDrawStringPos[] = {NextDrawStringPosInitial[0], NextDrawStringPosInitial[1]};
static fix PreviousFrametimeOverall = 0;
static bool WarnedBadThicknessLine = false;
}
namespace scripting {
namespace api {
// Sets the graphics context to the Lua context on creation and resets it back to the main context on destruction
// Ensures that no matter how the parent function is exited the context is reset properly
struct LuaScreenContext {
LuaScreenContext() noexcept
{
gr_lua_screen.active = true;
}
~LuaScreenContext() noexcept
{
gr_lua_screen.active = false;
}
LuaScreenContext(const LuaScreenContext&) = delete;
LuaScreenContext& operator=(const LuaScreenContext&) = delete;
};
model_draw_list *Current_scene = nullptr;
//**********LIBRARY: Graphics
ADE_LIB(l_Graphics, "Graphics", "gr", "Graphics Library");
static float lua_Opacity = 1.0f;
static int lua_Opacity_type = GR_ALPHABLEND_FILTER;
static int lua_ResizeMode = GR_RESIZE_NONE;
int get_resize_mode() { return lua_ResizeMode; }
//****SUBLIBRARY: Graphics/Cameras
ADE_LIB_DERIV(l_Graphics_Cameras, "Cameras", NULL, "Cameras", l_Graphics);
ADE_INDEXER(l_Graphics_Cameras, "number/string IndexOrName", "Gets camera", "camera", "Ship handle, or invalid ship handle if index was invalid")
{
const char* s = nullptr;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Camera.Set(camid()));
camid cid = cam_lookup(s);
if(!cid.isValid())
{
int cn = atoi(s);
if(cn > 0)
{
//Lua-->FS2
cn--;
cid = cam_get_camera(cn);
}
}
return ade_set_args(L, "o", l_Camera.Set(cid));
}
ADE_FUNC(__len, l_Graphics_Cameras, NULL, "Gets number of cameras", "number", "Number of cameras")
{
return ade_set_args(L, "i", (int)cam_get_num());
}
//****SUBLIBRARY: Graphics/Fonts
ADE_LIB_DERIV(l_Graphics_Fonts, "Fonts", nullptr, "Font library", l_Graphics);
ADE_INDEXER(l_Graphics_Fonts, "number/string IndexOrFilename", "Array of loaded fonts", "font", "Font handle, or invalid font handle if index is invalid")
{
if (lua_isnumber(L, 2))
{
int index = -1;
if (!ade_get_args(L, "*i", &index))
return ade_set_error(L, "o", l_Font.Set(font_h()));
auto realIdx = index - 1;
if (realIdx < 0 || realIdx >= font::FontManager::numberOfFonts())
{
return ade_set_error(L, "o", l_Font.Set(font_h()));
}
return ade_set_args(L, "o", l_Font.Set(font_h(index - 1)));
}
else
{
const char* s = nullptr;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Font.Set(font_h()));
return ade_set_args(L, "o", l_Font.Set(font_h(font::FontManager::getFontIndex(s))));
}
}
ADE_FUNC(__len, l_Graphics_Fonts, nullptr, "Number of loaded fonts", "number", "Number of loaded fonts")
{
return ade_set_args(L, "i", font::FontManager::numberOfFonts());
}
ADE_VIRTVAR(CurrentFont, l_Graphics, "font", "Current font", "font", nullptr)
{
font_h *newFh = nullptr;
if(!ade_get_args(L, "*|o", l_Font.GetPtr(&newFh)))
return ade_set_error(L, "o", l_Font.Set(font_h()));
LuaScreenContext context;
if(ADE_SETTING_VAR && newFh->isValid()) {
font::FontManager::setCurrentFontIndex(newFh->GetIndex());
}
int font = font::FontManager::getCurrentFontIndex();
return ade_set_args(L, "o", l_Font.Set(font_h(font)));
}
//****SUBLIBRARY: Graphics/PostEffects
ADE_LIB_DERIV(l_Graphics_Posteffects, "PostEffects", nullptr, "Post-processing effects", l_Graphics);
ADE_INDEXER(l_Graphics_Posteffects, "number index", "Gets the name of the specified post-processing index", "string", "post-processing name or empty string on error")
{
int index = -1;
if(!ade_get_args(L, "*i", &index))
return ade_set_error(L, "s", "");
index--; // Lua -> C/C++
if (index < 0)
return ade_set_error(L, "s", "");
SCP_vector<SCP_string> names;
gr_get_post_process_effect_names(names);
names.push_back(SCP_string("lightshafts"));
if (index >= (int) names.size())
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", names[index].c_str());
}
ADE_FUNC(__len, l_Graphics_Posteffects, nullptr, "Gets the number of available post-processing effects", "number", "number of post-processing effects or 0 on error")
{
SCP_vector<SCP_string> names;
gr_get_post_process_effect_names(names);
// Add one for lightshafts
return ade_set_args(L, "i", ((int) names.size()) + 1);
}
ADE_FUNC(setPostEffect, l_Graphics, "string name, [number value=0, number red=0.0, number green=0.0, number blue=0.0]", "Sets the intensity of the specified post-processing effect. Optionally sets RGB values for effects that use them (valid values are 0.0 to 1.0)", "boolean", "true when successful, false otherwise")
{
const char* name = nullptr;
int intensity = 0;
vec3d rgb; rgb.xyz.x = 0.0f; rgb.xyz.y = 0.0f; rgb.xyz.z = 0.0f; // clang you are a PITA
if (!ade_get_args(L, "s|ifff", &name, &intensity, &rgb.xyz.x, &rgb.xyz.y, &rgb.xyz.z))
return ADE_RETURN_FALSE;
if (name == nullptr || intensity < 0)
return ADE_RETURN_FALSE;
CAP(rgb.xyz.x, 0.0f, 1.0f);
CAP(rgb.xyz.y, 0.0f, 1.0f);
CAP(rgb.xyz.z, 0.0f, 1.0f);
gr_post_process_set_effect(name, intensity, &rgb);
return ADE_RETURN_TRUE;
}
ADE_FUNC(resetPostEffects, l_Graphics, nullptr, "Resets all post-processing effects to their default values", "boolean", "true if successful, false otherwise")
{
gr_post_process_set_defaults();
return ADE_RETURN_TRUE;
}
ADE_VIRTVAR(CurrentOpacityType, l_Graphics, "enumeration", "Current alpha blending type; uses ALPHABLEND_* enumerations", "enumeration", NULL)
{
enum_h *alphatype = NULL;
if(!ade_get_args(L, "*|o", l_Enum.GetPtr(&alphatype)))
return ADE_RETURN_NIL;
if(ADE_SETTING_VAR)
{
if(alphatype != NULL && alphatype->index == LE_ALPHABLEND_FILTER)
lua_Opacity_type = GR_ALPHABLEND_FILTER;
else
lua_Opacity_type = GR_ALPHABLEND_NONE;
}
lua_enum rtn;
switch(lua_Opacity_type)
{
case GR_ALPHABLEND_FILTER:
rtn = LE_ALPHABLEND_FILTER;
break;
default:
rtn = LE_ALPHABLEND_NONE;
}
return ade_set_args(L, "o", l_Enum.Set(enum_h(rtn)));
}
ADE_VIRTVAR(CurrentRenderTarget, l_Graphics, "texture", "Current rendering target", "texture", "Current rendering target, or invalid texture handle if screen is render target")
{
texture_h* newtx = nullptr;
if(ADE_SETTING_VAR && lua_isnil(L, 2))
{
bm_set_render_target(-1);
return ade_set_args(L, "o", l_Texture.Set(texture_h(gr_screen.rendering_to_texture)));
}
else
{
if (!ade_get_args(L, "*|o", l_Texture.GetPtr(&newtx)))
return ade_set_error(L, "o", l_Texture.Set(texture_h()));
if(ADE_SETTING_VAR) {
if (newtx != nullptr && newtx->isValid())
bm_set_render_target(newtx->handle, 0);
else
bm_set_render_target(-1);
}
return ade_set_args(L, "o", l_Texture.Set(texture_h(gr_screen.rendering_to_texture)));
}
}
ADE_VIRTVAR(CurrentResizeMode, l_Graphics, "enumeration ResizeMode", "Current resize mode; uses GR_RESIZE_* enumerations. This resize mode will be used by the gr.* drawing methods.", "enumeration", nullptr)
{
enum_h resize_arg;
if (!ade_get_args(L, "*|o", l_Enum.Get(&resize_arg)))
return ADE_RETURN_NIL;
if (ADE_SETTING_VAR)
{
if (!resize_arg.isValid() || resize_arg.index < LE_GR_RESIZE_NONE || resize_arg.index > LE_GR_RESIZE_MENU_NO_OFFSET)
{
Warning(LOCATION, "Invalid resize mode index %d", resize_arg.index);
return ADE_RETURN_NIL;
}
lua_ResizeMode = resize_arg.index - LE_GR_RESIZE_NONE;
}
return ade_set_args(L, "o", l_Enum.Set(enum_h(static_cast<lua_enum>(LE_GR_RESIZE_NONE + lua_ResizeMode))));
}
ADE_FUNC(clear, l_Graphics, nullptr, "Calls gr_clear(), which fills the entire screen with the currently active color. Useful if you want to have a fresh start for drawing things. (Call this between setClip and resetClip if you only want to clear part of the screen.)", nullptr, nullptr)
{
gr_clear();
SCP_UNUSED(L); // avoid unused parameter warning
return ADE_RETURN_NIL;
}
ADE_FUNC(clearScreen, l_Graphics, "[number|color /* red value or color object */, number green, number blue, number alpha]",
"Clears the screen to black, or the color specified.",
nullptr,
nullptr)
{
int r, g, b, a;
r = g = b = 0;
a = 255;
if (lua_isnumber(L, 1)) {
ade_get_args(L, "|iiii", &r, &g, &b, &a);
} else {
color col;
gr_init_alphacolor(&col, 0, 0, 0, 255);
ade_get_args(L, "|o", l_Color.Get(&col));
r = col.red;
g = col.green;
b = col.blue;
a = col.alpha;
}
//WMC - Set to valid values
if(r != 0 || g != 0 || b != 0 || a!= 255)
{
CAP(r,0,255);
CAP(g,0,255);
CAP(b,0,255);
CAP(a,0,255);
gr_set_clear_color(r,g,b);
gr_screen.current_clear_color.alpha = (ubyte)a;
gr_clear();
gr_set_clear_color(0,0,0);
return ADE_RETURN_NIL;
}
gr_clear();
return ADE_RETURN_NIL;
}
ADE_FUNC(createCamera, l_Graphics,
"string Name, [vector Position, orientation Orientation]",
"Creates a new camera using the specified position and orientation (World)",
"camera",
"Camera handle, or invalid camera handle if camera couldn't be created")
{
const char* s = nullptr;
vec3d *v = &vmd_zero_vector;
matrix_h *mh = NULL;
if(!ade_get_args(L, "s|oo", &s, l_Vector.GetPtr(&v), l_Matrix.GetPtr(&mh)))
return ADE_RETURN_NIL;
matrix *mtx = &vmd_identity_matrix;
if(mh != NULL)
mtx = mh->GetMatrix();
camid cid = cam_create(s, v, mtx);
//Set position
return ade_set_args(L, "o", l_Camera.Set(cid));
}
ADE_FUNC(isMenuStretched, l_Graphics, NULL, "Returns whether the standard interface is stretched", "boolean", "True if stretched, false if aspect ratio is maintained")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
return ade_set_args(L, "b", Cmdline_stretch_menu);
}
ADE_FUNC(getScreenWidth, l_Graphics, NULL, "Gets screen width", "number", "Width in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.max_w);
}
ADE_FUNC(getScreenHeight, l_Graphics, NULL, "Gets screen height", "number", "Height in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.max_h);
}
ADE_FUNC(getCenterWidth, l_Graphics, NULL, "Gets width of center monitor (should be used in conjunction with getCenterOffsetX)", "number", "Width of center monitor in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_w);
}
ADE_FUNC(getCenterHeight, l_Graphics, NULL, "Gets height of center monitor (should be used in conjunction with getCenterOffsetY)", "number", "Height of center monitor in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_h);
}
ADE_FUNC(getCenterOffsetX, l_Graphics, NULL, "Gets X offset of center monitor", "number", "X offset of center monitor in pixels")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_offset_x);
}
ADE_FUNC(getCenterOffsetY, l_Graphics, NULL, "Gets Y offset of center monitor", "number", "Y offset of center monitor in pixels")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_offset_y);
}
ADE_FUNC(getCurrentCamera, l_Graphics, "[boolean]", "Gets the current camera handle, if argument is <i>true</i> then it will also return the main camera when no custom camera is in use", "camera", "camera handle or invalid handle on error")
{
camid current;
bool rtnMain = false;
ade_get_args(L, "|b", &rtnMain);
if (!rtnMain || Viewer_mode & VM_FREECAMERA)
current = cam_get_current();
else
current = Main_camera;
return ade_set_args(L, "o", l_Camera.Set(current));
}
ADE_FUNC(getEyePosition, l_Graphics, nullptr, "Where the viewer's eye is at in World coordinates", "vector", "a vector containing the eye position")
{
return ade_set_args(L, "o", l_Vector.Set(Eye_position));
}
ADE_FUNC(getEyeOrientation, l_Graphics, nullptr, "Where the viewer's eye is pointing in World coordinates", "orientation", "a matrix containing the eye orientation")
{
return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&Eye_matrix)));
}
ADE_FUNC(getEyeFOV, l_Graphics, "[boolean visible_fov=true]", "What the viewer's FOV is, in radians. If visible_fov is true, the visible field of view (for culling) "
"is returned; if false, the camera field of view (for rendering) is returned. For non-VR setups, these two numbers are the same.",
"number", "a number containing the eye fov")
{
bool visible_fov = true;
ade_get_args(L, "|b", &visible_fov);
return ade_set_args(L, "f", g3_get_hfov(Eye_fov, visible_fov));
}
ADE_FUNC(getVectorFromCoords, l_Graphics,
"[number X=center, number Y=center, number Depth, boolean normalize = false]",
"Returns a vector through screen coordinates x and y. "
"If depth is specified, vector is extended to Depth units into space"
"If normalize is true, vector will be normalized.",
"vector",
"Vector, or zero vector on failure")
{
int x = gr_screen.max_w/2;
int y = gr_screen.max_h/2;
float depth = 0.0f;
bool normalize = false;
ade_get_args(L, "|iifb", &x, &y, &depth, &normalize);
vec3d pos = vmd_zero_vector;
bool in_frame = g3_in_frame() > 0;
if(!in_frame) {
g3_start_frame(0);
vec3d cam_pos;
matrix cam_orient;
camid cid;
if (Viewer_mode & VM_FREECAMERA)
cid = cam_get_current();
else
cid = Main_camera;
camera *cam = cid.getCamera();
if (cam != NULL) {
cam->get_info(&cam_pos, &cam_orient);
g3_set_view_matrix(&cam_pos, &cam_orient, View_zoom);
} else {
g3_set_view_matrix(&Eye_position, &Eye_matrix, View_zoom);
}
gr_set_proj_matrix( Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
g3_point_to_vec(&pos, x, y);
if(!in_frame) {
gr_end_view_matrix();
gr_end_proj_matrix();
g3_end_frame();
}
if(depth)
vm_vec_scale(&pos, depth);
if (normalize)
vm_vec_normalize_quick(&pos);
vm_vec_add2(&pos, &View_position);
return ade_set_args(L, "o", l_Vector.Set(pos));
}
ADE_FUNC(setTarget, l_Graphics, "[texture Texture]",
"If texture is specified, sets current rendering surface to a texture."
"Otherwise, sets rendering surface back to screen.",
"boolean",
"True if successful, false otherwise")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
texture_h* idx = nullptr;
ade_get_args(L, "|o", l_Texture.GetPtr(&idx));
if (idx == nullptr) {
return ade_set_args(L, "b", bm_set_render_target(-1, 0));
}
else {
return ade_set_args(L, "b", bm_set_render_target(idx->isValid() ? idx->handle : -1, 0));
}
}
ADE_FUNC(setCamera, l_Graphics, "[camera Camera]", "Sets current camera, or resets camera if none specified", "boolean", "true if successful, false or nil otherwise")
{
camid cid;
if(!ade_get_args(L, "|o", l_Camera.Get(&cid)))
{
cam_reset_camera();
return ADE_RETURN_NIL;
}
if(!cid.isValid())
return ADE_RETURN_NIL;
cam_set_camera(cid);
return ADE_RETURN_TRUE;
}
ADE_FUNC(setColor,
l_Graphics,
"number|color /* red value or color object */, [number Green, number Blue, number Alpha]",
"Sets 2D drawing color for the active context; each color number should be from 0 (darkest) to 255 (brightest)",
nullptr,
nullptr)
{
if (!Gr_inited)
return ADE_RETURN_NIL;
int r, g, b, a = 255;
color col;
if (lua_isnumber(L, 1)) {
if (!ade_get_args(L, "iii|i", &r, &g, &b, &a))
return ADE_RETURN_NIL;
gr_init_alphacolor(&col, r, g, b, a);
} else {
gr_init_alphacolor(&col, 0, 0, 0, 255);
ade_get_args(L, "o", l_Color.Get(&col));
}
LuaScreenContext context;
gr_set_color_fast(&col);
return ADE_RETURN_NIL;
}
ADE_FUNC(getColor,
l_Graphics,
"[boolean]",
"Gets the active 2D drawing color from the active context. False to return raw rgb, true to return a color object. Defaults to false.",
"number, number, number, number | color",
"rgba color which is currently in use for 2D drawing")
{
if(!Gr_inited)
return ADE_RETURN_NIL;
bool rc = false;
ade_get_args(L, "|b", &rc);
LuaScreenContext context;
const color cur = GR_CURRENT_COLOR;
if (!rc) {
return ade_set_args(L, "iiii", static_cast<int>(cur.red), static_cast<int>(cur.green), static_cast<int>(cur.blue), static_cast<int>(cur.alpha));
} else {
return ade_set_args(L, "o", l_Color.Set(cur));
}
}
ADE_FUNC(setLineWidth, l_Graphics, "[number width=1.0]", "Sets the line width for lines for the active context. This call might fail if the specified width is not supported by the graphics implementation. Then the width will be the nearest supported value.", "boolean", "true if succeeded, false otherwise")
{
if(!Gr_inited)
return ADE_RETURN_FALSE;
float width = 1.0f;
ade_get_args(L, "|f", &width);
if (width <= 0.0f)
{
return ADE_RETURN_FALSE;
}
LuaScreenContext context;
gr_set_line_width(width);
return ADE_RETURN_TRUE;
}
ADE_FUNC(drawCircle, l_Graphics, "number Radius, number X, number Y, [boolean Filled=true]", "Draws a circle using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,ra;
bool fill = true;
if(!ade_get_args(L, "iii|b", &ra,&x,&y,&fill))
return ADE_RETURN_NIL;
LuaScreenContext context;
if (fill) {
//WMC - Circle takes...diameter.
gr_circle(x,y, ra*2, lua_ResizeMode);
} else {
gr_unfilled_circle(x,y, ra*2, lua_ResizeMode);
}
return ADE_RETURN_NIL;
}
ADE_FUNC(drawArc, l_Graphics, "number Radius, number X, number Y, number StartAngle, number EndAngle, [boolean Filled=true]", "Draws an arc using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y;
float ra,angle_start,angle_end;
bool fill = true;
if(!ade_get_args(L, "fiiff|b", &ra,&x,&y,&angle_start,&angle_end,&fill)) {
return ADE_RETURN_NIL;
}
LuaScreenContext context;
gr_arc(x,y, ra, angle_start, angle_end, fill, lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawCurve, l_Graphics, "number X, number Y, number Radius", "Draws a curve using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,ra,dir = 0;
if(!ade_get_args(L, "iii|i", &x,&y,&ra, &dir))
return ADE_RETURN_NIL;
LuaScreenContext context;
//WMC - direction should be settable at a certain point via enumerations.
//Not gonna deal with it now.
gr_curve(x, y, ra, dir, lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawGradientLine, l_Graphics, "number X1, number Y1, number X2, number Y2", "Draws a line from (x1,y1) to (x2,y2) that steadily fades out using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return 0;
int x1,y1,x2,y2;
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
return ADE_RETURN_NIL;
LuaScreenContext context;
gr_gradient(x1,y1,x2,y2,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawLine, l_Graphics, "number X1, number Y1, number X2, number Y2", "Draws a line from (x1,y1) to (x2,y2) using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x1,y1,x2,y2;
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
return ADE_RETURN_NIL;
LuaScreenContext context;
gr_line(x1,y1,x2,y2,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawPixel, l_Graphics, "number X, number Y", "Draws a pixel using active context values (like color or width).", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y;
if(!ade_get_args(L, "ii", &x, &y))
return ADE_RETURN_NIL;
LuaScreenContext context;
gr_pixel(x,y,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawPolygon,
l_Graphics,
"texture Texture, [vector Position /* Default is null vector */, orientation Orientation=nil, number Width=1.0, "
"number Height=1.0]",
"Draws a polygon using active context values (like color or width). May not work properly in hooks other than On Object Render.",
nullptr,
nullptr)
{
texture_h* tdx = nullptr;
vec3d pos = vmd_zero_vector;
matrix_h *mh = NULL;
float width = 1.0f;
float height = 1.0f;
if (!ade_get_args(L, "o|ooff", l_Texture.GetPtr(&tdx), l_Vector.Get(&pos), l_Matrix.GetPtr(&mh), &width, &height))
return ADE_RETURN_NIL;
if (!tdx->isValid())
return ADE_RETURN_FALSE;
LuaScreenContext context;
matrix *orip = &vmd_identity_matrix;
if(mh != NULL)
orip = mh->GetMatrix();
//Do 3D stuff
bool in_frame = g3_in_frame() > 0;
if(!in_frame)
g3_start_frame(0);
//gr_set_bitmap(tdx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL, lua_Opacity);
//g3_draw_polygon(&pos, orip, width, height, TMAP_FLAG_TEXTURED | TMAP_HTL_3D_UNLIT);
material mat_params;
material_set_unlit(&mat_params, tdx->handle, lua_Opacity, lua_Opacity_type == GR_ALPHABLEND_FILTER ? true : false,
false);
g3_render_rect_oriented(&mat_params, &pos, orip, width, height);
if(!in_frame)
g3_end_frame();
return ADE_RETURN_TRUE;
}
void drawRectInternal(int x1, int x2, int y1, int y2, bool f = true, float a = 0.f)
{
LuaScreenContext context;
if(f)
{
gr_set_bitmap(0); // gr_rect will use the last bitmaps info, so set to zero to flush any previous alpha state
gr_rect(x1, y1, x2-x1, y2-y1, lua_ResizeMode, a);
}
else
{
if (a != 0) {
float centerX = (x1 + x2) / 2.0f;
float centerY = (y1 + y2) / 2.0f;
//We need to calculate each point individually due to the rotation, as they won't always align horizontally and vertically.
float AX = cosf(a) * (x1 - centerX) - sinf(a) * (y1 - centerY) + centerX;
float AY = sinf(a) * (x1 - centerX) + cosf(a) * (y1 - centerY) + centerY;
float BX = cosf(a) * (x2 - centerX) - sinf(a) * (y1 - centerY) + centerX;
float BY = sinf(a) * (x2 - centerX) + cosf(a) * (y1 - centerY) + centerY;
float CX = cosf(a) * (x2 - centerX) - sinf(a) * (y2 - centerY) + centerX;
float CY = sinf(a) * (x2 - centerX) + cosf(a) * (y2 - centerY) + centerY;
float DX = cosf(a) * (x1 - centerX) - sinf(a) * (y2 - centerY) + centerX;
float DY = sinf(a) * (x1 - centerX) + cosf(a) * (y2 - centerY) + centerY;
gr_line(fl2i(AX), fl2i(AY), fl2i(BX), fl2i(BY), lua_ResizeMode);
gr_line(fl2i(BX), fl2i(BY), fl2i(CX), fl2i(CY), lua_ResizeMode);
gr_line(fl2i(CX), fl2i(CY), fl2i(DX), fl2i(DY), lua_ResizeMode);
gr_line(fl2i(DX), fl2i(DY), fl2i(AX), fl2i(AY), lua_ResizeMode);
}
else {
gr_line(x1, y1, x2, y1, lua_ResizeMode); //Top
gr_line(x1, y2, x2, y2, lua_ResizeMode); //Bottom
gr_line(x1, y1, x1, y2, lua_ResizeMode); //Left
gr_line(x2, y1, x2, y2, lua_ResizeMode); //Right
}
}
}
ADE_FUNC(drawRectangle, l_Graphics, "number X1, number Y1, number X2, number Y2, [boolean Filled=true, number angle=0.0]", "Draws a rectangle using active context values (like color or width). May be rotated by passing the angle parameter in radians.", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x1,y1,x2,y2;
bool f=true;
float a = 0;
if(!ade_get_args(L, "iiii|bf", &x1, &y1, &x2, &y2, &f, &a))
return ADE_RETURN_NIL;
drawRectInternal(x1, x2, y1, y2, f, a);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawRectangleCentered, l_Graphics,
"number X, number Y, number Width, number Height, [boolean Filled=true, number angle=0.0]",
"Draws a rectangle centered at X,Y using active context values (like color or width). May be rotated by passing the angle parameter in radians.", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,w,h;
bool f=true;
float a = 0;
if(!ade_get_args(L, "iiii|bf", &x, &y, &w, &h, &f, &a))
return ADE_RETURN_NIL;
int x1 = x - (w / 2);
int x2 = x + (w / 2);
int y1 = y - (h / 2);
int y2 = y + (h / 2);
drawRectInternal(x1, x2, y1, y2, f, a);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawSphere, l_Graphics, "[number Radius = 1.0, vector Position]", "Draws a sphere with radius Radius at world vector Position using active context values (like color). May not work properly in hooks other than On Object Render.", "boolean", "True if successful, false or nil otherwise")
{
float rad = 1.0f;
vec3d pos = vmd_zero_vector;
ade_get_args(L, "|fo", &rad, l_Vector.Get(&pos));
LuaScreenContext context;
bool in_frame = g3_in_frame() > 0;
if(!in_frame) {
g3_start_frame(0);
vec3d cam_pos;
matrix cam_orient;
camid cid;
if (Viewer_mode & VM_FREECAMERA)
cid = cam_get_current();
else
cid = Main_camera;
camera *cam = cid.getCamera();
if (cam != NULL) {
cam->get_info(&cam_pos, &cam_orient);
g3_set_view_matrix(&cam_pos, &cam_orient, View_zoom);
} else {
g3_set_view_matrix(&Eye_position, &Eye_matrix, View_zoom);
}
gr_set_proj_matrix( Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
vertex vtx;
vtx.world = pos;
g3_rotate_vertex(&vtx, &pos);
g3_draw_sphere(&vtx, rad);
if(!in_frame) {
gr_end_view_matrix();
gr_end_proj_matrix();
g3_end_frame();
}
return ADE_RETURN_TRUE;
}
ADE_FUNC(draw3dLine, l_Graphics, "vector origin, vector destination, [boolean translucent=true, number thickness=1.0, number thicknessEnd=thickness]", "Draws a line from origin to destination using active context values (like color). "
"The line may be translucent or solid. Translucent lines will NOT use the alpha value, instead being more transparent the darker the color is. "
"The thickness at the start can be different from the thickness at the end, to draw a line that tapers or expands.", nullptr, nullptr)
{
vec3d *v1 = &vmd_zero_vector;
vec3d *v2 = &vmd_zero_vector;
bool translucent = true;
float thickness = 1.0f;
float thicknessEnd = -1.0f;
if (!ade_get_args(L, "oo|bff", l_Vector.GetPtr(&v1), l_Vector.GetPtr(&v2), &translucent, &thickness, &thicknessEnd))
return ADE_RETURN_NIL;
LuaScreenContext context;
if (thickness < 0) thickness = 0;
if (thicknessEnd < 0) thicknessEnd = thickness;
if (thickness <= 0 && thicknessEnd <= 0)
{
if (!WarnedBadThicknessLine)
{
LuaError(L, "At least one end of a 3D line must be greater than zero! The line won't be drawn!\nThis warning won't be shown again this play session.");
WarnedBadThicknessLine = true;
}
//While the batching_add_line function would also do this check, I feel that its good to return early here.
return ADE_RETURN_NIL;
}
batching_add_line(v1, v2, thickness, thicknessEnd, GR_CURRENT_COLOR, translucent);
return ADE_RETURN_NIL;
}
// Aardwolf's test code to render a model, supposed to emulate WMC's gr.drawModel function
ADE_FUNC(drawModel, l_Graphics, "model model, vector position, orientation orientation",
"Draws the given model with the specified position and orientation. "
"Note: this method does NOT use CurrentResizeMode.",
"number", "Zero if successful, otherwise an integer error code")
{
model_h *mdl = NULL;
vec3d *v = &vmd_zero_vector;
matrix_h *mh = NULL;
if(!ade_get_args(L, "ooo", l_Model.GetPtr(&mdl), l_Vector.GetPtr(&v), l_Matrix.GetPtr(&mh)))
return ade_set_args(L, "i", 1);
if(mdl == NULL)
return ade_set_args(L, "i", 2);
int model_num = mdl->GetID();
if(model_num < 0)
return ade_set_args(L, "i", 3);
// Make sure we have a scene to use
// Note that this is only relevant, and thus only expected to be set, in OBJECTRENDER hooks
if (scripting::hooks::OnObjectRender->isActive() && !Current_scene)
return ade_set_args(L, "i", 4);
//Handle angles
matrix *orient = mh->GetMatrix();
//Clip
gr_set_clip(0, 0, gr_screen.max_w, gr_screen.max_h, GR_RESIZE_NONE); // don't use lua_ResizeMode here since this function handles its own resizing
//Handle 3D init stuff