-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.cpp
executable file
·736 lines (613 loc) · 15.7 KB
/
model.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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <gl/gl.h>
#include "model.h"
#include "local.h"
#include "math.h"
#include <crtdbg.h>
#include <math.h>
using namespace std;
/*
==========
mesh_t::Load
==========
*/
int mesh_t::Load( char* filename )
{
fstream file( filename, ios::in );
char str[200];
int i, num;
if( !file.is_open() )
{
cout << "-->ERROR (mesh_t::Load): Cannot open \"" << filename << "\"" << endl;
return 0;
}
// verts
file >> str >> num;
verts.Malloc( num );
for( i=0; i< verts.size; i++ )
{
file >> str >> str >> str >> verts[i].coords.x() >> verts[i].coords.y() >> verts[i].coords.z();
// VERT index COORDS num num num NORMAL num num num
}
// tris
file >> str >> num;
tris.Malloc( num );
for( i=0; i<tris.size; i++ )
{
file >> str >> str >> str >> tris[i].vert_ids[0] >> tris[i].vert_ids[1] >> tris[i].vert_ids[2];
}
// uvs
file >> str >> num;
uvs.Malloc( num );
for( i=0; i<uvs.size; i++ )
{
file >> str >> str >> uvs[i][0].x() >> uvs[i][0].y() >> uvs[i][1].x() >> uvs[i][1].y() >> uvs[i][2].x() >> uvs[i][2].y();
}
// groups
file >> str >> num;
vert_groups.Malloc( num );
for( i=0; i<vert_groups.size; i++ )
file >> str >> str >> str >> vert_groups[i].name;
// weights
file >> str >> num;
weights.Malloc( num );
for( i=0; i<weights.size; i++ )
{
file >> str >> str >> str >> str >> str >> num; // pairs num
weights[i].pairs.Malloc( num );
for( int j=0; j<weights[i].pairs.size; j++ )
file >> str >> weights[i].pairs[j].vert_group_id >> str >> weights[i].pairs[j].weight;
}
CalcAllNormals();
file.close();
return 1;
}
/*
==========
mesh_t::CalcFaceNormals
==========
*/
void mesh_t::CalcFaceNormals()
{
int i;
vec3_t a, b;
triangle_t* tri;
for( i=0; i<tris.size; i++ )
{
tri = &tris[i];
a = verts[ tri->vert_ids[0] ].coords - verts[ tri->vert_ids[1] ].coords;
b = verts[ tri->vert_ids[1] ].coords - verts[ tri->vert_ids[2] ].coords;
tri->normal = a*b;
tri->normal.Normalize();
}
}
/*
==========
mesh_t::CalcVertNormals
==========
*/
void mesh_t::CalcVertNormals()
{
int i, j;
vertex_t* vert;
triangle_t* tri;
int found_num;
// the slow way
if( vert_tris.size == 0 )
{
for( i=0; i<verts.size; i++ )
{
vert = &verts[i];
vert->normal.LoadZero();
found_num = 0;
for( j=0; j<tris.size; j++ )
{
tri = &tris[j];
if( tri->vert_ids[0]==i || tri->vert_ids[1]==i || tri->vert_ids[2]==i )
{
vert->normal = vert->normal + tri->normal;
++found_num;
}
}
vert->normal *= 1.0f/(float)found_num;
}
}
// the fast way
else
{
for( i=0; i<verts.size; i++ )
{
vert = &verts[i];
vert->normal.LoadZero();
for( j=0; j<vert_tris[i].tri_ids.size; j++ ) // for all the tris of this vert
{
vert->normal += tris[ vert_tris[i].tri_ids[j] ].normal;
}
vert->normal *= 1.0f/(float)vert_tris[i].tri_ids.size;
} // end for all verts
}// end else
}
/*
EnableFastNormalCalcs
this is for fast vertex normal calcs but it allocates memory for that
*/
void mesh_t::EnableFastNormalCalcs()
{
int i, j;
int arr [40]; // max 40 tris per vert
int tris_num;
triangle_t* tri;
// alloc the main array
vert_tris.Malloc( verts.size );
for( i=0; i<verts.size; i++ ) // for all verts
{
tris_num = 0;
for( j=0; j<tris.size; j++ ) // for all tris
{
tri = &tris[j];
if( tri->vert_ids[0]==i || tri->vert_ids[1]==i || tri->vert_ids[2]==i ) // if the vert is in this tri...
{
arr[tris_num++] = j; // ...then put it on the arr
}
}
// evaluate the findings for the vert
vert_tris[i].tri_ids.Malloc( tris_num );
for( j=0; j<tris_num; j++ )
vert_tris[i].tri_ids[j] = arr[j];
}
}
/*
==========
DrawMesh
==========
*/
void DrawMesh( mesh_t& mesh, GLuint tex_id )
{
int i, j;
glDisable( GL_BLEND );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, tex_id);
/*glBegin( GL_TRIANGLES );
for( i=0; i<mesh.tris.size; i++ )
{
triangle_t* tri = &mesh.tris[i];
for( j=0; j<3; j++ )
{
vertex_t* vert = &mesh.verts[tri->vert_ids[j]];
glNormal3fv( &vert->normal[0] );
glTexCoord2fv( tri->uvs[j] );
glVertex3fv( vert->coords );
}
}
glEnd();*/
/*glVertexPointer( 3, GL_FLOAT, sizeof(vertex_t), &mesh.verts[0].coords[0] );
glNormalPointer( GL_FLOAT, sizeof(vertex_t), &mesh.verts[0].normal[0] );*/
//glDisable( GL_LIGHTING );
glBegin( GL_TRIANGLES );
for( i=0; i<mesh.tris.size; i++ )
{
triangle_t* tri = &mesh.tris[i];
for( j=0; j<3; j++ )
{
vertex_t* vert = &mesh.verts[tri->vert_ids[j]];
glTexCoord2fv( &mesh.uvs[i][j][0] );
glNormal3fv( &vert->normal[0] );
glVertex3fv( &vert->coords[0] );
//glArrayElement( mesh.tris[i].vert_ids[j] );
}
}
glEnd();
// vert normals
if( 0 )
{
glColor3f( 0.0, 0.0, 1.0 );
glDisable( GL_LIGHTING );
glDisable( GL_TEXTURE_2D );
glBegin( GL_LINES );
for( i=0; i<mesh.tris.size; i++ )
{
triangle_t* tri = &mesh.tris[i];
for( j=0; j<3; j++ )
{
vertex_t* vert = &mesh.verts[tri->vert_ids[j]];
vec3_t vec0;
vec0 = (vert->normal * 0.1f) + vert->coords;
glVertex3fv( &vert->coords[0] );
glVertex3fv( &vec0[0] );
}
}
glEnd();
}
// tri normals
if( 0 )
{
glDisable( GL_LIGHTING );
glDisable( GL_TEXTURE_2D );
glBegin( GL_LINES );
for( i=0; i<mesh.tris.size; i++ )
{
triangle_t* tri = &mesh.tris[i];
vec3_t vec1;
vec1 = mesh.verts[ tri->vert_ids[0] ].coords;
vec1 = vec1.VecsMiddle( mesh.verts[ tri->vert_ids[1] ].coords );
vec1 = vec1.VecsMiddle( mesh.verts[ tri->vert_ids[2] ].coords );
vec3_t vec2( tri->normal );
vec2 = tri->normal;
vec2 *= 0.09;
vec2 += vec1;
glColor3f( 0.0, 1.0, 0.0 );
glVertex3fv( &vec1[0] );
glColor3f( 0.0, 0.0, 1.0 );
glVertex3fv( &vec2[0] );
}
glEnd();
}
/*glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );
glLineWidth( lw );*/
}
/*
==============
armature_t::Load
==============
*/
int armature_t::Load( char* filename )
{
fstream file( filename, ios::in );
char str[200];
int i, j;
if( !file.is_open() )
{
cout << "-->ERROR (armature_t::Load): Cannot open \"" << filename << "\"" << endl;
return 0;
}
// bones heads and tails
int bones_num;
file >> str >> bones_num;
bones.Malloc( bones_num );
for( i=0; i<bones.size; i++ )
{
bone_t* bone = &bones[i];
// name
file >> str >> str >> str >> &bone->name[0];
// head and tail
file >> str;
for( j=0; j<3; j++ )
file >> bone->head[j];
file >> str;
for( j=0; j<3; j++ )
file >> bone->tail[j];
// parent
int parent;
file >> str >> parent;
bone->parent_id = parent;
// childs
int childs_num;
file >> str >> childs_num >> str;
bone->child_ids.Malloc( childs_num );
for( int j=0; j<bone->child_ids.size; j++ )
{
int child;
file >> child;
bone->child_ids[j] = child;
}
// init the matrix
bone->matrix.LoadIdent();
}
file.close();
return 1;
}
/*
==============
armature_t::Draw
==============
*/
void armature_t::Draw()
{
float lw;
float ps;
vec3_t newhead, newtail;
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
glDisable( GL_TEXTURE_2D );
glGetFloatv( GL_LINE_WIDTH, &lw );
glGetFloatv ( GL_POINT_SIZE, &ps );
glLineWidth( 1.0 );
glPointSize( 4.0 );
for( int i=0; i<bones.size; i++ )
{
newhead = bones[i].matrix * bones[i].head;
newtail = bones[i].matrix * bones[i].tail;
//bone_t* bone = &bones[i];
//vec3_t h, t, H, T, h_, t_;
//mat4_t rot, ma, mb, mai;
//h_.LoadZero();
//t_.LoadZero(); t_.y() = bones[i].length;
//H = bones[i].head_armat;
//T = bones[i].tail_armat;
//ma = bones[i].matrix_armat;
//mai = ma; mai.Invert();
//mb.LoadMat3( bones[i].matrix_bone );
//h = bones[i].head_bone;
//t = bones[i].tail_bone;
//rot.LoadQuat( bone->quat );
//
//newhead = ma * rot * mai * H;
//newtail = ma * rot * mai * T;
//
///*if( i==2 )
//{
// mat4_t m4;
// m4.LoadMat3( bones[i].matrix_bone );
// newhead = rot * h;
// newtail = rot * t;
//}*/
// draw
glBegin( GL_POINTS );
glColor3f( 1.0, 1.0, 1.0 );
glVertex3fv( &newhead[0] );
glEnd();
glBegin( GL_LINES );
glColor3f( 1.0, 1.0, 1.0 );
glVertex3fv( &newhead[0] );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3fv( &newtail[0] );
glEnd();
}
glLineWidth( lw );
glPointSize( ps );
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
}
/*
==========
armature_anim_t::Load
==========
*/
int armature_anim_t::Load( char* filename )
{
fstream file( filename, ios::in );
char str[200];
int i, j, ii, jj;
if( !file.is_open() )
{
cout << "-->ERROR (armature_anim_t::Load): Cannot open \"" << filename << "\"" << endl;
return 0;
}
// frames num
file >> str >> frames_num;
// keyframes list
int keyframes_num;
file >> str >> keyframes_num >> str;
keyframes.Malloc( keyframes_num );
for( i=0; i<keyframes.size; i++ )
file >> keyframes[i];
// bones num
file >> str >> i;
bone_poses.Malloc( i );
// for all bones
for( i=0; i<bone_poses.size; i++ )
{
file >> str >> str >> str >> str;
file >> str >> j;
bone_poses[i].poses.Malloc( j );
for( j=0; j<bone_poses[i].poses.size; j++ )
{
// the matrix
file >> str >> str >> str >> bone_poses[i].poses[j].keyframe;
file >> str;
for( ii=0; ii<4; ii++ )
for( jj=0; jj<4; jj++ )
file >> bone_poses[i].poses[j].matrix[ii][jj];
// the loc
file >> str;
for( ii=0; ii<3; ii++ )
file >> bone_poses[i].poses[j].loc[ii];
// the quat
file >> str;
for( ii=0; ii<4; ii++ )
file >> bone_poses[i].poses[j].quat[ii];
}
}
file.close();
return 1;
}
/*
===========
model_t::Load
===========
*/
int model_t::Load( char* filename )
{
fstream file( filename, ios::in );
char str[200];
int i;
if( !file.is_open() )
{
cout << "-->ERROR (model_t::Load): Cannot open \"" << filename << "\"" << endl;
return 0;
}
// mesh
file >> str >> str;
mesh.Load(str);
mesh.EnableFastNormalCalcs();
verts_initial = mesh.verts;
// armature
file >> str >> str;
armat.Load( str );
// anims
int anims_num;
file >> str >> anims_num;
anims.Malloc( anims_num );
for( i=0; i<anims.size; i++ )
{
file >> str >> str >> str;
anims[i].Load( str );
}
// vgroup2bone
vgroup2bone.Malloc( mesh.vert_groups.size );
for( i=0; i<mesh.vert_groups.size; i++ )
{
vgroup2bone[i] = -1;
for( int j=0; j<armat.bones.size; j++ )
{
if( strcmp(mesh.vert_groups[i].name, armat.bones[j].name) == 0 )
{
vgroup2bone[i] = j;
continue;
}
}
}
// end
file.close();
return 1;
}
/*
==========
Interpolate
==========
*/
void model_t::Interpolate( int anim_id, int frame )
{
// calculate the t (used in slerp and lerp) and
// calc the l_pose and r_pose witch indicate the pose ids in witch the frame lies between
array_t<int>& keyframes = anims[anim_id].keyframes;
float t;
int l_pose=0, r_pose=0;
for( int j=0; j<keyframes.size; j++ )
{
if( keyframes[j] == frame )
{
l_pose = r_pose = j;
break;
}
else if( keyframes[j] > frame )
{
l_pose = j-1;
r_pose = j;
break;
}
}
if( l_pose==r_pose )
t = 0.0;
else
t = float( frame - keyframes[l_pose] ) / float( keyframes[r_pose] - keyframes[l_pose] );
// using the in depth search algorithm animate the bones
// from father to child cause the child needs tha father's transformation matrix
int queue [100]; // the queue for the seatch
int he = 0, ta = 0; // head and tail of queue
// put the roots (AKA the bones without father) in the queue
for( int i=0; i<anims[anim_id].bone_poses.size; i++ )
{
if( armat.bones[i].parent_id == -1 )
queue[ta++] = i;
}
// while queue is not empty
while( he!=ta )
{
int bone_id = queue[ he ];
bone_t* bone = &armat.bones[bone_id];
bone_poses_t* anim = &anims[anim_id].bone_poses[bone_id];
// =========================
// here we do the calculations of the bone's new matrix
// =========================
// 0
// the rotation
mat4_t m4rot, origin, origini;
quat_t q;
vec3_t head_i; // the inverce of bone->head vector
q.Slerp( anim->poses[l_pose].quat, anim->poses[r_pose].quat, t );
bone->quat = q;
m4rot.LoadQuat( q );
// m4rot = head * local_rot * inverted(head) AKA rotate from the bone's head
origin.LoadVec3( bone->head );
head_i = bone->head;
head_i *= -1.0;
origini.LoadVec3( head_i );
m4rot = origin * m4rot * origini;
// 1
// the translation
vec3_t transv;
mat4_t m4trans;
transv.Lerp( anim->poses[l_pose].loc, anim->poses[r_pose].loc, t);
m4trans.LoadVec3( transv );
// combine rot and trans
bone->matrix = m4rot * m4trans;
// apply the father's transformation
if( bone->parent_id!=-1 )
bone->matrix = armat.bones[ bone->parent_id ].matrix * bone->matrix;
// =========================
// end animation code
// =========================
// queue stuff:
// put the childs at the end of the queue
for( int i=0; i<bone->child_ids.size; i++ )
{
queue[ta++] = bone->child_ids[i];
}
++he;
}
}
/*
=============
model_t::ApplyArmatAnimToMesh
=============
*/
void model_t::ApplyArmatAnimToMesh()
{
//int i, j, vert_index, group_index;
//vertex_group_t* vgroup;
//
//for( i=0; i<armat.bones.size; i++ ) // for all the bones
//{
// group_index = armat.bones[i].vert_group_index;
// if( group_index==-1 ) continue;
//
// vgroup = &mesh.vert_groups[ group_index ];
// for( j=0; j<vgroup->vert_ids.size; j++ )
// {
// vert_index = vgroup->vert_ids[j];
// MatMul( dmatrices[i], initials.vert_coords[vert_index], mesh.verts[ vert_index ].coords );
// MatMul( dmatrices[i], initials.vert_norms[vert_index], mesh.verts[ vert_index ].normal );
// VecNormalize( mesh.verts[ vert_index ].normal );
// }
//}
//for( int i=0; i<armat.verts.size; i++ )
//{
// // calc the matrix according the weights
// mat4_t m4;
// m4.LoadZero();
// for( int j=0; j<armat.verts[i].weights_num; j++ )
// {
// m4 += (armat.bones[ armat.verts[i].bone_indeces[j] ].matrix * armat.verts[i].weights[j]);
// }
//
// // apply the matrix to the verts
// mesh.verts[ i ].coords = m4 * mesh_basic.verts[i].coords;
// mesh.verts[ i ].normal = m4 * mesh_basic.verts[i].normal;
// mesh.verts[ i ].normal.Normalize();
//}
for( int i=0; i<mesh.verts.size; i++ ) // for all verts
{
// calc the matrix according the weights
mat4_t m4;
m4.LoadZero();
for( int j=0; j<mesh.weights[i].pairs.size; j++ ) // for all groups of this vert
{
vertex_weights_t* vweights = &mesh.weights[i];
int vgroup_id = vweights->pairs[j].vert_group_id;
int bone_id = vgroup2bone[ vgroup_id ];
if( bone_id == -1 ) continue;
m4 += (armat.bones[ bone_id ].matrix * vweights->pairs[j].weight);
}
// apply the matrix to the verts
mesh.verts[ i ].coords = m4 * verts_initial[i].coords;
//mesh.verts[ i ].normal = m4 * mesh_basic.verts[i].normal;
//mesh.verts[ i ].normal.Normalize();
}
mesh.CalcAllNormals();
}