35 #include <libFreeWRL.h>
37 #include "../vrml_parser/Structs.h"
38 #include "../main/headers.h"
40 #include "../x3d_parser/Bindable.h"
41 #include "LinearAlgebra.h"
42 #include "Collision.h"
43 #include "quaternion.h"
45 #include "../opengl/Frustum.h"
47 #include "../opengl/OpenGL_Utils.h"
48 #include "../scenegraph/RenderFuncs.h"
51 struct X3D_Node *getActiveLayerBoundViewpoint();
56 if (!renderstate()->render_vp)
return;
75 if((
struct X3D_Node*)node == getActiveLayerBoundViewpoint() && !node->_donethispass){
76 node->_donethispass = 1;
84 if (viewer->SLERPing) {
92 tickFrac = (TickTime() - viewer->startSLERPtime)/viewer->transitionTime;
94 quaternion_slerp (&slerpedDiff,&viewer->startSLERPprepVPQuat,&viewer->prepVPQuat,tickFrac);
96 quaternion_togl(&slerpedDiff);
98 antipos.x = viewer->AntiPos.x * tickFrac + (viewer->startSLERPAntiPos.x * (1.0 - tickFrac));
99 antipos.y = viewer->AntiPos.y * tickFrac + (viewer->startSLERPAntiPos.y * (1.0 - tickFrac));
100 antipos.z = viewer->AntiPos.z * tickFrac + (viewer->startSLERPAntiPos.z * (1.0 - tickFrac));
102 FW_GL_TRANSLATE_D(-antipos.x, -antipos.y, -antipos.z);
110 vrmlrot_to_quaternion(&q3,node->orientation.c[0],node->orientation.c[1],node->orientation.c[2],-node->orientation.c[3]);
111 quaternion_togl(&q3);
113 FW_GL_TRANSLATE_D(-node->position.c[0],-node->position.c[1],-node->position.c[2]);
117 FW_GL_GETINTEGERV(GL_VIEWPORT, viewPort);
118 if(viewPort[2] > viewPort[3]) {
120 viewer->fieldofview = node->fieldOfView/3.1415926536*180;
122 a1 = node->fieldOfView;
123 a1 = atan2(sin(a1),viewPort[2]/((
float)viewPort[3]) * cos(a1));
124 viewer->fieldofview = a1/3.1415926536*180;
134 if (!renderstate()->render_vp)
return;
152 if((
struct X3D_Node*)node == getActiveLayerBoundViewpoint() && !node->_donethispass){
153 node->_donethispass = 1;
157 FW_GL_ROTATE_RADIANS(-node->orientation.c[3],node->orientation.c[0],node->orientation.c[1],
158 node->orientation.c[2]);
159 FW_GL_TRANSLATE_D(-node->position.c[0],-node->position.c[1],-node->position.c[2]);
162 if (node->fieldOfView.n == 4) {
163 for (ind=0; ind<4; ind++) {
164 Viewer()->orthoField[ind] = (double) node->fieldOfView.p[ind];
180 struct point_XYZ vpos, ax, cp, cp2, arcp;
181 static const struct point_XYZ orig = {0.0, 0.0, 0.0};
182 static const struct point_XYZ zvec = {0.0, 0.0, 1.0};
187 double len, len2, angle;
192 ax.x = node->axisOfRotation.c[0];
193 ax.y = node->axisOfRotation.c[1];
194 ax.z = node->axisOfRotation.c[2];
195 align = (APPROX(VECSQ(ax),0));
197 quaternion_to_vrmlrot(&(Viewer()->Quat),
198 &(viewer_orient.x), &(viewer_orient.y),
199 &(viewer_orient.z), &(viewer_orient.a));
203 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, mod);
205 FW_GL_GETDOUBLEV(GL_PROJECTION_MATRIX, proj);
206 FW_GLU_UNPROJECT(orig.x, orig.y, orig.z, mod, proj, viewport, &vpos.x, &vpos.y, &vpos.z);
211 matinverseAFFINE(modi,mod);
212 transform(&vpos,&orig,modi);
215 if (APPROX(len, 0)) {
return; }
216 VECSCALE(vpos, 1/sqrt(len));
219 ax.x = viewer_orient.x;
220 ax.y = viewer_orient.y;
221 ax.z = viewer_orient.z;
224 VECCP(ax, zvec, arcp);
226 if (APPROX(len, 0)) {
return; }
229 if (APPROX(len, 0)) {
return; }
230 VECSCALE(ax, 1/sqrt(len));
233 len = sqrt(VECSQ(cp));
234 if (APPROX(len, 0)) {
235 FW_GL_ROTATE_RADIANS(-viewer_orient.a, ax.x, ax.y, ax.z);
241 VECCP(cp, zvec, cp2);
243 len2 = VECPT(cp, zvec);
244 len = sqrt(VECSQ(cp2));
247 if (VECPT(cp, arcp) > 0) { sign = -1; }
else { sign = 1; }
248 angle = atan2(len2, sign*len);
250 FW_GL_ROTATE_RADIANS(angle, ax.x, ax.y, ax.z);
260 int nc = node->children.n;
268 printf(
"RENDER BILLBOARD START %d (%d)\n",node, nc);
273 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
276 normalChildren(node->children);
278 if (renderstate()->render_geom && (!renderstate()->render_blend)) {
283 printf(
"RENDER BILLBOARD END %d\n",node);
285 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
317 int nc = node->children.n;
321 if(renderstate()->render_collision) {
323 if((node->collide) && (node->enabled) && !(node->proxy)) {
326 OldCollisionInfo = *ci;
327 for(i=0; i<nc; i++) {
328 void *p = ((node->children).p[i]);
330 printf(
"RENDER COLLISION %d CHILD %d\n",node, p);
334 if((!APPROX(ci->Offset.x,
335 OldCollisionInfo.Offset.x)) ||
336 (!APPROX(ci->Offset.y,
337 OldCollisionInfo.Offset.y)) ||
338 (!APPROX(ci->Offset.z,
339 OldCollisionInfo.Offset.z))) {
346 node->__hit = (node->__hit & 1) ? 1 : 3;
348 node->__hit = (node->__hit & 1) ? 2 : 0;
352 POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->proxy,tmpN)
360 printf(
"RENDER COLLISIONCHILD START %d (%d)\n",node, nc);
365 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
368 normalChildren(node->children);
371 printf(
"RENDER COLLISIONCHILD END %d\n",node);
374 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
380 void child_LOD (
struct X3D_LOD *node) {
399 render_node(node->_selected);
404 void proximity_LOD (
struct X3D_LOD *node) {
409 int nran = (node->range).n;
410 int nnod = (node->level).n;
411 int xnod = (node->children).n;
418 if (nnod > 0) node->_selected = (node->children).p[0];
419 else node->_selected = NULL;
421 if (xnod > 0) node->_selected = (node->level).p[0];
422 else node->_selected = NULL;
428 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, mod);
432 FW_GL_GETDOUBLEV(GL_PROJECTION_MATRIX, proj);
433 FW_GLU_UNPROJECT(0,0,0,mod,proj,viewport, &vec.x,&vec.y,&vec.z);
441 matinverseAFFINE(modi,mod);
442 transform(&vec,&orig,modi);
446 vec.x -= (node->center).c[0];
447 vec.y -= (node->center).c[1];
448 vec.z -= (node->center).c[2];
450 dist = sqrt(VECSQ(vec));
454 if(dist < ((node->range).p[i])) {
break; }
462 if(i >= xnod) i = xnod-1;
463 node->_selected = (node->children).p[i];
465 }
else node->_selected = NULL;
470 if(i >= nnod) i = nnod-1;
471 node->_selected = (node->level).p[i];
473 }
else { node->_selected = NULL; }
484 void add_node_to_broto_context(
struct X3D_Proto *currentContext,
struct X3D_Node *node);
490 if (node->__proxNode == NULL) {
493 if(node->_executionContext)
494 add_node_to_broto_context(X3D_PROTO(node->_executionContext),X3D_NODE(pn));
497 node->__proxNode = (
void *)pn;
500 ADD_PARENT(X3D_NODE(pn),X3D_NODE(node));
504 pn = X3D_PROXIMITYSENSOR(node->__proxNode);
507 memcpy (&pn->center, &node->center, sizeof (
float)*3);
508 memcpy (&pn->size, &node->size, sizeof (
float)*3);
530 if (renderstate()->render_proximity) {
531 if (node->__proxNode != NULL) {
533 render_node(X3D_NODE(node->__proxNode));
540 if (!renderstate()->render_vp)
return;
543 for(i=0; i<node->children.n; i++) {
544 struct X3D_Node *p = X3D_NODE(node->children.p[i]);