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"
248void *Component_HAnim_constructor(){
253void Component_HAnim_init(
struct tComponent_HAnim *t){
256 t->prv = Component_HAnim_constructor();
263void 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,
503static 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++){
603int vecsametol3f(
float *a,
float *b,
float tol){
606 if(fabsf(a[i] - b[i]) > tol) isame = FALSE;
614 int nsc = 0, nsn = 0;
615 float *psc = NULL, *psn = NULL;
616 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
620 psc = (
float*)nc->point.p;
621 node->_origCoords = realloc(node->_origCoords,nsc*3*
sizeof(
float));
622 memcpy(node->_origCoords,psc,nsc*3*
sizeof(
float));
625 float myfind[9] = {-0.030000f, -0.070000f, 1.777000f, -0.070000f, 1.777000f, 0.130000f, 1.777000f, 0.130000f, 0.070000f };
629 if(vecsametol3f(&psc[i*3],&myfind[j*3],.001f)){
630 printf(
"%d %f %f %f\n",i,myfind[j*3 + 0],myfind[j*3 +1],myfind[j*3 +2]);
634 extent6f_from_box3fn(ee,nc->point.p->c, nc->point.n);
635 setExtent(ee[0],ee[1],ee[2],ee[3],ee[4],ee[5],X3D_NODE(node));
637 if(node->skinNormal && node->skinNormal->_nodeType == NODE_Normal){
641 psn = (
float*)nn->vector.p;
642 node->_origNorms = realloc(node->_origNorms,nsn*3*
sizeof(
float));
643 memcpy(node->_origNorms,psn,nsn*3*
sizeof(
float));
649 if(node->_NV == 0 || node->_NV != nsc){
650 node->_PVI = realloc(node->_PVI,nsc*4*
sizeof(
float));
651 node->_PVW = realloc(node->_PVW,nsc*4*
sizeof(
float));
655 if(node->_JT == NULL) {
656 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
658 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
685 nc = node->joints.n + node->segments.n + node->viewpoints.n + node->sites.n +
686 node->skeleton.n + node->skin.n;
688 RETURN_FROM_CHILD_IF_NOT_FOR_ME
690 if(renderstate()->render_vp){
692 normalChildren(node->viewpoints);
702 if(0) normalChildren(node->segments);
708 if(0) normalChildren(node->joints);
716 if(0) normalChildren(node->sites);
718 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
723 memset(node->_PVI,0,4*node->_NV*
sizeof(
float));
724 memset(node->_PVW,0,4*node->_NV*
sizeof(
float));
731 double modelviewMatrix[16];
732 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, modelviewMatrix);
733 matinverseAFFINE(p->HHMatrix,modelviewMatrix);
736 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
740 int nsc = 0, nsn = 0;
741 float *psc = NULL, *psn = NULL;
742 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
746 psc = (
float*)nc->point.p;
747 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
750 psn = (
float *)nn->vector.p;
751 memcpy(psn,node->_origNorms,3*nsn*
sizeof(
float));
756 if(1) normalChildren(node->skeleton);
759 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
764 float *psc = NULL, *psn = NULL;
765 if(node->skinCoord && node->skinCoord->_nodeType == NODE_Coordinate){
770 psc = (
float*)nc->point.p[0].c;
774 psn = (
float *)nn->vector.p;
781 float newpoint[3], newnorm[3];
786 if(nn) norm = &psn[i*3];
790 memset(newpoint,0,3*
sizeof(
float));
791 memset(newnorm,0,3*
sizeof(
float));
794 int jointTransformIndex = (int)PVI[i*4 + j];
795 float wt = PVW[i*4 + j];
796 if(jointTransformIndex > 0){
797 float tpoint[3], tnorm[3];
799 jointMatrix = vector_get(
JMATRIX,node->_JT,jointTransformIndex -1);
800 transformf(tpoint,point,jointMatrix.mat);
801 vecscale3f(tpoint,tpoint,wt);
802 vecadd3f(newpoint,newpoint,tpoint);
804 transform3x3f(tnorm,norm,jointMatrix.normat);
805 vecnormalize3f(tnorm,tnorm);
806 vecscale3f(tnorm,tnorm,wt);
807 vecadd3f(newnorm,newnorm,tnorm);
812 if(totalWeight > 0.0f){
813 vecscale3f(newpoint,newpoint,1.0f/totalWeight);
814 veccopy3f(point,newpoint);
816 vecscale3f(newnorm,newnorm,1.0f/totalWeight);
817 vecnormalize3f(norm,newnorm);
823 float *osc = node->_origCoords;
826 for(j=0;j<3;j++) printf(
"%f ",psc[i*3 +j]);
828 for(j=0;j<3;j++) printf(
"%f ",osc[i*3 +j]);
840 node->skinCoord->_change++;
841 parents = node->skinCoord->_parentVector;
842 for(k=0;k<vectorSize(parents);k++){
849 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
853 if(1) normalChildren(node->skin);
854 if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_GPU){
856 }
else if(vertexTransformMethod == VERTEXTRANSFORMMETHOD_CPU){
863 psc = (
float*)nc->point.p;
864 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
867 psn = (
float*)nn->vector.p;
868 memcpy(psn,node->_origNorms,3*nsn*
sizeof(
float));
872 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
892 normalChildren(node->children);
896float *vecmix3f(
float *out3,
float* a3,
float *b3,
float fraction){
899 out3[i] = (1.0f - fraction)*a3[i] + fraction*b3[i];
919 if(node->coord && node->displacers.n){
925 psc = (
float*)nc->point.p;
927 if(!node->_origCoords)
928 node->_origCoords = malloc(3*nsc*
sizeof(
float));
929 memcpy(node->_origCoords,psc,3*nsc*
sizeof(
float));
930 for(i=0;i<node->displacers.n;i++){
932 float *point, weight, wdisp[3];
937 pdp = (
float*)dp->displacements.p;
940 ni = dp->coordIndex.n;
941 ci = dp->coordIndex.p;
944 point = &psc[index*3];
945 vecscale3f(wdisp,&pdp[j*3],weight);
946 vecadd3f(point,point,wdisp);
953 node->coord->_change++;
954 parents = node->coord->_parentVector;
955 for(k=0;k<vectorSize(parents);k++){
964 float myfind[9] = {-0.029100f, 1.603000f, 0.042740f, -0.045570f, 1.601000f, 0.036520f, -0.018560f, 1.600000f, 0.043490f };
969 if(vecsametol3f(&psc[i*3],&myfind[j*3],.0001f)){
970 printf(
"%d %f %f %f\n",i,myfind[j*3 + 0],myfind[j*3 +1],myfind[j*3 +2]);
978 normalChildren(node->children);
979 if(node->coord && node->displacers.n){
983 psc = (
float*)nc->point.p;
985 memcpy(psc,node->_origCoords,3*nsc*
sizeof(
float));
1000 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
1003 normalChildren(node->children);
1006 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);