forked from MDFL64/vbsp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
873 lines (668 loc) · 22.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <ctype.h>
#include <emscripten/emscripten.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/glfw.h>
#include "glm/mat4x4.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/rotate_vector.hpp"
// Structures from https://github.com/ValveSoftware/source-sdk-2013/blob/master/sp/src/public/bspfile.h
// See also https://developer.valvesoftware.com/wiki/Source_BSP_File_Format
#define HEADER_LUMPS 64
#define LUMP_ENTITIES 0
#define LUMP_PLANES 1
#define LUMP_TEXDATA 2
#define LUMP_VERTS 3
#define LUMP_TEXINFO 6
#define LUMP_FACES 7
#define LUMP_EDGES 12
#define LUMP_EDGE_INDEX 13
#define LUMP_MODELS 14
#define LUMP_DISPINFO 26
#define LUMP_DISPVERTS 33
#define LUMP_TEXDATA_STRING_DATA 43
#define LUMP_TEXDATA_STRING_TABLE 44
#define PI_OVER_180 0.01745329251
#define CAM_MOVE_SPEED 10.0f
#define CAM_FAST_SPEED 50.0f
#define CAM_ROT_SPEED 3
struct header_lump_t {
int offset;
int size;
int version;
int _unused;
};
struct bsp_header_t {
int bsp_ident;
int bsp_version;
header_lump_t lumps[HEADER_LUMPS];
int map_version;
};
struct face_t {
unsigned short planenum; // the plane number
char side; // faces opposite to the node's plane direction
char onNode; // 1 of on node, 0 if in leaf
int firstedge; // index into surfedges
short numedges; // number of surfedges
short texinfo; // texture info
short dispinfo; // displacement info
short surfaceFogVolumeID; // ?
char styles[4]; // switchable lighting info
int lightofs; // offset into lightmap lump
float area; // face area in units^2
int LightmapTextureMinsInLuxels[2]; // texture lighting info
int LightmapTextureSizeInLuxels[2]; // texture lighting info
int origFace; // original face this was split from
unsigned short numPrims; // primitives
unsigned short firstPrimID;
unsigned int smoothingGroups; // lightmap smoothing group
};
struct edge_t {
unsigned short v[2];
};
struct vector_t {
float x;
float y;
float z;
vector_t operator+(const vector_t& right) const {
vector_t result;
result.y = y + right.y;
result.x = x + right.x;
result.z = z + right.z;
return result;
}
vector_t operator-(const vector_t& right) const {
vector_t result;
result.x = x - right.x;
result.y = y - right.y;
result.z = z - right.z;
return result;
}
vector_t operator*(const float right) const {
vector_t result;
result.x = x * right;
result.y = y * right;
result.z = z * right;
return result;
}
};
struct plane_t {
vector_t normal;
float dist;
int type;
};
struct texture_info_t
{
float textureVecs[2][4];
float lightmapVecs[2][4];
int flags;
int texdata;
};
struct texture_data_t
{
vector_t reflectivity;
int nameStringTableID;
int width, height;
int view_width, view_height;
};
struct model_t
{
vector_t mins, maxs; // bounding box
vector_t origin; // for sounds or lights
int headnode; // index into node array
int firstface, numfaces; // index into face array
};
struct dispinfo_t
{
vector_t startPosition; // start position used for orientation
int DispVertStart; // Index into LUMP_DISP_VERTS.
int DispTriStart; // Index into LUMP_DISP_TRIS.
int power; // power - indicates size of surface (2^power 1)
int minTess; // minimum tesselation allowed
float smoothingAngle; // lighting smoothing angle
int contents; // surface contents
unsigned short MapFace; // Which map face this displacement comes from.
int LightmapAlphaStart; // Index into ddisplightmapalpha.
int LightmapSamplePositionStart; // Index into LUMP_DISP_LIGHTMAP_SAMPLE_POSITIONS.
//CDispNeighbor EdgeNeighbors[4]; // Indexed by NEIGHBOREDGE_ defines.
//CDispCornerNeighbors CornerNeighbors[4]; // Indexed by CORNER_ defines.
char neighbor_data[86];
unsigned int AllowedVerts[10]; // active verticies
};
struct dispvert_t {
vector_t pos;
float distance;
float alpha;
};
struct gl_vert_t {
vector_t pos;
vector_t normal;
float r;
float g;
float b;
};
GLuint h_vertexbuffer_opaque = 0;
GLuint h_vertexbuffer_trans = 0;
GLuint h_vertexbuffer_sky = 0;
int vert_count_opaque = 0;
int vert_count_trans = 0;
int vert_count_sky = 0;
vector_t findNormal(const vector_t& a,const vector_t& b,const vector_t& c) {
vector_t u = b - c;
vector_t v = a - c;
vector_t norm;
norm.x = u.y*v.z - u.z*v.y;
norm.y = u.z*v.x - u.x*v.z;
norm.z = u.x*v.y - u.y*v.x;
float len = sqrt(norm.x*norm.x + norm.y*norm.y + norm.z*norm.z);
norm.x /= len;
norm.y /= len;
norm.z /= len;
return norm;
}
glm::vec3 cam_pos(0,0,0);
float cam_pitch = 0;
float cam_yaw = 0;
bool skybox_active = false;
glm::vec3 skybox_pos;
float skybox_scale;
float sky_r;
float sky_g;
float sky_b;
float ambient_r;
float ambient_g;
float ambient_b;
float light_r;
float light_g;
float light_b;
float light_x;
float light_y;
float light_z;
vector_t* model_offsets = 0;
extern "C" {
extern int pick_color(char* name,float alpha);
extern int parse_ents(char* data);
void setCam(float x, float y, float z, float pa, float ya) {
cam_pos.x = x;
cam_pos.y = y;
cam_pos.z = z;
cam_pitch = pa*PI_OVER_180;
cam_yaw = ya *PI_OVER_180;
}
void setSkybox(float x, float y, float z, float scale) {
skybox_active = true;
skybox_pos.x = x;
skybox_pos.y = y;
skybox_pos.z = z;
skybox_scale = scale;
}
void setSkyColor(float r, float g, float b) {
sky_r = r;
sky_g = g;
sky_b = b;
}
void setAmbient(float r, float g, float b) {
ambient_r = r;
ambient_g = g;
ambient_b = b;
}
void setLight(float r, float g, float b) {
light_r = r;
light_g = g;
light_b = b;
}
void setLightAngle(float pitch, float yaw) {
light_x = sin(yaw*PI_OVER_180) * cos(pitch*PI_OVER_180);
light_y = cos(yaw*PI_OVER_180) * cos(pitch*PI_OVER_180);
light_z = -sin(pitch*PI_OVER_180);
}
void setModel(int model_id, float x, float y, float z) {
model_offsets[model_id].x = x;
model_offsets[model_id].y = y;
model_offsets[model_id].z = z;
}
}
extern "C"
int loadMap(bsp_header_t* bsp_file) {
if (bsp_file->bsp_ident!=1347633750) {
printf("Not a BSP!\n");
return 1;
}
printf("Format V%i\n",bsp_file->bsp_version);
// Models!
model_t* models = reinterpret_cast<model_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_MODELS].offset);
int model_count = bsp_file->lumps[LUMP_MODELS].size / sizeof(model_t);
if (model_offsets != 0)
delete[] model_offsets;
model_offsets = new vector_t[model_count];
model_offsets[0].x = 0;
model_offsets[0].y = 0;
model_offsets[0].z = 0;
// Parse the entities to get model offsets, spawn position, skybox settings...
skybox_active = false;
char* ent_data = reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_ENTITIES].offset;
parse_ents(ent_data);
// Mesh
face_t* faces = reinterpret_cast<face_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_FACES].offset);
int* edge_index = reinterpret_cast<int*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_EDGE_INDEX].offset);
edge_t* edges = reinterpret_cast<edge_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_EDGES].offset);
vector_t* verts = reinterpret_cast<vector_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_VERTS].offset);
// Need for normals
plane_t* planes = reinterpret_cast<plane_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_PLANES].offset);
// Material flags, names
texture_info_t* texture_info = reinterpret_cast<texture_info_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_TEXINFO].offset);
texture_data_t* texture_data = reinterpret_cast<texture_data_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_TEXDATA].offset);
int* texture_string_table = reinterpret_cast<int*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_TEXDATA_STRING_TABLE].offset);
char* texture_string_data = reinterpret_cast<char*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_TEXDATA_STRING_DATA].offset);
// Displacements!
dispinfo_t* displacements = reinterpret_cast<dispinfo_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_DISPINFO].offset);
dispvert_t* disp_verts = reinterpret_cast<dispvert_t*>(reinterpret_cast<char*>(bsp_file)+bsp_file->lumps[LUMP_DISPVERTS].offset);
std::vector<gl_vert_t> mesh_opaque;
std::vector<gl_vert_t> mesh_trans;
std::vector<gl_vert_t> mesh_sky;
// texture_info[0].texdata
for (int i=0; i < model_count; i++) {
int start_face = models[i].firstface;
int last_face = models[i].firstface + models[i].numfaces;
for (int j= start_face; j < last_face; j++) {
bool nodraw = texture_info[faces[j].texinfo].flags & 0x280; // todo need more?
if (nodraw)
continue;
bool sky = texture_info[faces[j].texinfo].flags & 6;
bool transparent = texture_info[faces[j].texinfo].flags & 0x18;
std::vector<gl_vert_t>& mesh = transparent ? mesh_trans : (sky ? mesh_sky : mesh_opaque);
//texture_data[texture_info[faces[j].texinfo].texdata].nameStringTableID;
vector_t reflect = texture_data[texture_info[faces[j].texinfo].texdata].reflectivity;
char* texture_name = &texture_string_data[texture_string_table[texture_data[texture_info[faces[j].texinfo].texdata].nameStringTableID]];
//printf("ch %s %i\n",texture_name,texture_info[faces[j].texinfo].flags);
float tex_r;
float tex_g;
float tex_b;
if (sky) {
tex_r = sky_r;
tex_g = sky_g;
tex_b = sky_b;
} else {
// Lowercase texture_name
int k = 0;
while (texture_name[k]) {
texture_name[k] = tolower(texture_name[k]);
k++;
}
if (strcmp(texture_name, "tools/toolstrigger") == 0)
continue;
tex_r = sqrt(reflect.x);
tex_g = sqrt(reflect.y);
tex_b = sqrt(reflect.z);
}
gl_vert_t vert;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
if (faces[j].dispinfo != -1) {
vector_t low_base = displacements[faces[j].dispinfo].startPosition;
if (faces[j].numedges!=4) {
printf("Bad displacement!\n");
return 1;
}
vector_t corner_verts[4];
int base_i = -1;
float base_dist = INFINITY;
for (int k=0;k<4;k++) {
int edge_i = edge_index[faces[j].firstedge+k];
int vert_i;
if (edge_i<0)
vert_i = edges[-edge_i].v[1];
else
vert_i = edges[edge_i].v[0];
corner_verts[k] = verts[vert_i];
float this_dist = std::abs(verts[vert_i].x - low_base.x) + std::abs(verts[vert_i].y - low_base.y) + std::abs(verts[vert_i].z - low_base.z);
if (this_dist < base_dist) {
base_dist = this_dist;
base_i = k;
}
}
if (base_i==-1) {
printf("Bad base in displacement!\n");
return 1;
}
vector_t high_base = corner_verts[ (base_i+3)%4 ];
vector_t high_ray = corner_verts[ (base_i+2)%4 ] - high_base;
vector_t low_ray = corner_verts[ (base_i+1)%4 ] - low_base;
int verts_wide = (2<<(displacements[faces[j].dispinfo].power-1)) + 1;
vector_t base_verts[289];
float base_alphas[289];
int base_dispvert_index = displacements[faces[j].dispinfo].DispVertStart;
for (int y = 0; y< verts_wide; y++) {
float fy = y / (verts_wide-(float)1);
vector_t mid_base = low_base + low_ray * fy;
vector_t mid_ray = high_base + high_ray * fy - mid_base;
for (int x = 0; x< verts_wide; x++) {
float fx = x / (verts_wide-(float)1);
int i = x+y*verts_wide;
vector_t offset = disp_verts[base_dispvert_index+i].pos;
float scale = disp_verts[base_dispvert_index+i].distance;
float alpha = disp_verts[base_dispvert_index+i].alpha / 255;
base_verts[i] = mid_base + mid_ray*fx + offset*scale;
base_alphas[i] = alpha;
}
}
for (int y = 0; y< verts_wide-1; y++) {
for (int x = 0; x< verts_wide-1; x++) {
int i = x+y*verts_wide;
vector_t v1 = base_verts[i];
vector_t v2 = base_verts[i+1];
vector_t v3 = base_verts[i+verts_wide];
vector_t v4 = base_verts[i+verts_wide+1];
int color1 = pick_color(texture_name, base_alphas[i]);
float tex_r1 = ((color1 >> 16) & 255) / 255.0f;;
float tex_g1 = ((color1 >> 8) & 255) / 255.0f;
float tex_b1 = (color1 & 255) / 255.0f;
int color2 = pick_color(texture_name, base_alphas[i+1]);
float tex_r2 = ((color2 >> 16) & 255) / 255.0f;;
float tex_g2 = ((color2 >> 8) & 255) / 255.0f;
float tex_b2 = (color2 & 255) / 255.0f;
int color3 = pick_color(texture_name, base_alphas[i+verts_wide]);
float tex_r3 = ((color3 >> 16) & 255) / 255.0f;;
float tex_g3 = ((color3 >> 8) & 255) / 255.0f;
float tex_b3= (color3 & 255) / 255.0f;
int color4 = pick_color(texture_name, base_alphas[i+verts_wide+1]);
float tex_r4 = ((color4 >> 16) & 255) / 255.0f;;
float tex_g4 = ((color4 >> 8) & 255) / 255.0f;
float tex_b4 = (color4 & 255) / 255.0f;
if (i%2) {
vert.normal = findNormal(v1,v3,v2);
vert.pos = v1;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v3;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v2;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.normal = findNormal(v2,v3,v4);
vert.pos = v2;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v3;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v4;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
} else {
vert.normal = findNormal(v1,v3,v4);
vert.pos = v1;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v3;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v4;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.normal = findNormal(v1,v4,v2);
vert.pos = v2;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v1;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
vert.pos = v4;
vert.r=tex_r;
vert.g=tex_g;
vert.b=tex_b;
mesh.push_back(vert);
}
}
}
} else {
if (sky) {
vert.normal.x = 0;
vert.normal.y = 0;
vert.normal.z = 1;
} else
vert.normal = planes[faces[j].planenum].normal;
int triangle_vert = 0;
vector_t triangle[3];
for (int v=faces[j].firstedge;v<faces[j].firstedge+faces[j].numedges;v++) {
int edge_i = edge_index[v]; //should be #1->#2->base (todo account for reverse)
int vert_i;
if (edge_i<0)
vert_i = edges[-edge_i].v[1];
else
vert_i = edges[edge_i].v[0];
triangle[triangle_vert++] = verts[vert_i] + model_offsets[i];
if (triangle_vert>2) {
vert.pos=triangle[0];
mesh.push_back(vert);
vert.pos=triangle[1];
mesh.push_back(vert);
vert.pos=triangle[2];
mesh.push_back(vert);
triangle[1] = triangle[2];
triangle_vert=2;
}
}
}
}
}
vert_count_opaque = mesh_opaque.size();
vert_count_trans = mesh_trans.size();
vert_count_sky = mesh_sky.size();
if (vert_count_opaque) {
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_opaque);
glBufferData(GL_ARRAY_BUFFER, vert_count_opaque*sizeof(gl_vert_t), mesh_opaque.data(), GL_STATIC_DRAW);
}
if (vert_count_trans) {
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_trans);
glBufferData(GL_ARRAY_BUFFER, vert_count_trans*sizeof(gl_vert_t), mesh_trans.data(), GL_STATIC_DRAW);
}
if (vert_count_sky) {
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_sky);
glBufferData(GL_ARRAY_BUFFER, vert_count_sky*sizeof(gl_vert_t), mesh_sky.data(), GL_STATIC_DRAW);
}
return 0;
}
bool renderer_active = false;
GLuint h_matrix;
GLuint h_ambient;
GLuint h_light;
GLuint h_light_vector;
bool was_clicked = false;
int old_cx = 0;
int old_cy = 0;
void doFrame() {
int w, h;
glfwGetWindowSize(&w, &h);
h = h > 0 ? h : 1;
glViewport(0, 0, w, h);
double aspect = w/static_cast<double>(h);
glm::vec3 fwd = glm::rotateZ(glm::rotateY(glm::vec3(1,0,0),cam_pitch),cam_yaw);
glm::vec3 left = glm::rotateZ(glm::vec3(0,1,0),cam_yaw);
if (glfwGetMouseButton(0)) {
int cx,cy;
glfwGetMousePos(&cx,&cy);
if (was_clicked) {
double dx = (cx-old_cx) / static_cast<double>(w);
double dy = (cy-old_cy) / static_cast<double>(h);
cam_yaw -= dx*CAM_ROT_SPEED*aspect;
cam_pitch += dy*CAM_ROT_SPEED;
if (cam_pitch<-1.57) cam_pitch = -1.57;
if (cam_pitch>1.57) cam_pitch = 1.57;
float speed = CAM_MOVE_SPEED;
if (glfwGetKey(70)) {
speed = CAM_FAST_SPEED;
}
if (glfwGetKey('W')) {
cam_pos+=fwd*speed;
} else if (glfwGetKey('S')) {
cam_pos-=fwd*speed;
}
if (glfwGetKey('A')) {
cam_pos+=left*speed;
} else if (glfwGetKey('D')) {
cam_pos-=left*speed;
}
} else {
was_clicked = true;
}
old_cx = cx;
old_cy = cy;
} else if (was_clicked) {
was_clicked=false;
}
glColorMask(1,1,1,1);
glClearColor(.2,.2,.2,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColorMask(1,1,1,0);
double fov = 75; // this appears to be source's default FOV
glm::mat4 matrix = glm::perspective(fov*PI_OVER_180,aspect,1.0,1000000.0);
matrix*= glm::lookAt(cam_pos, cam_pos+fwd,glm::vec3(0,0,1));
glUniformMatrix4fv(h_matrix,1,GL_FALSE,reinterpret_cast<float*>(&matrix));
glUniform4f(h_ambient,ambient_r,ambient_g,ambient_b,1);
glUniform4f(h_light,light_r,light_g,light_b,1);
glUniform3f(h_light_vector,light_x,light_y,light_z);
int passes = skybox_active ? 2 : 1;
for (int pass=1;pass<=passes;pass++) {
if (vert_count_opaque) {
glDisable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_opaque);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, pos)));
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, normal)));
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, r)));
glDrawArrays(GL_TRIANGLES,0,vert_count_opaque);
}
if (vert_count_sky) {
glDisable(GL_BLEND);
if (pass != passes) {
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
}
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_sky);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, pos)));
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, normal)));
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, r)));
glDrawArrays(GL_TRIANGLES,0,vert_count_sky);
if (pass != passes)
glDisable(GL_STENCIL_TEST);
}
if (vert_count_trans) {
glEnable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, h_vertexbuffer_trans);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, pos)));
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, normal)));
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(gl_vert_t),reinterpret_cast<void*>(offsetof(gl_vert_t, r)));
glDrawArrays(GL_TRIANGLES,0,vert_count_trans);
if (pass != passes) {
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glClear(GL_DEPTH_BUFFER_BIT);
} else if (pass==2) {
glDisable(GL_STENCIL_TEST);
}
}
if (pass != passes) {
matrix = glm::scale(matrix, glm::vec3(skybox_scale,skybox_scale,skybox_scale));
matrix = glm::translate(matrix, -skybox_pos);
glUniformMatrix4fv(h_matrix,1,GL_FALSE,reinterpret_cast<float*>(&matrix));
}
}
glfwSwapBuffers();
}
extern "C"
const char* initRenderer(int w, int h) {
if (renderer_active) {
return "Renderer is already running!.";
}
if (glfwInit() != GL_TRUE) {
glfwTerminate();
return "OpenGL failed to init.";
}
if (glfwOpenWindow(w, h, 8, 8, 8, 8, 16, 8, GLFW_WINDOW) != GL_TRUE) {
glfwTerminate();
return "OpenGL failed to open viewport.";
}
// Turn on things that should be on by default.
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Shaders
const char* vertex_shader = R"(
precision mediump float;
uniform mat4 matrix;
uniform vec4 ambient;
uniform vec4 light;
uniform vec3 light_vector;
attribute vec4 vPos;
attribute vec3 vNorm;
attribute vec4 baseColor;
varying vec4 color;
void main()
{
float f = max(dot(vNorm,light_vector),.0);
color = baseColor*(ambient+light*f);
gl_Position = matrix*vPos;
})";
const char* fragment_shader = R"(
precision mediump float;
varying vec4 color;
void main () {
gl_FragColor = color;
gl_FragColor.w = .5;
})";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
GLuint prog = glCreateProgram();
glAttachShader(prog, fs);
glAttachShader(prog, vs);
glLinkProgram(prog);
glUseProgram(prog);
h_matrix = glGetUniformLocation(prog, "matrix");
h_ambient = glGetUniformLocation(prog, "ambient");
h_light = glGetUniformLocation(prog, "light");
h_light_vector = glGetUniformLocation(prog, "light_vector");
// Start Loop
emscripten_set_main_loop(doFrame,0,0);
renderer_active = true;
glGenBuffers(1, &h_vertexbuffer_opaque);
glGenBuffers(1, &h_vertexbuffer_trans);
glGenBuffers(1, &h_vertexbuffer_sky);
return 0;
}