-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelmanager.c
More file actions
1317 lines (1116 loc) · 41.9 KB
/
modelmanager.c
File metadata and controls
1317 lines (1116 loc) · 41.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 <GL/glew.h> //may be unneeded
#include <GL/gl.h>
#include <sys/stat.h> //todo filesys
#include <math.h>
#include "globaldefs.h"
#include "hashtables.h"
#include "texturemanager.h"
#include "vbomanager.h"
#include "matrixlib.h"
#include "modelmanager.h"
#include "shadermanager.h"
#include "console.h"
#include "mathlib.h"
#include "iqm.h"
#include "animmanager.h"
#include "glstates.h"
#include "glmanager.h"
int model_count = 0;
int model_arrayFirstOpen = 0;
int model_arrayLastTaken = -1;
int model_arraySize = 0;
int model_ok = 0;
model_t *model_list;
hashbucket_t modelhashtable[MAXHASHBUCKETS];
//hashbucket_t * modelhashtable;
//model_t * defaultModel;
//TODO change the numstatic and whatnot to do the null string trick that dp uses
#define NUMSTATIC 1
#define NUMANIM 1
char *statictypes[] = {".obj"}; //todo filesys
char *animtypes[] = {".iqm"}; //todo filesys //todo
/*
int model_makeAnyModel(const char * name, GLfloat * verts, GLuint * faces, GLuint numfaces, GLuint numverts){ //todo have a way for different attirbutes and whatnot
model_t m;
m.type = 1;
m.name = malloc(strlen("name")+1);
strcpy(m.name, name);
vbo_t * myvbo = createAndAddVBORPOINT(m.name, 1);
if(!myvbo) return FALSE; // todo free and error handle
m.vbo = myvbo->myid;
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, numverts * 3 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
myvbo->numverts = numverts;
// m.interleaveddata = points;
glEnableVertexAttribArray(POSATTRIBLOC);
glVertexAttribPointer(POSATTRIBLOC, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), 0);
// setUpVBOStride(myvbo, 3, 3, 2, 0);
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numfaces * 3 *sizeof(GLuint), faces, GL_STATIC_DRAW);
myvbo->numfaces = numfaces;
// m.tris = tris;
return model_addRINT(m);
}
*/
int makeCubeModel(void){
model_t m;// = malloc(sizeof(model_T));
// memset(m, 0, sizeof(model_t));
// console_printf("generating cube\n");
m.type = 1;
m.name = malloc(strlen("cube")+1);
strcpy(m.name, "cube");
GLfloat * points = malloc(sizeof(GLfloat)*64);
int i;
for(i = 0; i < 3; i++){
m.bbox[i*2] = 1.0;
m.bbox[(i*2)+1] = -1.0;
}
for(i = 0; i < 8; i++){
points[(i*8)+0] = ((i&1)<<1)-1.0;
points[(i*8)+1] = (i&2) -1.0;
points[(i*8)+2] = ((i&4)>>1)-1.0;
m.bboxp[(i*3)+0] = ((i&1)<<1)-1.0;
m.bboxp[(i*3)+1] = (i&2) -1.0;
m.bboxp[(i*3)+2] = ((i&4)>>1)-1.0;
points[(i*8)+3] = points[(i*8)+0] * 0.57735;
points[(i*8)+4] = points[(i*8)+1] * 0.57735;
points[(i*8)+5] = points[(i*8)+2] * 0.57735;
points[(i*8)+6] = ((i&1)<<1)-1.0;
points[(i*8)+7] = (i&2) -1.0;
}
m.spheresize = 1.7320508075688772935274463415058723669428052538103806;
// getBBoxFromInterleavedMesh(points, 8, 8, m.bbox);
// getBBoxpFromBBox(m.bbox, m.bboxp);
GLuint tris[36] = {
0, 2, 3,
0, 3, 1,
4, 5, 7,
4, 7, 6,
2, 0, 4,
2, 4, 6,
0, 1, 5,
0, 5, 4,
1, 3, 7,
1, 7, 5,
3, 2, 6,
3, 6, 7
};
CHECKGLERROR
vbo_t * myvbo = createAndAddVBORPOINT(m.name, 1);
if(!myvbo) return FALSE; // todo free and error handle
m.vbo = myvbo->myid;
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, 8 * 8 * sizeof(GLfloat), points, GL_STATIC_DRAW);
myvbo->numverts = 8;
// m.interleaveddata = points;
glEnableVertexAttribArray(POSATTRIBLOC);
glVertexAttribPointer(POSATTRIBLOC, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
glEnableVertexAttribArray(NORMATTRIBLOC);
glVertexAttribPointer(NORMATTRIBLOC, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(TCATTRIBLOC);
glVertexAttribPointer(TCATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(6*sizeof(GLfloat)));
// setUpVBOStride(myvbo, 3, 3, 2, 0);
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 12 * 3 *sizeof(GLuint), tris, GL_STATIC_DRAW);
myvbo->numfaces = 12;
// m.tris = tris;
// m.numfaces = 12;
// m.numverts = 8;
return model_addRINT(m);
}
int makeFSQuadModel(void){
model_t m;// = malloc(sizeof(model_T));
// memset(m, 0, sizeof(model_t));
// console_printf("generating cube\n");
m.type = 1;
m.name = malloc(strlen("fsquad")+1);
strcpy(m.name, "fsquad");
GLfloat points[16] = { -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0 };
GLuint tris[6] = {0, 1, 2, 0, 2, 3};
vbo_t * myvbo = createAndAddVBORPOINT(m.name, 1);
if(!myvbo) return FALSE; // todo free and error handle
m.vbo = myvbo->myid;
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), points, GL_STATIC_DRAW);
myvbo->numverts = 4;
// m.interleaveddata = points;
glEnableVertexAttribArray(POSATTRIBLOC);
glVertexAttribPointer(POSATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
glEnableVertexAttribArray(TCATTRIBLOC);
glVertexAttribPointer(TCATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
// setUpVBOStride(myvbo, 3, 3, 2, 0);
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 *sizeof(GLuint), tris, GL_STATIC_DRAW);
myvbo->numfaces = 2;
return model_addRINT(m);
}
int makeQSQuadTRModel(void){
model_t m;// = malloc(sizeof(model_T));
// memset(m, 0, sizeof(model_t));
// console_printf("generating cube\n");
m.type = 1;
m.name = malloc(strlen("qsquadtr")+1);
strcpy(m.name, "qsquadtr");
GLfloat points[16] = { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
GLuint tris[6] = {0, 1, 2, 0, 2, 3};
vbo_t * myvbo = createAndAddVBORPOINT(m.name, 1);
if(!myvbo) return FALSE; // todo free and error handle
m.vbo = myvbo->myid;
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), points, GL_STATIC_DRAW);
myvbo->numverts = 4;
// m.interleaveddata = points;
glEnableVertexAttribArray(POSATTRIBLOC);
glVertexAttribPointer(POSATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
glEnableVertexAttribArray(TCATTRIBLOC);
glVertexAttribPointer(TCATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
// setUpVBOStride(myvbo, 3, 3, 2, 0);
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 *sizeof(GLuint), tris, GL_STATIC_DRAW);
myvbo->numfaces = 2;
return model_addRINT(m);
}
int makeCubeModel2(void){
model_t m;// = malloc(sizeof(model_T));
// memset(m, 0, sizeof(model_t));
// console_printf("generating cube\n");
m.type = 1;
m.name = malloc(strlen("cube2")+1);
strcpy(m.name, "cube2");
GLfloat points[192] = {
//position normals texcoords
-1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0,
1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0,
1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0,
-1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
-1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 1.0, -1.0, 0.0, 0.0, 1.0, 0.0,
-1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0,
-1.0, -1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0,
1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0,
-1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0,
1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 1.0, 1.0,
1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0,
-1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 0.0,
-1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0,
1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
-1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0 };
int i;
for(i = 0; i < 3; i++){
m.bbox[i*2] = 1.0;
m.bbox[(i*2)+1] = -1.0;
}
for(i = 0; i < 8; i++){
m.bboxp[(i*3)+0] = ((i&1)<<1)-1.0;
m.bboxp[(i*3)+1] = (i&2) -1.0;
m.bboxp[(i*3)+2] = ((i&4)>>1)-1.0;
}
m.spheresize = 1.7320508075688772935274463415058723669428052538103806;
// getBBoxFromInterleavedMesh(points, 8, 8, m.bbox);
// getBBoxpFromBBox(m.bbox, m.bboxp);
GLuint tris[36] = {
// 0, 1, 2,
// 2, 3, 0,
0, 2, 1,
2, 0, 3,
4, 5, 6,
6, 7, 4,
// 4, 6, 5,
// 6, 4, 7,
8, 9, 10,
10,11, 8,
// 8, 10, 9,
// 10,8, 11,
// 12, 13, 14,
// 14, 15, 12,
12, 14, 13,
14, 12, 15,
16, 17, 18,
18, 19, 16,
// 16, 18, 17,
// 18, 16, 19,
// 20, 21, 22,
// 22, 23, 20
20, 22, 21,
22, 20, 23
};
vbo_t * myvbo = createAndAddVBORPOINT(m.name, 1);
if(!myvbo) return FALSE; // todo free and error handle
m.vbo = myvbo->myid;
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, 192 * sizeof(GLfloat), points, GL_STATIC_DRAW);
myvbo->numverts = 24;
// m.interleaveddata = points;
glEnableVertexAttribArray(POSATTRIBLOC);
glVertexAttribPointer(POSATTRIBLOC, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
glEnableVertexAttribArray(NORMATTRIBLOC);
glVertexAttribPointer(NORMATTRIBLOC, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(TCATTRIBLOC);
glVertexAttribPointer(TCATTRIBLOC, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(6*sizeof(GLfloat)));
// setUpVBOStride(myvbo, 3, 3, 2, 0);
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 12 * 3 *sizeof(GLuint), tris, GL_STATIC_DRAW);
myvbo->numfaces = 12;
// m.tris = tris;
// m.numfaces = 12;
// m.numverts = 24;
//m.stride = 8; //todo
return model_addRINT(m);
}
int model_init(void){
// modelhashtable = malloc(MAXHASHBUCKETS * 2* sizeof(hashbucket_t));
memset(modelhashtable, 0, MAXHASHBUCKETS *sizeof(hashbucket_t));
// model_t none = {"default", findTextureGroupByName("default"), 0, 0};
if(model_list) free(model_list);
model_list = 0;
// model_list = malloc(modelnumber * sizeof(model_t));
// if(!model_list) memset(model_list, 0 , modelnumber * sizeof(model_t));
// model_addRINT("default");
// defaultModel = addModelToList(none);
makeCubeModel(); //todo check these
makeCubeModel2();
makeFSQuadModel();
makeQSQuadTRModel();
model_ok = TRUE;
return TRUE;
}
model_t * model_findByNameRPOINT(const char * name){
return model_returnById(findByNameRINT(name, modelhashtable));
}
int model_findByNameRINT(const char * name){ //todo global
return findByNameRINT(name, modelhashtable);
}
int model_delete(const int id){
int modelindex = (id & 0xFFFF);
model_t * m = &model_list[modelindex];
if(m->myid != id) return FALSE;
if(!m->name) return FALSE;
deleteFromHashTable(m->name, id, modelhashtable);
free(m->name);
if(m->interleaveddata) free(m->interleaveddata);
vbo_t * v = returnVBOById(m->vbo);
if(v) deleteVBO(v->myid);
memset(m, 0, sizeof(model_t));
if(modelindex < model_arrayFirstOpen) model_arrayFirstOpen = modelindex;
for(; model_arrayLastTaken > 0 && !model_list[model_arrayLastTaken].type; model_arrayLastTaken--);
return TRUE;
}
int model_deleteAll(void){
int i, count = 0;
for(i = 0; i < model_arrayLastTaken; i++){
model_t *m = &model_list[i];
count += model_delete(m->myid);
}
memset(modelhashtable, 0, MAXHASHBUCKETS *sizeof(hashbucket_t));
// model_t none = {"default", findTextureGroupByName("default"), 0, 0};
if(model_list) free(model_list);
model_list = 0;
model_count = 0;
model_arrayFirstOpen = 0;
model_arrayLastTaken = -1;
model_arraySize = 0;
return count;
}
model_t * model_returnById(const int id){
int modelindex = (id & 0xFFFF);
model_t * mod = &model_list[modelindex];
if(!mod->type) return FALSE;
if(mod->myid == id) return mod;
return FALSE;
}
int generateNormalsFromMesh(GLfloat * vertbuffer, GLfloat * normbuffer, GLuint * indices, GLuint indicecount, GLuint vertcount, char type){
//used http://devmaster.net/posts/6065/calculating-normals-of-a-mesh and darkplaces model_shared.c as a ref
int i;
for(i = 0; i < indicecount; i += 3 ){
float *p1 = &vertbuffer[indices[i+0]*3];
float *p2 = &vertbuffer[indices[i+1]*3];
float *p3 = &vertbuffer[indices[i+2]*3];
float v1[3];
float v2[3];
int n;
for(n = 0; n < 3; n++){
v1[n] = p2[n] - p1[n];
v2[n] = p3[n] - p1[n];
}
float normal[3];
normal[0] = v1[1] * v2[2] - v2[1] * v1[2];
normal[1] = v1[2] * v2[0] - v2[2] * v1[0];
normal[2] = v1[0] * v2[1] - v2[0] * v1[1];
//if dontwant to do area weighting, normalize now
if(!type){
float length = vec3length(normal);
normal[0] /= length;
normal[1] /= length;
normal[2] /= length;
}
float *n1 = &normbuffer[indices[i+0]*3];
float *n2 = &normbuffer[indices[i+1]*3];
float *n3 = &normbuffer[indices[i+2]*3];
for(n = 0; n < 3; n++){
n1[n] += normal[n];
n2[n] += normal[n];
n3[n] += normal[n];
}
}
//go through and renormalize
if(type <2){
for(i = 0; i < vertcount; i++){
float * normal = &normbuffer[i*3];
float length = vec3length(normal);;
// length = sqrt((normal[0] * normal[0]) + (normal[1] * normal[1]) + (normal[2] * normal[2]));
normal[0] /= length;
normal[1] /= length;
normal[2] /= length;
}
}
return TRUE;
}
float getSphereFromInterleavedMesh(GLfloat * interleavedbuffer, GLuint vertcount, int stride){
if(stride < 5) return 0;
float size = 0.0;
int i;
for(i = 0; i < vertcount; i++){
float * vert = &interleavedbuffer[(i*stride)];
float mysize = vec3length(vert);
if(mysize > size) size = mysize;
}
// console_printf("spheresize = %f\n", size);
return size;
}
int getBBoxFromInterleavedMesh(GLfloat * interleavedbuffer, GLuint vertcount, int stride, vec_t * bbox){
if(stride < 5) return 0;
bbox[0] = interleavedbuffer[0];
bbox[1] = interleavedbuffer[0];
bbox[2] = interleavedbuffer[1];
bbox[3] = interleavedbuffer[1];
bbox[4] = interleavedbuffer[2];
bbox[5] = interleavedbuffer[2];
int i;
for(i = 1; i < vertcount; i++){
float * vert = &interleavedbuffer[(i*stride)];
if(vert[0] > bbox[0]) bbox[0] = vert[0];
else if(vert[0] < bbox[1]) bbox[1] = vert[0];
if(vert[1] > bbox[2]) bbox[2] = vert[1];
else if(vert[1] < bbox[3]) bbox[3] = vert[1];
if(vert[2] > bbox[4]) bbox[4] = vert[2];
else if(vert[2] < bbox[5]) bbox[5] = vert[2];
}
/*
for(i = 0; i < 6; i++){
console_printf("bbox %i:%f\n", i, bbox[i]);
}
*/
return TRUE;
}
int normalizeNormalsFromInterleavedMesh(GLfloat * interleavedbuffer, GLuint vertcount, int stride){
if(stride < 5) return FALSE;
int i;
for(i = 0; i < vertcount; i++){
//todo set up something to have a varialbe offset
float * normal = &interleavedbuffer[(i*stride)+3];
float length = vec3length(normal);
vec3div(normal, normal, length);
}
return TRUE;
}
int generateNormalsFromInterleavedMesh(GLfloat * interleavedbuffer, GLuint * indices, GLuint indicecount, GLuint vertcount, int stride, char type){
//used http://devmaster.net/posts/6065/calculating-normals-of-a-mesh and darkplaces model_shared.c as a ref
if(stride < 5) return FALSE;
int i;
for(i = 0; i < indicecount; i += 3 ){
//todo set up something to have a varialbe offset
float *p1 = &interleavedbuffer[indices[i+0]*stride];
float *p2 = &interleavedbuffer[indices[i+1]*stride];
float *p3 = &interleavedbuffer[indices[i+2]*stride];
float v1[3];
float v2[3];
int j;
for(j = 0; j < 3; j++){
v1[j] = p2[j] - p1[j];
v2[j] = p3[j] - p1[j];
}
float n[3];
n[0] = v1[1] * v2[2] - v2[1] * v1[2];
n[1] = v1[2] * v2[0] - v2[2] * v1[0];
n[2] = v1[0] * v2[1] - v2[0] * v1[1];
//if dontwant to do area weighting, normalize now
if(!type){ // type 0, no area weighting
float length = vec3length(n);
vec3div(n, n, length);
}
//todo set up something to have a varialbe offset
float *n1 = p1 + 3;
float *n2 = p2 + 3;
float *n3 = p3 + 3;
for(j = 0; j < 3; j++){
n1[j] += n[j];
n2[j] += n[j];
n3[j] += n[j];
}
}
//go through and renormalize
if(type < 2) normalizeNormalsFromInterleavedMesh(interleavedbuffer, vertcount, stride);
return TRUE;
}
//todo maybe instead of sampling from indices, sample from newindices
//if i dont, it may cause some "resetting" issues.
GLuint * meshDecimate(GLfloat * interleavedbuffer, GLuint * indices, GLuint indicecount, GLuint vertcount, int stride, float cutdistance, GLuint * returncount){
if(stride < 5) return FALSE;
GLuint * newindices = malloc(sizeof(GLuint)*indicecount);
memcpy(newindices, indices, sizeof(GLuint)*indicecount);
int i;
for(i = 0; i < indicecount; i += 3 ){
int m;
for(m = 0; m<3; m++){
int first = i+m;
int second = i + ((m+1) % 3);
//todo set up something to have a varialbe offset
float *p1 = &interleavedbuffer[(indices[first]*stride)+3];
float *p2 = &interleavedbuffer[(indices[second]*stride)+3];
float diff[3];
diff[0] = p1[0]-p2[0];
diff[1] = p1[1]-p2[1];
diff[2] = p1[2]-p2[2];
float dist = (diff[0] *diff[0]) + (diff[1] * diff[1]) + (diff[2] * diff[2]);
if(dist > cutdistance){
float l1 = (p1[0] * p1[0]) + (p1[1] * p1[1]) + (p1[2] * p1[2]);
float l2 = (p2[0] * p2[0]) + (p2[1] * p2[1]) + (p2[2] * p2[2]);
int replace;
int test;
if(l1>l2){
replace = indices[first];
test = indices[second];
} else {
replace = indices[second];
test = indices[first];
}
int k;
for(k = 0; k < indicecount; k++){
//loop through all triangles, make any verts be other verts
if(indices[k] == test) newindices[k] = replace;
}
}
}
}
indices = realloc(indices, sizeof(GLuint)*indicecount*2);
//loop through all triangles, make sure the triangles are actually full
int count = 0;
for(i = 0; i < indicecount; i += 3 ){
if(newindices[i] != newindices[i+1] && newindices[i+1] != newindices[i+2] && newindices[i+2] != newindices[i]){
indices[count+indicecount] = newindices[i];
indices[count+1+indicecount] = newindices[i+1];
indices[count+2+indicecount] = newindices[i+2];
count+=3;
}
}
free(newindices);
indices = realloc(indices, (count+indicecount) * sizeof(GLuint));
*returncount = (count + indicecount)/3;
return indices;
}
int loadiqmmeshes(model_t * m, const struct iqmheader hdr, unsigned char *buf){
// if(meshdata) return false;
/*
lilswap((uint *)&buf[hdr.ofs_vertexarrays], hdr.num_vertexarrays*sizeof(iqmvertexarray)/sizeof(uint));
lilswap((uint *)&buf[hdr.ofs_triangles], hdr.num_triangles*sizeof(iqmtriangle)/sizeof(uint));
lilswap((uint *)&buf[hdr.ofs_meshes], hdr.num_meshes*sizeof(iqmmesh)/sizeof(uint));
lilswap((uint *)&buf[hdr.ofs_joints], hdr.num_joints*sizeof(iqmjoint)/sizeof(uint));
*/
// if(hdr.ofs_adjacency) lilswap((uint *)&buf[hdr.ofs_adjacency], hdr.num_triangles*sizeof(iqmtriangle)/sizeof(uint));
// meshdata = buf;
// int nummeshes = hdr.num_meshes; //todo have multiple meshes support
int numtris = hdr.num_triangles;
int numverts = hdr.num_vertexes;
float *pos = 0;
float *norm = 0;
float *texcoord = 0;
float *tangent = 0;
unsigned int *blendindex = 0;
unsigned int *blendweight = 0;
int i;
// const char *str = hdr.ofs_text ? (char *)&buf[hdr.ofs_text] : "";
struct iqmvertexarray *vas = (struct iqmvertexarray *)&buf[hdr.ofs_vertexarrays];
for(i = 0; i < (int)hdr.num_vertexarrays; i++){
struct iqmvertexarray va = vas[i];
switch(va.type){
case IQM_POSITION: if(va.format != IQM_FLOAT || va.size == 3) pos = (float *)&buf[va.offset]; break;
case IQM_NORMAL: if(va.format != IQM_FLOAT || va.size == 3) norm = (float *)&buf[va.offset]; break;
case IQM_TANGENT: if(va.format != IQM_FLOAT || va.size == 4) tangent = (float *)&buf[va.offset];
case IQM_TEXCOORD: if(va.format != IQM_FLOAT || va.size == 2) texcoord = (float *)&buf[va.offset]; break;
case IQM_BLENDINDEXES: if(va.format != IQM_UBYTE || va.size == 4) blendindex = (unsigned int *)&buf[va.offset]; break;
case IQM_BLENDWEIGHTS: if(va.format != IQM_UBYTE || va.size == 4) blendweight = (unsigned int *)&buf[va.offset]; break;
//case IQM_COLOR: if(va.format != IQM_UBYTE || va.size != 4) return FALSE; incolor = (uchar *)&buf[va.offset]; break;
default: break;
}
}
if(!pos) return FALSE;
vbo_t * myvbo = createAndAddVBORPOINT(m->name, 1);
if(!myvbo) return 0; // todo free and error handle
m->vbo = myvbo->myid;
GLuint stride = 0;
char poss =0, norms = 0, tcs = 0, tangents = 0, blendi = 0, blendw = 0;
if(pos) stride += (poss = 3);
if(norm) stride += (norms = 3);
if(texcoord) stride += (tcs = 2);
if(tangent) stride += (tangents = 4);
//todo are these really only 1?
if(blendindex && blendweight){ stride += 2; blendi = 1; blendw = 1;}
GLfloat * interleavedbuffer = malloc(stride*numverts*sizeof(GLfloat));
memset(interleavedbuffer, 0 , stride*numverts*sizeof(GLfloat));
for(i = 0; i < numverts; i++){
unsigned int stridem = i*stride;
interleavedbuffer[stridem+0] = pos[(i*3)+0];
interleavedbuffer[stridem+1] = pos[(i*3)+1];
interleavedbuffer[stridem+2] = pos[(i*3)+2];
if(norm){
interleavedbuffer[stridem+3] = norm[(i*3)+0];
interleavedbuffer[stridem+4] = norm[(i*3)+1];
interleavedbuffer[stridem+5] = norm[(i*3)+2];
}
if(texcoord){
interleavedbuffer[stridem+6] = texcoord[(i*2)+0];
interleavedbuffer[stridem+7] = texcoord[(i*2)+1];
}
if(tangent){
interleavedbuffer[stridem+8] = tangent[(i*4)+0];
interleavedbuffer[stridem+9] = tangent[(i*4)+1];
interleavedbuffer[stridem+10] = tangent[(i*4)+2];
interleavedbuffer[stridem+11] = tangent[(i*4)+3];
}
if(blendi && blendw){
interleavedbuffer[stridem+12] = ((GLuint*)blendindex)[i];
interleavedbuffer[stridem+13] = ((GLuint*)blendweight)[i];
}
}
m->spheresize = getSphereFromInterleavedMesh(interleavedbuffer, numverts, stride);
// m->numverts = numverts;
getBBoxFromInterleavedMesh(interleavedbuffer, numverts, stride, m->bbox);
getBBoxPFromBBox(m->bbox, m->bboxp);
states_bindBuffer(GL_ARRAY_BUFFER, myvbo->vboid);
glBufferData(GL_ARRAY_BUFFER, numverts * stride * sizeof(GLfloat), interleavedbuffer, GL_STATIC_DRAW);
myvbo->numverts = numverts;
m->interleaveddata = interleavedbuffer;
// free(interleavedbuffer);
setUpVBOStride(myvbo, poss, norms, tcs, tangents, blendi, blendw);
GLuint *tris = (GLuint *)&buf[hdr.ofs_triangles];
//flipping faces... temp fix
for(i = 0; i < numtris; i++){
GLuint temp = tris[i*3];
tris[i*3] = tris[(i*3)+1];
tris[(i*3)+1] = temp;
}
states_bindBuffer(GL_ELEMENT_ARRAY_BUFFER,myvbo->indicesid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,numtris * 3 *sizeof(GLuint), tris, GL_STATIC_DRAW);
myvbo->numfaces = numtris;
// m->tris = tris;
// m->numfaces = numtris;
//free(tris); // its the buffer
console_printf("Model %s.iqm has %i faces and %i verts with a stride of %i\n", m->name, numtris, numverts, stride);
/*
for(int i = 0; i < (int)hdr.num_meshes; i++){
struct iqmmesh &m = meshes[i];
console_printf("%s: loaded mesh: %s\n", filename, &str[m.name]);
}
*/
return TRUE;
}
int loadiqmjoints(model_t * m, const struct iqmheader hdr, unsigned char *buf){
if(!hdr.num_joints) return FALSE;
struct iqmjoint *joints = (struct iqmjoint*) &buf[hdr.ofs_joints];
// struct iqmjoint *joints = (struct iqmjoint*) (buf+hdr.ofs_joints);
if(!joints) return FALSE;
joint_t * myjoints = malloc(hdr.num_joints * sizeof(joint_t));
float * baseboneposeinverse = malloc(12*sizeof(float) * hdr.num_joints);
int i;
for(i = 0; i < hdr.num_joints; i++){
matrix4x4_t relbase, relinvbase, pinvbase, invbase;
memcpy(&myjoints[i], &joints[i], sizeof(joint_t));
//todo copy parent data and name?
if(myjoints[i].rotate[3] > 0) vec4mult(myjoints[i].rotate, myjoints[i].rotate, -1.0);
vec4norm2(myjoints[i].rotate, myjoints[i].rotate);
Matrix4x4_FromDoom3Joint(&relbase, myjoints[i].translate[0], myjoints[i].translate[1], myjoints[i].translate[2], myjoints[i].rotate[0], myjoints[i].rotate[1], myjoints[i].rotate[2]);
Matrix4x4_Invert_Simple(&relinvbase, &relbase);
if(myjoints[i].parent >=0){
Matrix4x4_FromArray12FloatD3D(&pinvbase, baseboneposeinverse + myjoints[i].parent * 12);
Matrix4x4_Concatsimdu(&invbase, &relinvbase, &pinvbase);
Matrix4x4_ToArray12FloatD3D(&invbase, baseboneposeinverse + 12*i);
Matrix4x4_Copy(&myjoints[i].bbpinverse, &invbase);
} else {
Matrix4x4_ToArray12FloatD3D(&relinvbase, baseboneposeinverse+12*i);
Matrix4x4_Copy(&myjoints[i].bbpinverse, &invbase);
}
}
m->bbpinverse = baseboneposeinverse;
m->numjoints = hdr.num_joints;
m->joints = myjoints;
return hdr.num_joints;
}
void setJointBBox(joint_t *j, float * vert){
vec3_t outvert;
Matrix4x4_Transformsimdu(&j->bbpinverse, vert, outvert);
vec_t *bbox = j->bbox;
if(!j->setbbox){
j->setbbox = TRUE;
bbox[0] = outvert[0];
bbox[1] = outvert[0];
bbox[2] = outvert[1];
bbox[3] = outvert[1];
bbox[4] = outvert[2];
bbox[5] = outvert[2];
} else {
if(outvert[0] > bbox[0]) bbox[0] = outvert[0];
else if(outvert[0] < bbox[1]) bbox[1] = outvert[0];
if(outvert[1] > bbox[2]) bbox[2] = outvert[1];
else if(outvert[1] < bbox[3]) bbox[3] = outvert[1];
if(outvert[2] > bbox[4]) bbox[4] = outvert[2];
else if(outvert[2] < bbox[5]) bbox[5] = outvert[2];
}
}
int loadiqmbboxes(model_t *m){
//flesh this error checking out more
if(!m) return FALSE;
if(!m->interleaveddata) return FALSE;
unsigned int numjoints = m->numjoints;
if(!numjoints) return FALSE;
if(numjoints == 1){
m->joints->setbbox = TRUE;
memcpy(m->joints->bbox, m->bbox, 6*sizeof(vec_t));
memcpy(m->joints->bboxp, m->bboxp, 24*sizeof(vec_t));
return TRUE;
}
vbo_t *v =returnVBOById(m->vbo);
if(!v) return FALSE;
if(v->totalstride != 14){
console_printf("model has the wrong stride for joints!\n");
return FALSE;
}
unsigned int vertcount = v->numverts;
GLfloat * data = m->interleaveddata;
unsigned int i;
unsigned char *ji;
unsigned char *jw;
for(i = 0; i < vertcount; i++){
unsigned int stridem = i*14;
ji = (unsigned char *)(&data[stridem+12]);
jw = (unsigned char *)(&data[stridem+13]);
int k;
for(k = 0; k < 4; k++){
//if the weight is over 0
if(jw[k]){
if(ji[k] > numjoints){ //todo
} else {
setJointBBox(&m->joints[ji[k]], &data[stridem]);
}
}
}
}
for(i = 0; i < numjoints; i++){
getBBoxPFromBBox(m->joints[i].bbox, m->joints[i].bboxp);
}
return numjoints;
}
extern int loadiqmposes(anim_t *a, const struct iqmheader hdr, unsigned char *buf);
extern int loadiqmanimscenes(anim_t *a, const struct iqmheader hdr, unsigned char *buf);
int loadModelIQM(model_t *m, char * filename){
//mostly copied from the sdk
FILE *f = fopen(filename, "rb");
if(!f) return FALSE;
unsigned char *buf = NULL;
struct iqmheader hdr;
if(fread(&hdr, 1, sizeof(hdr), f) != sizeof(hdr) || memcmp(hdr.magic, IQM_MAGIC, sizeof(hdr.magic)))
goto error; //spaghetti!
if(hdr.version != IQM_VERSION)goto error;
if(hdr.filesize > (16<<20)) goto error; // sanity check... don't load files bigger than 16 MB
buf = malloc(hdr.filesize);
if(fread(buf + sizeof(hdr), 1, hdr.filesize - sizeof(hdr), f) != hdr.filesize - sizeof(hdr))
goto error;
if(hdr.num_meshes > 0 && !loadiqmmeshes(m, hdr, buf)) goto error;
if(hdr.num_joints > 0 && !loadiqmjoints(m, hdr, buf)) goto error;
if(hdr.num_poses){
anim_t *a = anim_createAndAddRPOINT(m->name);
//todo actually have these have an error return
if(!loadiqmanimscenes(a, hdr, buf)) goto error;
if(!loadiqmposes(a, hdr, buf)) goto error;
}
// if(!loadiqmbboxes(m)) goto error;
loadiqmbboxes(m);
if(m->interleaveddata) free(m->interleaveddata);
m->interleaveddata = 0;
fclose(f);
free(buf);
return TRUE;
//todo
error:
if(m->interleaveddata) free(m->interleaveddata);
m->interleaveddata = 0;
console_printf("%s: error while loading\n", filename);
free(buf);
// if(buf != meshdata && buf != animdata) free(buf);
fclose(f);
return FALSE;
//todo
}
int loadModelOBJ(model_t * m, char * filename){//todo flags
unsigned int vertcount = 0;
unsigned int tccount = 0;
unsigned int normcount = 0;
unsigned int facecount = 0;
FILE *f;
if(!(f = fopen(filename, "r"))) return FALSE;
char * line = malloc(300*sizeof(char)); //max size of 300;
// char * testline;
int over;
while(fgets(line, 300, f)){
// testline = line;
// while((*testline == ' ' || *testline == '\t') && testline < line + 200) testline+=sizeof(char); //should be 1 //take off early white spaces
// if(testline > 200 + line) continue;
// if(!strncmp(testline, "v ", 2)) vertcount++;
// else if(!strncmp(testline, "vt", 2)) tccount++;
// else if(!strncmp(testline, "vp", 2)) normcount++;
// else if(!strncmp(testline, "f ", 2)) facecount++;
for(over = 0; (line[over] == ' ' || line[over] == '\t') && over < 200; over++); //should be 1 //take off early white spaces
if(!strncmp(line+over, "v ", 2)) vertcount++;
else if(!strncmp(line+over, "vt", 2)) tccount++;
else if(!strncmp(line+over, "vp", 2)) normcount++;
else if(!strncmp(line+over, "f ", 2)) facecount++;
}
// if(vertcount + tccount + normcount != vertcount*3) return FALSE;
if(!vertcount) return FALSE; // no verts
int addcount = 0;
if(normcount>tccount && normcount > vertcount){
addcount = normcount - vertcount;
} else if(tccount > vertcount) {
addcount = tccount - vertcount;
}
float * vertbuffer = malloc(3*vertcount*sizeof(float));
float * tcbuffer = malloc(2*tccount*sizeof(float));
float * normbuffer = malloc(3*normcount*sizeof(float));
//GLuint * facebuffer = malloc(9*facecount*sizeof(GLuint)); //one for verts, tc, and normals
int * facebuffer = malloc(9*sizeof(int)); //one for verts, tc, and normals
GLuint * indicebuffer = malloc(3*facecount*sizeof(GLuint));
GLfloat * interleavedbuffer = malloc(8*facecount*sizeof(GLfloat));
GLfloat * interleavedaddon = malloc(8*addcount*sizeof(GLfloat));
memset(vertbuffer, 0, 3*vertcount*sizeof(float));
memset(normbuffer, 0, 3*normcount*sizeof(float));
memset(tcbuffer, 0, 2* tccount*sizeof(float));
memset(facebuffer, 0, 9*sizeof(int));
memset(indicebuffer, 0, 3*facecount*sizeof(GLuint));
memset(interleavedbuffer, 0, 8*facecount*sizeof(GLfloat));
memset(interleavedaddon, 0, 8*addcount*sizeof(GLfloat));
rewind(f);
unsigned int readvert = 0;
unsigned int readnorm = 0;
unsigned int readtc = 0;
unsigned int readface = 0;
// unsigned int readadd = 0;
while(fgets(line, 300, f)){
for(over = 0; (line[over] == ' ' || line[over] == '\t') && over < 200; over++); //should be 1 //take off early white spaces
// if(!strncmp(testline, "v ", 2)){
if(!strncmp(line+over, "v ", 2)){
if(readvert >= vertcount) break; //todo debug
sscanf(line," v %f %f %f",&vertbuffer[(readvert*3)],&vertbuffer[(readvert*3)+1],&vertbuffer[(readvert*3)+2]);
readvert++;
}
else if(!strncmp(line+over, "vt", 2)){
if(readtc >= tccount) break; //todo debug
sscanf(line," vt %f %f",&tcbuffer[(readtc*2)],&tcbuffer[(readtc*2)+1]);
readtc++;
}
else if(!strncmp(line+over, "vp", 2)){
if(readnorm >= normcount) break; //todo debug
sscanf(line," vp %f %f %f",&normbuffer[(readnorm*3)],&normbuffer[(readnorm*3)+1], &normbuffer[(readnorm*3)+2]);
readnorm++;
}
else if(!strncmp(line+over, "f ", 2)){
if(readface >= facecount) break;
// printf("%i\n",readface);
if(sscanf(line," f %d/%d/%d %d/%d/%d %d/%d/%d",
&facebuffer[0], &facebuffer[1], &facebuffer[2],
&facebuffer[3], &facebuffer[4], &facebuffer[5],
&facebuffer[6], &facebuffer[7], &facebuffer[8]
) == 9);
else if(sscanf(line," f %d/%d %d/%d %d/%d",
&facebuffer[0], &facebuffer[1],
&facebuffer[3], &facebuffer[4],
&facebuffer[6], &facebuffer[7]
) == 6 ) facebuffer[2] = facebuffer[5] = facebuffer[8] = 0; // make sure they are 0s
else if(sscanf(line," f %d//%d %d//%d %d//%d",
&facebuffer[0], &facebuffer[2],