FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Avara/src/BSPViewer.cpp at main · assertivist/Avara · GitHub
assertivist
/
Avara
Public
forked from
avaraline/Avara
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Avara
/
src
/
BSPViewer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
419 lines (372 loc) · 13.9 KB
Breadcrumbs
Avara
/
src
/
BSPViewer.cpp
Copy path
File metadata and controls
419 lines (372 loc) · 13.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Copyright ©1995-1996, Juri Munkki
All rights reserved.
File: BSPViewer.c
Created: Thursday, July 27, 1995, 23:10
Modified: Tuesday, February 20, 1996, 08:02
*/
#
include
"
AvaraGL.h
"
#
include
"
CApplication.h
"
#
include
"
CBSPPart.h
"
#
include
"
CBSPWorld.h
"
#
include
"
ColorManager.h
"
#
include
"
CViewParameters.h
"
#
include
"
CWorldShader.h
"
#
include
"
ARGBColor.h
"
#
include
"
csscolorparser.hpp
"
#
include
"
Resource.h
"
#
include
<
SDL2/SDL.h
>
#
include
<
iostream
>
#
include
<
string
>
#
include
<
sstream
>
#
define
STB_IMAGE_WRITE_IMPLEMENTATION
extern
"
C
"
{
#
include
"
stb_image_write.h
"
}
class
BSPViewer
:
public
CApplication
{
public:
CBSPPart *itsPart;
CBSPWorldImpl *itsWorld;
CViewParameters *itsView;
CWorldShader *worldShader;
Vector location, orientation;
int
current_id;
bool
update =
true
;
bool
skyToggle =
true
;
bool
helpToggle =
true
;
ARGBColor marker1 = (*
ColorManager::getMarkerColor
(
0
)).WithA(
0xff
);
ARGBColor marker2 = (*
ColorManager::getMarkerColor
(
1
)).WithA(
0xff
);
ARGBColor marker3 = (*
ColorManager::getMarkerColor
(
2
)).WithA(
0xff
);
ARGBColor marker4 = (*
ColorManager::getMarkerColor
(
3
)).WithA(
0xff
);
std::string help_text;
BSPViewer
(
int
id) : CApplication(
"
BSP Viewer
"
) {
AvaraGLInitContext
();
glClearColor
(
1
,
0
,
1
,
1
);
glEnable
(
GL_DEPTH_TEST
);
glDepthFunc
(
GL_LEQUAL
);
glBlendFunc
(
GL_ONE
,
GL_ZERO
);
std::stringstream help;
help <<
"
AWSD: rotate model to cardinal directions
\n
"
;
help <<
"
Left click: move model on z axis
\n
"
;
help <<
"
Right click: rotate model
\n
"
;
help <<
"
R/T: previous/next shape
\n
"
;
help <<
"
U: sky toggle
\n
"
;
help <<
"
0: capture image
\n
"
;
help <<
"
F1: show/hide this text
"
;
help_text = help.
str
();
AvaraGLSetFOV
(
50
);
current_id = id;
itsWorld =
new
CBSPWorldImpl
(
1
);
newPart
(current_id);
itsView =
new
CViewParameters;
itsView->
yonBound
=
FIX
(
100
);
itsView->
dirtyLook
=
true
;
itsView->
LookFrom
(
0
,
0
,
FIX
(
1.5
));
itsView->
LookAtPart
(itsPart);
itsView->
PointCamera
();
worldShader =
new
CWorldShader;
worldShader->
IWorldShader
();
worldShader->
skyShadeCount
=
12
;
AvaraGLSetLight
(
0
,
1.0
,
85
,
165
,
0xffffffff
);
AvaraGLSetLight
(
1
,
0
,
45
,
0
,
0xffffffff
);
AvaraGLSetAmbient
(
0.31
,
0xffffffff
);
skyToggle =
true
;
helpToggle =
true
;
location[
0
] =
FIX
(
0
);
location[
1
] =
FIX
(
0
);
location[
2
] =
FIX
(
0
);
orientation[
0
] =
FIX
(
0
);
orientation[
1
] =
FIX
(
0
);
orientation[
2
] =
FIX
(
0
);
}
void
updateClearColor
(CSSColorParser::Color c) {
glClearColor
(c.
r
, c.
g
, c.
b
, c.
a
);
}
void
helpText
() {
if
(!helpToggle)
return
;
std::stringstream info;
info << help_text <<
"
\n
"
;
info <<
"
Model:
"
<< current_id <<
"
\n
"
;
info <<
"
Translation:
"
<<
ToFloat
(location[
0
]) <<
"
,
"
<<
ToFloat
(location[
1
]) <<
"
,
"
<<
ToFloat
(location[
2
]) <<
"
\n
"
;
info <<
"
Rotation:
"
<<
ToFloat
(orientation[
0
]) <<
"
,
"
<<
ToFloat
(orientation[
1
]) <<
"
,
"
<<
ToFloat
(orientation[
2
]);
nvgBeginFrame
(
mNVGContext
,
mFBSize
[
0
],
mFBSize
[
1
],
1.0
);
nvgFontSize
(
mNVGContext
,
25
);
nvgFontFace
(
mNVGContext
,
"
mono
"
);
nvgTextBox
(
mNVGContext
,
10
,
30
,
500
, info.
str
().
c_str
(),
NULL
);
nvgEndFrame
(
mNVGContext
);
}
bool
newPart
(
int
id) {
if
(itsWorld->
GetPartCount
() >
0
) {
itsWorld->
RemovePart
(itsPart);
}
itsPart =
CBSPPart::Create
(id);
if
(itsPart->
polyCount
>
0
) {
itsPart->
ReplaceColor
(
*
ColorManager::getMarkerColor
(
0
),
marker1
);
itsPart->
ReplaceColor
(
*
ColorManager::getMarkerColor
(
1
),
marker2
);
itsPart->
ReplaceColor
(
*
ColorManager::getMarkerColor
(
2
),
marker3
);
itsPart->
ReplaceColor
(
*
ColorManager::getMarkerColor
(
3
),
marker4
);
AvaraGLUpdateData
(itsPart);
itsWorld->
AddPart
(itsPart);
SDL_Log
(
"
Loaded BSP %d
"
, id);
return
true
;
}
else
return
false
;
}
bool
HandleEvent
(SDL_Event &event) {
Vector prevLocation;
memcpy
(prevLocation, location,
sizeof
(Vector));
Vector prevOrientation;
memcpy
(prevOrientation, orientation,
sizeof
(Vector));
switch
(event.
type
) {
case
SDL_MOUSEMOTION
:
if
(event.
motion
.
state
&
SDL_BUTTON
(
SDL_BUTTON_LEFT
)) {
location[
2
] +=
FIX
((
float
)event.
motion
.
yrel
/
5.0
);
update =
true
;
}
if
(event.
motion
.
state
&
SDL_BUTTON
(
SDL_BUTTON_RIGHT
)) {
orientation[
0
] +=
FIX
((
float
)event.
motion
.
yrel
/
5.0
);
orientation[
1
] +=
FIX
((
float
)event.
motion
.
xrel
/
5.0
);
update =
true
;
}
break
;
case
SDL_KEYDOWN
:
switch
(event.
key
.
keysym
.
sym
) {
case
SDLK_LEFT
:
orientation[
1
] -=
FIX
(
5
);
update =
true
;
break
;
case
SDLK_RIGHT
:
orientation[
1
] +=
FIX
(
5
);
update =
true
;
break
;
case
SDLK_DOWN
:
orientation[
0
] -=
FIX
(
5
);
update =
true
;
break
;
case
SDLK_UP
:
orientation[
0
] +=
FIX
(
5
);
update =
true
;
break
;
case
SDLK_w:
orientation[
0
] =
FIX
(
90
);
orientation[
1
] =
FIX
(
0
);
update =
true
;
break
;
case
SDLK_s:
orientation[
0
] =
FIX
(-
90
);
orientation[
1
] =
FIX
(
0
);
update =
true
;
break
;
case
SDLK_d:
orientation[
0
] =
FIX
(
0
);
orientation[
1
] =
FIX
(
90
);
update =
true
;
break
;
case
SDLK_a:
orientation[
0
] =
FIX
(
0
);
orientation[
1
] =
FIX
(-
90
);
return
true
;
case
SDLK_r:
do
{
current_id++;
if
(current_id >
INT32_MAX
) {
current_id =
1
;
}
}
while
(
newPart
(current_id) !=
true
);
return
true
;
case
SDLK_t:
do
{
current_id--;
if
(current_id <
1
) {
current_id =
INT32_MAX
;
}
}
while
(
newPart
(current_id) !=
true
);
return
true
;
case
SDLK_y:
newPart
(current_id);
return
true
;
case
SDLK_u:
skyToggle = !skyToggle;
return
true
;
case
SDLK_F1
:
helpToggle = !helpToggle;
return
true
;
case
SDLK_0
:
std::stringstream ss;
ss << current_id <<
'
-
'
<<
SDL_GetTicks
() <<
"
.png
"
;
capture
(ss.
str
().
c_str
());
return
true
;
}
break
;
}
if
(update) {
if
(prevLocation[
0
] != location[
0
] ||
prevLocation[
1
] != location[
1
] ||
prevLocation[
2
] != location[
2
] ||
prevOrientation[
0
] != orientation[
0
] ||
prevOrientation[
1
] != orientation[
1
] ||
prevOrientation[
2
] != orientation[
2
])
SDL_Log
(
"
-s %d --location %f %f %f --orientation %f %f %f
"
, current_id,
ToFloat
(location[
0
]),
ToFloat
(location[
1
]),
ToFloat
(location[
2
]),
ToFloat
(orientation[
0
]),
ToFloat
(orientation[
1
]),
ToFloat
(orientation[
2
]));
}
return
update;
}
int
capture
(
const
char
* fn) {
drawContents
();
//
SDL_GL_SwapWindow(app->window);
int
w =
mFBSize
[
0
];
int
h =
mFBSize
[
1
];
int
comp =
3
;
std::vector<GLchar>
pic
(w * h * comp);
glReadPixels
(
0
,
0
, w, h, comp ==
4
?
GL_RGBA
:
GL_RGB
,
GL_UNSIGNED_BYTE
, pic.
data
());
for
(
int
line =
0
; line != h /
2
; ++line) {
std::swap_ranges
(
pic.
begin
() + comp * w * line,
pic.
begin
() + comp * w * (line +
1
),
pic.
begin
() + comp * w * (h - line -
1
));
}
SDL_Log
(
"
Writing %s
"
, fn);
return
stbi_write_png
(fn, w, h, comp, pic.
data
(), w * comp);
}
void
drawContents
() {
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
|
GL_STENCIL_BUFFER_BIT
);
AvaraGLViewport
(
mFBSize
[
0
],
mFBSize
[
1
]);
itsView->
SetViewRect
(
mFBSize
[
0
],
mFBSize
[
1
],
mFBSize
[
0
] /
2
,
mFBSize
[
1
] /
2
);
itsView->
viewPixelRatio
=
FIX
(
4.0
/
3.0
);
itsView->
CalculateViewPyramidCorners
();
itsView->
PointCamera
();
itsPart->
Reset
();
itsPart->
RotateZ
(orientation[
2
]);
itsPart->
RotateY
(orientation[
1
]);
itsPart->
RotateX
(orientation[
0
]);
TranslatePart
(itsPart, location[
0
], location[
1
], location[
2
]);
itsPart->
MoveDone
();
if
(skyToggle)
worldShader->
ShadeWorld
(itsView);
itsWorld->
Render
(itsView);
helpText
();
}
};
int
main
(
int
argc,
char
*argv[]) {
//
Init Avara stuff.
InitMatrix
();
nanogui::init
();
//
The BSPViewer application itself.
BSPViewer *app =
new
BSPViewer
(
215
);
for
(
int
i =
1
; i < argc; i++) {
std::string arg = argv[i];
if
(arg ==
"
-s
"
|| arg ==
"
--shape
"
) {
u_int16_t
id =
atoi
(argv[++i]);
app->
current_id
= id;
app->
newPart
(id);
}
if
(arg ==
"
--base
"
) {
std::string lbase =
std::string
(argv[++i]);
UseBaseFolder
(lbase);
app->
newPart
(app->
current_id
);
}
if
(arg ==
"
--set
"
) {
std::string lset =
std::string
(argv[++i]);
UseLevelFolder
(lset);
app->
newPart
(app->
current_id
);
}
if
(arg ==
"
--orientation
"
) {
app->
newPart
(app->
current_id
);
app->
orientation
[
0
] =
ToFixed
(
atof
(argv[++i]));
app->
orientation
[
1
] =
ToFixed
(
atof
(argv[++i]));
app->
orientation
[
2
] =
ToFixed
(
atof
(argv[++i]));
}
if
(arg ==
"
--location
"
) {
app->
newPart
(app->
current_id
);
app->
location
[
0
] =
ToFixed
(
atof
(argv[++i]));
app->
location
[
1
] =
ToFixed
(
atof
(argv[++i]));
app->
location
[
2
] =
ToFixed
(
atof
(argv[++i]));
}
else
if
(arg ==
"
--marker-color1
"
) {
std::string s =
std::string
(argv[++i]);
std::optional<ARGBColor> c =
ARGBColor::Parse
(s);
if
(c.
has_value
()) {
app->
marker1
= *c;
app->
newPart
(app->
current_id
);
}
}
else
if
(arg ==
"
--marker-color2
"
) {
std::string s =
std::string
(argv[++i]);
std::optional<ARGBColor> c =
ARGBColor::Parse
(s);
if
(c.
has_value
()) {
app->
marker2
= *c;
app->
newPart
(app->
current_id
);
}
}
else
if
(arg ==
"
--marker-color3
"
) {
std::string s =
std::string
(argv[++i]);
std::optional<ARGBColor> c =
ARGBColor::Parse
(s);
if
(c.
has_value
()) {
app->
marker3
= *c;
app->
newPart
(app->
current_id
);
}
}
else
if
(arg ==
"
--marker-color4
"
) {
std::string s =
std::string
(argv[++i]);
std::optional<ARGBColor> c =
ARGBColor::Parse
(s);
if
(c.
has_value
()) {
app->
marker4
= *c;
app->
newPart
(app->
current_id
);
}
}
else
if
(arg ==
"
--background-color
"
) {
app->
skyToggle
=
false
;
std::string s =
std::string
(argv[++i]);
std::optional<CSSColorParser::Color> c =
CSSColorParser::parse
(s);
if
(c.
has_value
()) app->
updateClearColor
(*c);
}
else
if
(arg ==
"
--output-file
"
) {
exit
(app->
capture
(argv[++i]));
}
else
if
(arg ==
"
--ambient
"
) {
AvaraGLSetAmbient
(
atof
(argv[++i]),
0xffffffff
);
}
else
if
(arg ==
"
--hull-emotes
"
) {
int
hulls[
3
] = {
215
,
216
,
217
};
std::string hull_names[] = {
"
Light
"
,
"
Medium
"
,
"
Heavy
"
};
app->
orientation
[
0
] =
ToFixed
(
14
);
app->
orientation
[
1
] =
ToFixed
(
25.2
);
app->
location
[
2
] =
ToFixed
(-
0.44007
);
app->
skyToggle
=
false
;
std::stringstream ss;
int
error =
0
;
for
(
size_t
i =
0
; i <
3
; i++) {
for
(
size_t
j =
0
; j <
kMaxTeamColors
; j++) {
app->
marker1
= *
ColorManager::getTeamColor
(j);
app->
newPart
(hulls[i]);
app->
drawContents
();
ss.
str
(
"
"
);
ss << hull_names[i] <<
"
-
"
<< *
ColorManager::getTeamColorName
(j) <<
"
.png
"
;
error += app->
capture
(ss.
str
().
c_str
());
}
}
SDL_Quit
();
exit
(error);
}
}
//
Init SDL and nanogui.
nanogui::init
();
//
Init Avara stuff.
InitMatrix
();
//
Wait at most 10ms for any event.
nanogui::mainloop
(
10
);
//
Shut it down!!
nanogui::shutdown
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL