35 #include <libFreeWRL.h>
37 #include "../vrml_parser/Structs.h"
38 #include "../vrml_parser/CRoutes.h"
39 #include "../main/headers.h"
40 #include "../opengl/Material.h"
41 #include "../opengl/OpenGL_Utils.h"
43 #include "../scenegraph/RenderFuncs.h"
44 #include "../opengl/Frustum.h"
45 #include "LinearAlgebra.h"
248 void *Component_HAnim_constructor(){
253 void Component_HAnim_init(
struct tComponent_HAnim *t){
256 t->prv = Component_HAnim_constructor();
263 void Component_HAnim_clear(
struct tComponent_HAnim *t){
278 node->__do_center = verify_translate ((GLfloat *)node->center.c);
279 node->__do_trans = verify_translate ((GLfloat *)node->translation.c);
280 node->__do_scale = verify_scale ((GLfloat *)node->scale.c);
281 node->__do_rotation = verify_rotate ((GLfloat *)node->rotation.c);
282 node->__do_scaleO = verify_rotate ((GLfloat *)node->scaleOrientation.c);
284 node->__do_anything = (node->__do_center ||
287 node->__do_rotation ||
309 if(!renderstate()->render_vp) {
311 if (node->__do_anything) {
316 if (node->__do_trans)
317 FW_GL_TRANSLATE_F(node->translation.c[0],node->translation.c[1],node->translation.c[2]);
320 if (node->__do_center)
321 FW_GL_TRANSLATE_F(node->center.c[0],node->center.c[1],node->center.c[2]);
324 if (node->__do_rotation) {
325 FW_GL_ROTATE_RADIANS(node->rotation.c[3], node->rotation.c[0],node->rotation.c[1],node->rotation.c[2]);
329 if (node->__do_scaleO) {
330 FW_GL_ROTATE_RADIANS(node->scaleOrientation.c[3], node->scaleOrientation.c[0], node->scaleOrientation.c[1],node->scaleOrientation.c[2]);
335 if (node->__do_scale)
336 FW_GL_SCALE_F(node->scale.c[0],node->scale.c[1],node->scale.c[2]);
339 if (node->__do_scaleO)
340 FW_GL_ROTATE_RADIANS(-node->scaleOrientation.c[3], node->scaleOrientation.c[0], node->scaleOrientation.c[1],node->scaleOrientation.c[2]);
343 if (node->__do_center)
344 FW_GL_TRANSLATE_F(-node->center.c[0],-node->center.c[1],-node->center.c[2]);
358 if(!renderstate()->render_vp) {
359 if (node->__do_anything) {
364 if((node->_renderFlags & VF_Viewpoint) == VF_Viewpoint) {
365 FW_GL_TRANSLATE_F(((node->center).c[0]),((node->center).c[1]),((node->center).c[2])
367 FW_GL_ROTATE_RADIANS(((node->scaleOrientation).c[3]),((node->scaleOrientation).c[0]),((node->scaleOrientation).c[1]),((node->scaleOrientation).c[2])
369 FW_GL_SCALE_F((
float)1.0/(((node->scale).c[0])),(
float)1.0/(((node->scale).c[1])),(
float)1.0/(((node->scale).c[2]))
371 FW_GL_ROTATE_RADIANS(-(((node->scaleOrientation).c[3])),((node->scaleOrientation).c[0]),((node->scaleOrientation).c[1]),((node->scaleOrientation).c[2])
373 FW_GL_ROTATE_RADIANS(-(((node->rotation).c[3])),((node->rotation).c[0]),((node->rotation).c[1]),((node->rotation).c[2])
375 FW_GL_TRANSLATE_F(-(((node->center).c[0])),-(((node->center).c[1])),-(((node->center).c[2]))
377 FW_GL_TRANSLATE_F(-(((node->translation).c[0])),-(((node->translation).c[1])),-(((node->translation).c[2]))
389 node->__do_center = verify_translate ((GLfloat *)node->center.c);
390 node->__do_trans = verify_translate ((GLfloat *)node->translation.c);
391 node->__do_scale = verify_scale ((GLfloat *)node->scale.c);
392 node->__do_rotation = verify_rotate ((GLfloat *)node->rotation.c);
393 node->__do_scaleO = verify_rotate ((GLfloat *)node->scaleOrientation.c);
395 node->__do_anything = (node->__do_center ||
398 node->__do_rotation ||
420 if(!renderstate()->render_vp) {
422 if (node->__do_anything) {
427 if (node->__do_trans)
428 FW_GL_TRANSLATE_F(node->translation.c[0],node->translation.c[1],node->translation.c[2]);
431 if (node->__do_center)
432 FW_GL_TRANSLATE_F(node->center.c[0],node->center.c[1],node->center.c[2]);
435 if (node->__do_rotation) {
436 FW_GL_ROTATE_RADIANS(node->rotation.c[3], node->rotation.c[0],node->rotation.c[1],node->rotation.c[2]);
440 if (node->__do_scaleO) {
441 FW_GL_ROTATE_RADIANS(node->scaleOrientation.c[3], node->scaleOrientation.c[0], node->scaleOrientation.c[1],node->scaleOrientation.c[2]);
446 if (node->__do_scale)
447 FW_GL_SCALE_F(node->scale.c[0],node->scale.c[1],node->scale.c[2]);
450 if (node->__do_scaleO)
451 FW_GL_ROTATE_RADIANS(-node->scaleOrientation.c[3], node->scaleOrientation.c[0], node->scaleOrientation.c[1],node->scaleOrientation.c[2]);
454 if (node->__do_center)
455 FW_GL_TRANSLATE_F(-node->center.c[0],-node->center.c[1],-node->center.c[2]);
469 if(!renderstate()->render_vp) {
470 if (node->__do_anything) {
475 if((node->_renderFlags & VF_Viewpoint) == VF_Viewpoint) {
476 FW_GL_TRANSLATE_F(((node->center).c[0]),((node->center).c[1]),((node->center).c[2])
478 FW_GL_ROTATE_RADIANS(((node->scaleOrientation).c[3]),((node->scaleOrientation).c[0]),((node->scaleOrientation).c[1]),((node->scaleOrientation).c[2])
480 FW_GL_SCALE_F((
float)1.0/(((node->scale).c[0])),(
float)1.0/(((node->scale).c[1])),(
float)1.0/(((node->scale).c[2]))
482 FW_GL_ROTATE_RADIANS(-(((node->scaleOrientation).c[3])),((node->scaleOrientation).c[0]),((node->scaleOrientation).c[1]),((node->scaleOrientation).c[2])
484 FW_GL_ROTATE_RADIANS(-(((node->rotation).c[3])),((node->rotation).c[0]),((node->rotation).c[1]),((node->rotation).c[2])
486 FW_GL_TRANSLATE_F(-(((node->center).c[0])),-(((node->center).c[1])),-(((node->center).c[2]))
488 FW_GL_TRANSLATE_F(-(((node->translation).c[0])),-(((node->translation).c[1])),-(((node->translation).c[2]))
500 VERTEXTRANSFORMMETHOD_CPU = 1,
501 VERTEXTRANSFORMMETHOD_GPU = 2,
503 static int vertexTransformMethod = VERTEXTRANSFORMMETHOD_CPU;
510 int i,j, jointTransformIndex;
511 double modelviewMatrix[16];
523 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, modelviewMatrix);
524 matmultiplyAFFINE(jointMatrix.mat,modelviewMatrix,p->HHMatrix);
525 if(p->HH->skinNormal){
528 float fmat4[16], fmat3[9],fmat3i[9];
529 matdouble2float4(fmat4,jointMatrix.mat);
530 mat423f(fmat3,fmat4);
531 matinverse3f(fmat3i,fmat3);
532 mattranspose3f(jointMatrix.normat,fmat3i);
536 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
539 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
541 stack_push(
JMATRIX,JT,jointMatrix);
546 jointTransformIndex = vectorSize(JT);
549 PVW = (
float*)p->HH->_PVW;
550 PVI = (
float*)p->HH->_PVI;
551 for(i=0;i<node->skinCoordIndex.n;i++){
552 int idx = node->skinCoordIndex.p[i];
553 float wt = node->skinCoordWeight.p[min(i,node->skinCoordWeight.n -1)];
555 if(PVI[idx*4 + j] == 0.0f){
556 PVI[idx*4 +j] = (float)jointTransformIndex;
562 if(p->HH->skinCoord && node->displacers.n ){
567 psc = (
float*)nc->point.p;
569 for(i=0;i<node->displacers.n;i++){
571 float *point, weight, wdisp[3];
576 pdp = (
float*)dp->displacements.p;
579 ni = dp->coordIndex.n;
580 ci = dp->coordIndex.p;
583 point = &psc[index*3];
584 vecscale3f(wdisp,&pdp[j*3],weight);
585 vecadd3f(point,point,wdisp);
592 p->HH->skinCoord->_change++;
593 parents = p->HH->skinCoord->_parentVector;
594 for(k=0;k<vectorSize(parents);k++){
603 int vecsametol3f(
float *a,
float *b,
float tol){
606 if(fabsf(a[i] - b[i]) > tol) isame = FALSE;
612 int nsc = 0, nsn = 0;
613 float *psc = NULL, *psn = NULL;
614 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
617 psc = (
float*)nc->point.p;
618 node->_origCoords = realloc(node->_origCoords,nsc*3*
sizeof(
float));
619 memcpy(node->_origCoords,psc,nsc*3*
sizeof(
float));
622 float myfind[9] = {-0.030000f, -0.070000f, 1.777000f, -0.070000f, 1.777000f, 0.130000f, 1.777000f, 0.130000f, 0.070000f };
626 if(vecsametol3f(&psc[i*3],&myfind[j*3],.001f)){
627 printf(
"%d %f %f %f\n",i,myfind[j*3 + 0],myfind[j*3 +1],myfind[j*3 +2]);
632 if(node->skinNormal && node->skinNormal->_nodeType == NODE_Normal){
636 psn = (
float*)nn->vector.p;
637 node->_origNorms = realloc(node->_origNorms,nsn*3*
sizeof(
float));
638 memcpy(node->_origNorms,psn,nsn*3*
sizeof(
float));
644 if(node->_NV == 0 || node->_NV != nsc){
645 node->_PVI = realloc(node->_PVI,nsc*4*
sizeof(
float));
646 node->_PVW = realloc(node->_PVW,nsc*4*
sizeof(
float));
650 if(node->_JT == NULL) {
651 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
653 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
680 nc = node->joints.n + node->segments.n + node->viewpoints.n + node->sites.n +
681 node->skeleton.n + node->skin.n;
683 RETURN_FROM_CHILD_IF_NOT_FOR_ME
685 if(renderstate()->render_vp){
687 normalChildren(node->viewpoints);
697 if(0) normalChildren(node->segments);
703 if(0) normalChildren(node->joints);
711 if(0) normalChildren(node->sites);
713 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
718 memset(node->_PVI,0,4*node->_NV*
sizeof(
float));
719 memset(node->_PVW,0,4*node->_NV*
sizeof(
float));
726 double modelviewMatrix[16];
727 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, modelviewMatrix);
728 matinverseAFFINE(p->HHMatrix,modelviewMatrix);
731 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
735 int nsc = 0, nsn = 0;
736 float *psc = NULL, *psn = NULL;
737 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
741 psc = (
float*)nc->point.p;
742 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
745 psn = (
float *)nn->vector.p;
746 memcpy(psn,node->_origNorms,3*nsn*
sizeof(
float));
751 if(1) normalChildren(node->skeleton);
754 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
759 float *psc = NULL, *psn = NULL;
760 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
764 psc = (
float*)nc->point.p;
768 psn = (
float *)nn->vector.p;
775 float newpoint[3], newnorm[3];
780 if(nn) norm = &psn[i*3];
784 memset(newpoint,0,3*
sizeof(
float));
785 memset(newnorm,0,3*
sizeof(
float));
788 int jointTransformIndex = (int)PVI[i*4 + j];
789 float wt = PVW[i*4 + j];
790 if(jointTransformIndex > 0){
791 float tpoint[3], tnorm[3];
793 jointMatrix = vector_get(
JMATRIX,node->_JT,jointTransformIndex -1);
794 transformf(tpoint,point,jointMatrix.mat);
795 vecscale3f(tpoint,tpoint,wt);
796 vecadd3f(newpoint,newpoint,tpoint);
798 transform3x3f(tnorm,norm,jointMatrix.normat);
799 vecnormalize3f(tnorm,tnorm);
800 vecscale3f(tnorm,tnorm,wt);
801 vecadd3f(newnorm,newnorm,tnorm);
806 if(totalWeight > 0.0f){
807 vecscale3f(newpoint,newpoint,1.0f/totalWeight);
808 veccopy3f(point,newpoint);
810 vecscale3f(newnorm,newnorm,1.0f/totalWeight);
811 vecnormalize3f(norm,newnorm);
817 float *osc = node->_origCoords;
820 for(j=0;j<3;j++) printf(
"%f ",psc[i*3 +j]);
822 for(j=0;j<3;j++) printf(
"%f ",osc[i*3 +j]);
833 node->skinCoord->_change++;
834 parents = node->skinCoord->_parentVector;
835 for(k=0;k<vectorSize(parents);k++){
842 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
846 if(1) normalChildren(node->skin);
847 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
849 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
856 psc = (
float*)nc->point.p;
857 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
860 psn = (
float*)nn->vector.p;
861 memcpy(psn,node->_origNorms,3*nsn*
sizeof(
float));
865 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
885 normalChildren(node->children);
889 float *vecmix3f(
float *out3,
float* a3,
float *b3,
float fraction){
892 out3[i] = (1.0f - fraction)*a3[i] + fraction*b3[i];
912 if(node->coord && node->displacers.n){
918 psc = (
float*)nc->point.p;
920 if(!node->_origCoords)
921 node->_origCoords = malloc(3*nsc*
sizeof(
float));
922 memcpy(node->_origCoords,psc,3*nsc*
sizeof(
float));
923 for(i=0;i<node->displacers.n;i++){
925 float *point, weight, wdisp[3];
930 pdp = (
float*)dp->displacements.p;
933 ni = dp->coordIndex.n;
934 ci = dp->coordIndex.p;
937 point = &psc[index*3];
938 vecscale3f(wdisp,&pdp[j*3],weight);
939 vecadd3f(point,point,wdisp);
946 node->coord->_change++;
947 parents = node->coord->_parentVector;
948 for(k=0;k<vectorSize(parents);k++){
957 float myfind[9] = {-0.029100f, 1.603000f, 0.042740f, -0.045570f, 1.601000f, 0.036520f, -0.018560f, 1.600000f, 0.043490f };
962 if(vecsametol3f(&psc[i*3],&myfind[j*3],.0001f)){
963 printf(
"%d %f %f %f\n",i,myfind[j*3 + 0],myfind[j*3 +1],myfind[j*3 +2]);
971 normalChildren(node->children);
972 if(node->coord && node->displacers.n){
976 psc = (
float*)nc->point.p;
978 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
993 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
996 normalChildren(node->children);
999 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);