-
Notifications
You must be signed in to change notification settings - Fork 0
/
jvs3d_object.cpp
executable file
·720 lines (648 loc) · 15.3 KB
/
jvs3d_object.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
#ifdef _MSC_VER
//#include <afxwin.h> // MFC core and standard components
//#include <afxext.h> // MFC extensions
#include <windows.h>
#else*/
#include <lcms.h> // for _stricmp
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <string>
#ifdef _MSC_VER
#include <GL/gl.h> //OPENGL
#include <GL/glu.h> //OPENGL
#include <GL/glaux.h> //OPENGL
#endif
#include "jvslib.h"
#include "jvs3dlib.h"
///////////////////////////////////////////
// All drawgl functions - except vertice //
///////////////////////////////////////////
bool jvs3d_polygon::drawGL(jvs3d_object *pobj, jvs3d_vertice *vlist)
{
jvs3d_vertice *pv=(jvs3d_vertice*)vlist->getTop();
const unsigned char * ret=null;
#ifdef _MSC_VER
if( !
(
(
m_materialPtr &&
m_materialPtr->m_glTextureObject
)
) ||
(m_drawMode==GL_LINE_LOOP)
)
{
glPolygonMode(m_polyFace,m_polyMode);
glColor3f(m_colorR,m_colorG,m_colorB);
glBegin(m_drawMode);
} else if( m_materialPtr && m_drawMode!=GL_LINE_LOOP )
{
m_materialPtr->bindGL();
glPolygonMode(GL_FRONT,GL_FILL);
glBegin(GL_POLYGON);
}
#else
if(DBGOBJDISP)
cout << "Polygon: " << m_num << " vertices" << endl;
#endif
for(unsigned int i=0; i<m_num; i++)
{
if(DBGVERT_MATH && m_txtrX && m_txtrY)
printf( "texture coord %f, %f\n",m_txtrX[i],m_txtrY[i]);
pv=(jvs3d_vertice*)vlist->getAt(m_index[i]);
if(pv)
{
#ifdef _MSC_VER
if(m_materialPtr &&
m_txtrX && m_txtrY &&
m_materialPtr->m_materialType==JVS3D_MAT_TEXTURE)
{
glTexCoord2d(
(double)m_txtrX[i],
(double)m_txtrY[i]); //verified
}
#endif
pv->drawGL(pobj);
}
}
#ifdef _MSC_VER
glEnd();
glDisable(GL_TEXTURE_2D);
#endif
return true;
}
bool jvs3d_object::drawGL(void)
{
#ifdef _MSC_VER
return drawGL(m_polyMode,m_polyFace);
#else
return true;
#endif
}
bool jvs3d_object::drawGL(unsigned long mode, unsigned long face)
{
jvs3d_polygon *ppoly=null;
if(DBGOBJDISP) printf("Attempting to display object\n");
if(mp_polygons)
{
if(DBGOBJDISP)
printf("%u polygons to process\n",mp_polygons->countList());
ppoly=(jvs3d_polygon*)mp_polygons->getTop();
// cycle through each polygon, drawing each
while(ppoly)
{
ppoly->drawMode(m_drawMode);
ppoly->polygonMode(mode,face);
ppoly->drawGL(this,mp_vertices);
ppoly=(jvs3d_polygon*)ppoly->getNext();
}
}
return true;
}
/***************************************************************
* class jvs3d_object - start
***************************************************************/
jvs3d_object::jvs3d_object()
{
mp_vertices=null;
mp_polygons=null;
m_drawMode=0;
m_polyMode=GL_FRONT;
m_polyFace=GL_FILL;
m_translate.set(0,0,0);
m_rotate.set(0,0,0);
}
jvs3d_object::~jvs3d_object()
{
void * last=null;
if(DBGDEL) printf("killing object\n");
if(mp_vertices)
{
last=mp_vertices->getTop();
mp_vertices=(jvs3d_vertice*)last;
while( mp_vertices=(jvs3d_vertice*)mp_vertices->getNext() )
{
delete (jvs3d_vertice*)last;
last = (void *)mp_vertices;
}
}
if(mp_polygons)
{
last=mp_polygons->getTop();
mp_polygons=(jvs3d_polygon*)last;
while( mp_polygons=(jvs3d_polygon*)mp_polygons->getNext() )
{
if(last) delete (jvs3d_polygon*)last;
last = (void *)mp_polygons;
}
}
}
bool jvs3d_object::addVertice(float x, float y, float z)
{
jvs3d_vertice *ptr = new jvs3d_vertice;
if(ptr)
{
ptr->set(x,y,z);
ptr->m_object=this;
if(DBGOBJLOAD) printf("vertice set\n");
if(mp_vertices==null)
{
mp_vertices=ptr;
return true;
} else if( mp_vertices->appendEnd(ptr)==null )
{ printf( "error: a vertice could not be tacked onto vertice list\n"); exit(1); }
} else { printf("error: allocation for an object vertice failed\n"); exit(1); }
return true;
}
bool jvs3d_object::addPolygon(float txo, float tyo, int length, uint *idx_list)
{
jvs3d_polygon *ptr=new jvs3d_polygon;
ptr->m_num=length;
ptr->m_index = new uint[length];
if(DBGPOLY)
printf("adding polygon definition\n" );
for(int i=0;i<length;i++)
{
if(DBGPOLY) printf(" idx: $u", idx_list[i]);
ptr->m_index[i]=idx_list[i];
}
ptr->prepTextureCoords(*mp_vertices);
if(!mp_polygons)
mp_polygons=ptr;
else mp_polygons->appendEnd(ptr);
return true;
}
unsigned int jvs3d_object::verticeCount(void)
{
jvs_baseclass *ptr = (jvs_baseclass *) this->mp_vertices;
if(ptr) return ptr->countList();
else return 0;
}
bool jvs3d_object::applyFrame(jvs3d_frame *frame, jvs3d_armature *exclude)
{
jvs3d_armupdate *pau=null; // current arm update
pau=frame->m_armUpdates;
while(pau)
{
//if(pau->m_
pau=(jvs3d_armupdate*)pau->getNext();
} // while(pau)
return true;
}
///////////////////
// file handling //
///////////////////
bool jvs3d_object::loadXFile(const char *pathfile)
// load x formatted file
{
int verts=0, faces=0;
//unsigned long ret;
jvs3d_file_x parser;
if(!parser.openFile(pathfile)) return false;
while(parser.readNext())
if(!strcmp(parser.s_jvs_cur,"Mesh"))
{printf("FIRST FOUND!");break;}
parser.readNext();
parser.readNext();
return true;
}
bool jvs3d_object::storeXFile(const char *pathfile)
// store x formatted file
{
return true;
}
bool jvs3d_object::loadAC3DFile(const char *pathfile)
{
jvs3d_file_ac3d fil;
if(fil.loadFile(pathfile,this)) return true;
else return false;
}
bool jvs3d_object::storeAC3DFile(const char *pathfile)
{return false;}
/////////////////////////
// Internal Adjustment //
/////////////////////////
bool jvs3d_object::newCenter(void)
{
if(!mp_vertices) return true;
double mx=0, my=0, mz=0;
unsigned int n;
n = mp_vertices->countList();
jvs3d_vertice *v=(jvs3d_vertice*)mp_vertices->getTop();
while(v)
{
mx+=v->m_data[0];
my+=v->m_data[1];
mz+=v->m_data[2];
v=(jvs3d_vertice*)v->getNext();
}
mx = mx / n;
my = my / n;
mz = mz / n;
v=(jvs3d_vertice*)mp_vertices->getTop();
while(v)
{
v->m_data[0]-=mx;
v->m_data[1]-=my;
v->m_data[2]-=mz;
v=(jvs3d_vertice*)v->getNext();
}
return true;
}
//////////////////////
// Display settings //
//////////////////////
void jvs3d_object::polygonMode(unsigned long face, unsigned long mode)
{m_polyMode=mode; m_polyFace=face;}
void jvs3d_object::drawMode(unsigned long mode)
{m_drawMode=mode;}
/***************************************************************
* class jvs3d_polygon - start
***************************************************************/
jvs3d_polygon::jvs3d_polygon()
{
#ifdef _MSC_VER
m_polyFace=GL_FRONT;
m_polyMode=GL_FILL;
m_drawMode=GL_POLYGON;
#else
m_polyFace=0;
m_polyMode=0;
m_drawMode=0;
#endif
m_colorR=0.5f;
m_colorG=0.5f;
m_colorB=0.5f;
m_materialPtr=0;
m_txtrX=0;
m_txtrY=0;
m_index=0;
m_num=0;
}
jvs3d_polygon::~jvs3d_polygon()
{
if(DBGDEL) printf("killing polygon\n");
if(m_index)
{
if(DBGDEL) printf("killing index array\n");
delete m_index;
}
if(m_txtrX)
{
if(DBGDEL) printf("killing x coord array\n");
delete m_txtrX;
}
if(m_txtrY)
{
if(DBGDEL) printf("killing y coord array\n" );
delete m_txtrY;
}
if(DBGDEL) printf("polygon killed.\n" );
}
void jvs3d_polygon::polygonMode(unsigned long face, unsigned long mode)
{m_polyMode=mode; m_polyFace=face;}
void jvs3d_polygon::drawMode(unsigned long mode)
{m_drawMode=mode;}
double * jvs3d_polygon::getLenAng(jvs3d_vertice &verts)
{
//return false;
// not even a triangle!? return that bitch; we ain doin shit! fer rizza
if(m_num<3) return false;
if(verts.countList()<3) return false;
// determine the angle at each point of the polygon;
// line texture up with idx edge on polygon
double *toret = new double[m_num*2];
jvs3d_vertice a,b,c;
unsigned int j=0;
if(toret)
{
toret[0]=0;toret[m_num]=0;
a.set((jvs3d_vertice *)verts[m_index[0]]);
b.set((jvs3d_vertice *)verts[m_index[1]]);
b-=a;
toret[1]=0; // cosine
toret[m_num+1]=b.length();
for(int i=2; i < m_num; i++)
{
a.set((jvs3d_vertice *)verts[m_index[1]]);
b.set((jvs3d_vertice *)verts[m_index[0]]);
c.set((jvs3d_vertice *)verts[m_index[i]]);
a-=b;
c-=b;
toret[i]=fabs(a.dotcos(c));
toret[i+m_num]=c.length();
if(DBGVERT_MATH)
printf("this index %u has cosine %f and length %f from v[0] and v[1] \n" ,i ,toret[i] ,toret[i+m_num]);
}
return toret;
} else return false;
}
void jvs3d_polygon::textureCenter(double *x, double *y)
{
int i=0;
*x=0;
*y=0;
if(m_materialPtr && m_num>0)
{
for(i=0;i<m_num;i++)
{
*x+=m_txtrX[i];
*y+=m_txtrY[i];
}
*x/=(double)m_num;*y/=(double)m_num;
for(i=0;i<m_num;i++)
{
m_txtrX[i]-=*x;
m_txtrY[i]-=*y;
}
}
}
void jvs3d_polygon::textureTranslate(double x, double y)
{
unsigned int i=0;
if(m_materialPtr && m_num>0)
{
for(i=0;i < m_num;i++)
{
m_txtrX[i]+=x;
m_txtrY[i]+=y;
}
}
}
void jvs3d_polygon::textureRotate(double theta)
{
double x=0, y=0;
double *oldY=new double;
double *oldX=new double;
double rad=(JVS_PI/180.00)*theta;
int i=0;
if(m_materialPtr && m_num>0)
{
textureCenter(oldX,oldY);
for(i=0;i<m_num;i++)
{
m_txtrX[i]=m_txtrX[i]*cos(rad)-m_txtrY[i]*sin(rad);
m_txtrY[i]=m_txtrY[i]*cos(rad)+m_txtrX[i]*sin(rad);
}
textureTranslate(*oldX,*oldY);
}
}
void jvs3d_polygon::textureScale(double x, double y)
{
double *oldY=new double;
double *oldX=new double;
int i=0;
if(m_materialPtr && m_num>0)
{
textureCenter(oldX,oldY);
for(i=0;i<m_num;i++)
{
m_txtrX[i]*=x;
m_txtrY[i]*=y;
}
textureTranslate(*oldX,*oldY);
}
}
bool jvs3d_polygon::prepTextureCoords(jvs3d_vertice &verts)
{
double *lenangs=getLenAng(verts);
double s;
if(!lenangs) return false;
jvs3d_vertice *thisvert;
double width, height;
if(m_txtrX) {delete m_txtrX;m_txtrX=0;}
if(m_txtrY) {delete m_txtrY;m_txtrY=0;}
if(!(m_txtrX && m_txtrY))
{
m_txtrX=new double[m_num];
m_txtrY=new double[m_num];
}
if(m_txtrY && m_txtrX)
{
if(DBGVERT_MATH) printf("successful allocate: %d, %d\n", m_txtrX, m_txtrY);
m_txtrX[0]=0;
m_txtrY[0]=0;
m_txtrX[1]=lenangs[1+m_num];
m_txtrY[1]=0;
if(DBGVERT_MATH)
{
printf("%d,%d\n",m_txtrX[0],m_txtrY[0]);
printf("%d,%d\n",m_txtrX[1],m_txtrY[1]);
}
for(int idx=2; idx<m_num; idx++)
{
m_txtrX[idx]=lenangs[idx+m_num]*lenangs[idx];
m_txtrY[idx]=lenangs[idx+m_num]*sin(acos(lenangs[idx]));
if(DBGVERT_MATH)
printf("%d, %d\n",m_txtrX[idx],m_txtrY[idx]);
}
} else return false;
}
/***************************************************************
* class jvs3d_armature - start
***************************************************************/
jvs3d_armature::jvs3d_armature()
{
m_armatures=0;
// m_vertices=0;
m_parentArmature=0;
m_pivot.set(0,0,0);
m_matrix.initialize(JVS3D_MATRIX_IDENT,null);
m_parentName=null;
};
jvs3d_armature::~jvs3d_armature()
{
// if(m_vertices)
// delete m_vertices;
if(m_armatures)
delete m_armatures;
};
///////////////
/*
bool jvs3d_armature::addVertice(jvs3d_vertice *v)
{
jvs_baseclass *ptr=null;
if(hasVertice(v))
return false;
if(m_vertices)
{
ptr = new jvs_baseclass;
ptr->m_ptr=v;
m_vertices->appendEnd(ptr);
} else
{
m_vertices = new jvs_baseclass;
m_vertices->m_ptr=v;
}
return true;
}
jvs_baseclass * jvs3d_armature::hasVertice(jvs3d_vertice *v)
{
jvs_baseclass *ptr=null;
if(m_vertices)
{
ptr=(jvs_baseclass*)m_vertices->getTop();
while(ptr)
{
if(v==ptr->m_ptr)
return ptr;
ptr=(jvs_baseclass*)ptr->getNext();
}
return false;
} else return false;
}
bool jvs3d_armature::remVertice(jvs3d_vertice *v)
{
jvs_baseclass *ptr=null;
if(ptr=hasVertice(v))
{
if(m_vertices->countList()>=1)
{
if(m_vertices==ptr)
{
if( m_vertices->getNext() )
m_vertices=(jvs_baseclass*)m_vertices->getNext();
else if( m_vertices->getPrev() )
m_vertices=(jvs_baseclass*)m_vertices->getPrev();
}
m_vertices->removeMember(ptr);
} else m_vertices=null;
delete ptr;
return true;
} else return false;
}*/
///////////////
bool jvs3d_armature::addArmature(jvs3d_armature *v)
{
jvs_baseclass *ptr=null;
if(hasArmature(v))
return false;
if(m_armatures)
{
ptr = new jvs_baseclass;
ptr->m_ptr=v;
m_armatures->appendEnd(ptr);
} else
{
m_armatures = new jvs_baseclass;
m_armatures->m_ptr=v;
}
v->m_parentArmature=(jvs3d_armature*)this;
return true;
}
jvs_baseclass * jvs3d_armature::hasArmature(jvs3d_armature *v)
{
jvs_baseclass *ptr=null;
if(m_armatures)
{
ptr=(jvs_baseclass*)m_armatures->getTop();
while(ptr)
{
if(v==ptr->m_ptr)
return ptr;
ptr=(jvs_baseclass*)ptr->getNext();
}
return false;
} else return false;
}
bool jvs3d_armature::remArmature(jvs3d_armature *v)
{
jvs_baseclass *ptr=null;
if(ptr=hasArmature(v))
{
v->m_parentArmature=null;
if(m_armatures->countList()>=1)
{
if(m_armatures==ptr)
{
if( m_armatures->getNext() )
m_armatures=(jvs_baseclass*)m_armatures->getNext();
else if( m_armatures->getPrev() )
m_armatures=(jvs_baseclass*)m_armatures->getPrev();
}
m_armatures->removeMember(ptr);
} else m_armatures=null;
delete ptr;
return true;
} else return false;
}
bool jvs3d_armature::setupMatrices(jvs3d_armupdate *udates,jvs3d_armature *parent)
// pass in null, parent is necessary for recursive aspect of function
{
jvs3d_armature *parm = (jvs3d_armature*)this->getTop();
jvs3d_armupdate *pudate=null;
jvs3d_matrix rm;
int i=0;
while(parm)
{
pudate=udates->findArmature(parm);
if(pudate)
parm->m_matrix.initialize(JVS3D_MATRIX_ROT,pudate->m_rotation);
parm=(jvs3d_armature*)parm->getNext();
}
return true;
}
bool jvs3d_armature::clearMatrices(void)
{
jvs3d_armature *parm = (jvs3d_armature*)this->getTop();
while(parm)
{
parm->m_matrix.initialize(JVS3D_MATRIX_IDENT,null);
parm=(jvs3d_armature*)parm->getNext();
}
return true;
}
/***************************************************************
* class jvs3d_frame - start
***************************************************************/
jvs3d_armupdate::jvs3d_armupdate()
{
m_armature=null;
m_rotation[0]=0;
m_rotation[1]=0;
m_rotation[2]=0;
}
jvs3d_armupdate* jvs3d_armupdate::findArmature(jvs3d_armature *arm)
{
jvs3d_armupdate *pudate;
pudate=(jvs3d_armupdate*)this->getTop();
while(pudate)
{
if(pudate->m_armature==arm)
return pudate;
pudate=(jvs3d_armupdate*)pudate->getNext();
}
return null;
}
jvs3d_frame::jvs3d_frame()
{
m_armUpdates=null;
m_parentCycle=null;
}
jvs3d_frame::~jvs3d_frame()
{
jvs3d_armupdate *parmupdate=m_armUpdates;
jvs3d_armupdate *tokill=null;
while(parmupdate)
{
tokill=parmupdate;
parmupdate=(jvs3d_armupdate*)parmupdate->getNext();
if(tokill) delete tokill;
}
}
jvs3d_cycle::jvs3d_cycle() {m_frames=null;m_curFrame=null;}
jvs3d_cycle::~jvs3d_cycle()
{
jvs3d_frame *pframe=m_frames;
jvs3d_frame *tokill=null;
while(pframe)
{
tokill=pframe;
pframe=(jvs3d_frame*)pframe->getNext();
if(tokill) delete tokill;
}
}