35 #include "../vrml_parser/Structs.h"
36 #include "../vrml_parser/CRoutes.h"
37 #include "../main/headers.h"
39 #include "../input/EAIHeaders.h"
40 #include "../input/EAIHelpers.h"
41 #include "../opengl/Frustum.h"
42 #include "../opengl/OpenGL_Utils.h"
43 #include "../opengl/Textures.h"
45 #include "Component_Networking.h"
47 #include "../scenegraph/RenderFuncs.h"
49 #include <libFreeWRL.h>
56 #define TRACK_OSC_MSG 0
62 #include "../vrml_parser/CParseGeneral.h"
63 #include "../scenegraph/Vector.h"
64 #include "../vrml_parser/CFieldDecls.h"
65 #include "../world_script/JScript.h"
66 #include "../world_script/CScripts.h"
67 #include "../world_script/fieldSet.h"
68 #include "../vrml_parser/CParseParser.h"
69 #include "../vrml_parser/CParseLexer.h"
70 #include "../vrml_parser/CParse.h"
78 void error(
int num,
const char *m,
const char *path);
79 void utilOSCcounts(
char *types ,
int *intCount,
int *fltCount,
int *strCount,
int *blobCount,
int *midiCount,
int *otherCount);
82 #include "OSCcallbacks.c"
85 #define MAX_OSC_SERVERS 32
86 int serverPort[MAX_OSC_SERVERS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
87 lo_server_thread oscThread[MAX_OSC_SERVERS] ;
89 static uintptr_t *OSC_Nodes = NULL;
90 static int num_OSC_Nodes = 0;
91 int curr_OSC_Node = 0;
92 int active_OSC_Nodes = FALSE;
94 void utilOSCcounts(
char *types ,
int *intCount,
int *fltCount,
int *strCount,
int *blobCount,
int *midiCount,
int *otherCount) {
106 for (i=0 ; i < j ; i++) {
130 void activate_OSCsensors() {
132 active_OSC_Nodes = TRUE ;
144 while (active_OSC_Nodes && curr_OSC_Node < num_OSC_Nodes) {
146 if (checkNode(realnode,__FILE__,__LINE__)) {
148 printf(
"activate_OSCsensors : %s,%d node=%p name=%s\n", __FILE__,__LINE__,realnode,realnode->description->strptr) ;
150 if (realnode->_status < 0) {
151 printf(
"activate_OSCsensors : %s,%d Moving %s to ready.\n", __FILE__,__LINE__,realnode->description->strptr) ;
152 realnode->_status = 0 ;
153 }
else if (realnode->_status == 0) {
154 printf(
"activate_OSCsensors : %s,%d\n", __FILE__,__LINE__) ;
155 printf(
"activate_OSCsensors : enabled=%d\n", realnode->enabled) ;
156 printf(
"activate_OSCsensors : gotEvents=%d\n", realnode->gotEvents) ;
157 printf(
"activate_OSCsensors : description=%s\n",realnode->description->strptr) ;
158 printf(
"activate_OSCsensors : protocol=%s\n", realnode->protocol->strptr) ;
159 printf(
"activate_OSCsensors : port=%d\n", realnode->port) ;
160 printf(
"activate_OSCsensors : filter=%s\n", realnode->filter->strptr) ;
161 printf(
"activate_OSCsensors : handler=%s\n", realnode->handler->strptr) ;
179 printf(
"activate_OSCsensors : talksTo=[ ");
180 for (i=0; i < realnode->talksTo.n; i++) {
181 printf(
"\"%s\" ",realnode->talksTo.p[i]->strptr);
185 myNode = parser_getNodeFromName(realnode->talksTo.p[i]->strptr);
186 if (myNode != NULL) {
187 printf(
"(%p) ",(
void *)myNode);
192 printf(
"] (%d nodes) (Need to fix %s,%d)\n",realnode->talksTo.n , __FILE__,__LINE__);
193 printf(
"activate_OSCsensors : listenfor=%s , expect %d parameters\n", realnode->listenfor->strptr , (
int)strlen(realnode->listenfor->strptr)) ;
194 printf(
"activate_OSCsensors : FIFOsize=%d\n", realnode->FIFOsize) ;
195 printf(
"activate_OSCsensors : _status=%d\n", realnode->_status) ;
197 if (realnode->FIFOsize > 0) {
199 utilOSCcounts(realnode->listenfor->strptr,&intCount,&fltCount,&strCount,&blobCount,&midiCount,&otherCount);
200 intCount = realnode->FIFOsize * (intCount + midiCount);
201 fltCount = realnode->FIFOsize * fltCount;
202 strCount = realnode->FIFOsize * (strCount + blobCount + otherCount);
203 printf(
"Allocate %d floats, %d ints for '%s'\n",fltCount,intCount,realnode->description->strptr);
205 realnode->_int32InpFIFO = (
void *) NewRingBuffer (intCount) ;
206 realnode->_floatInpFIFO = (
void *) NewRingBuffer (fltCount) ;
207 realnode->_stringInpFIFO = (
void *) NewRingBuffer (strCount) ;
212 int foundCurrentPort = -1 ;
213 for ( i=0 ; i < num_OSC_Nodes ; i++) {
214 if(realnode->port == serverPort[i]) {
219 if (foundCurrentPort < 0) {
220 foundCurrentPort = serverCount ;
221 serverPort[foundCurrentPort] = realnode->port ;
224 sprintf (buf,
"%d",realnode->port);
226 if (strcmp(
"TCP",realnode->protocol->strptr)==0) {
228 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
229 }
else if (strcmp(
"UNIX",realnode->protocol->strptr)==0) {
231 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
234 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
236 lo_server_thread_start(oscThread[foundCurrentPort]);
239 printf(
"%d servers; current server is running in slot %d on port %d\n",serverCount,foundCurrentPort,serverPort[foundCurrentPort]) ;
244 int foundHandler = 0 ;
245 for (i=0 ; i < OSCfuncCount ; i++) {
246 printf(
"%d/%d : Check %s against %s\n",i,OSCfuncCount,realnode->handler->strptr,OSCfuncNames[i]);
247 if (0 == strcmp(realnode->handler->strptr,OSCfuncNames[i])) {foundHandler = i;}
249 if (OSCcallbacks[foundHandler] != NULL) {
250 printf(
"Going to hook '%s' to '%s' handler\n",realnode->description->strptr ,OSCfuncNames[foundHandler]) ;
251 lo_server_thread_add_method(oscThread[foundCurrentPort], realnode->filter->strptr, realnode->listenfor->strptr,
252 (OSCcallbacks[foundHandler]), realnode);
255 realnode->_status = 1 ;
257 active_OSC_Nodes = FALSE ;
265 void error(
int num,
const char *msg,
const char *path)
267 printf(
"liblo server error %d in path %s: %s\n", num, path, msg);
270 void add_OSCsensor(
struct X3D_Node * node) {
274 printf (
"error in registerOSCNode; somehow the node datastructure is zero \n");
278 if (node->_nodeType != NODE_OSC_Sensor)
return;
280 OSC_Nodes = (uintptr_t *) REALLOC (OSC_Nodes,
sizeof (uintptr_t *) * (num_OSC_Nodes+1));
284 *myptr = (uintptr_t) node;
288 void remove_OSCsensor(
struct X3D_Node * node) {}
291 void add_OSCsensor(
struct X3D_Node * node) {}
292 void remove_OSCsensor(
struct X3D_Node * node) {}
296 int loadstatus_Script(
struct X3D_Script *script);
306 if (node->__oldEnabled != node->enabled) {
307 node->__oldEnabled = node->enabled;
308 MARK_EVENT(X3D_NODE(node),offsetof (
struct X3D_LoadSensor, enabled));
310 if (!node->enabled)
return;
314 if (!renderstate()->render_sensitive)
return;
317 if NODE_NEEDS_COMPILING {
320 node->__finishedloading = 0;
321 node->progress = (float) 0.0;
322 node->__StartLoadTime = 0.0;
326 if (node->watchList.n<=0)
return;
329 if (node->__finishedloading == node->watchList.n)
return;
337 for (count = 0; count < node->watchList.n; count ++) {
339 cnode = node->watchList.p[count];
342 switch (cnode->_nodeType) {
343 case NODE_ImageTexture:
350 if (fwl_isTextureLoaded(tnode->__textureTableIndex)) {
360 if(inode->__loadstatus > INLINE_INITIAL_STATE && inode->__loadstatus < INLINE_STABLE)
362 if(inode->__loadstatus == INLINE_STABLE)
369 if(loadstatus_Script(X3D_SCRIPT(cnode)))
373 case NODE_ShaderProgram:
376 shader=(
struct Shader_Script *)(X3D_SHADERPROGRAM(cnode)->_shaderUserDefinedFields);
377 if(shader->loaded) nowFinished++;
380 case NODE_PackagedShader:
383 shader=(
struct Shader_Script *)(X3D_PACKAGEDSHADER(cnode)->_shaderUserDefinedFields);
384 if(shader->loaded) nowFinished++;
387 case NODE_ComposedShader:
390 shader=(
struct Shader_Script *)(X3D_COMPOSEDSHADER(cnode)->_shaderUserDefinedFields);
391 if(shader->loaded) nowFinished++;
398 shader=(
struct Shader_Script *)(X3D_EFFECT(cnode)->_shaderUserDefinedFields);
399 if(shader->loaded) nowFinished++;
403 case NODE_MovieTexture:
410 istate = loadstatus_AudioClip(anode);
424 if (nowFinished == node->watchList.n) {
426 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
429 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isLoaded));
431 node->progress = (float) 1.0;
432 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, progress));
434 node->loadTime = TickTime();
435 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, loadTime));
439 if ((nowLoading > 0) && (node->__loading == 0)) {
442 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
445 node->__StartLoadTime = TickTime();
449 if (node->isActive == 1) {
450 node->progress = (float)(nowFinished)/(float)(node->watchList.n);
451 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, progress));
455 node->__loading = nowLoading;
456 node->__finishedloading = nowFinished;
459 if (node->timeOut > 0.0001) {
460 if (node->__StartLoadTime > 0.001) {
463 if ((TickTime() - node->__StartLoadTime) > node->timeOut) {
465 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isLoaded));
468 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
471 node->__finishedloading = node->watchList.n;
479 int nc = (node->children).n;
491 printf(
"RENDER ANCHOR START %d (%d)\n",node, nc);
496 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
499 normalChildren(node->children);
502 printf(
"RENDER ANCHOR END %d\n",node);
504 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
508 struct X3D_Node *broto_search_DEFname(
struct X3D_Proto *context,
const char *name);
512 struct X3D_Node * broto_search_ALLnames(
struct X3D_Proto *context,
char *name,
int *source){
524 node = broto_search_DEFname(context,name);
528 im = broto_search_IMPORTname(context,name);
533 nlinenode = broto_search_DEFname(context,im->inlinename);
534 if(nlinenode && nlinenode->_nodeType == NODE_Inline ){
535 struct X3D_Inline *nline = X3D_INLINE(nlinenode);
536 if(nline->__loadstatus == INLINE_IMPORTING || nline->__loadstatus == INLINE_STABLE){
538 struct IMEXPORT *ex = broto_search_EXPORTname(X3D_PROTO(nline),im->mxname);
547 node = broto_search_DEFname(X3D_PROTO(nline),ex->mxname);
565 struct X3D_Node* newnodef, *newnodet;
566 int source, type, kind, ifield;
570 newnodef = route->from.node;
571 newnodet = route->to.node;
572 if(route->from.weak){
574 newnodef = broto_search_ALLnames(context,route->from.cnode,&source);
575 ic = newnodef != route->from.node;
576 changed = changed || ic;
578 route->from.weak = 3;
579 getFieldFromNodeAndName(newnodef,route->from.cfield,&type,&kind,&ifield,&value);
580 if(ifield < 0) ConsoleMessage(
"bad FROM field ROUTE %s.%s TO %s.%s\n",route->from.cnode,route->from.cfield,route->to.cnode,route->to.cfield);
581 route->from.ifield = ifield;
582 route->from.ftype = type;
585 else route->from.weak = 1;
589 newnodet = broto_search_ALLnames(context,route->to.cnode,&source);
590 ic = newnodet != route->to.node;
591 changed = changed || ic;
594 getFieldFromNodeAndName(newnodet,route->to.cfield,&type,&kind,&ifield,&value);
596 ConsoleMessage(
"bad TO field ROUTE %s.%s TO %s.%s\n",route->from.cnode,route->from.cfield,route->to.cnode,route->to.cfield);
597 route->to.ifield = ifield;
598 route->to.ftype = type;
601 else route->to.weak = 1;
604 if(route->lastCommand){
606 CRoutes_RemoveSimpleB(route->from.node,route->from.ifield,route->to.node,route->to.ifield,route->ft);
607 route->lastCommand = 0;
609 route->from.node = newnodef;
610 route->to.node = newnodet;
611 if(route->from.node && route->to.node && route->from.ifield > -1 && route->to.ifield > -1){
612 route->lastCommand = 1;
613 CRoutes_RegisterSimpleB(route->from.node,route->from.ifield,route->to.node,route->to.ifield,route->ft);
617 void update_weakRoutes(
struct X3D_Proto *context){
627 if(context && context->__ROUTES){
633 for(k=0;k<vectorSize(context->__ROUTES);k++){
635 if(route->from.weak || route->to.weak){
636 update_weakRoute(context,route);
644 int unload_broto(
struct X3D_Proto* node);
652 switch (node->__loadstatus) {
653 case INLINE_INITIAL_STATE:
655 if (node->url.n == 0) {
656 node->__loadstatus = INLINE_STABLE;
658 res = resource_create_multi(&(node->url));
659 res->media_type = resm_unknown;
660 node->__loadstatus = INLINE_REQUEST_RESOURCE;
661 node->__loadResource = res;
666 case INLINE_REQUEST_RESOURCE:
667 res = node->__loadResource;
668 resource_identify(node->_parentResource, res);
671 res->actions = resa_download | resa_load;
673 resitem_enqueue(ml_new(res));
675 node->__loadstatus = INLINE_FETCHING_RESOURCE;
678 case INLINE_FETCHING_RESOURCE:
679 res = node->__loadResource;
683 if (res->status == ress_loaded) {
685 res->ectx = (
void*)node;
686 res->whereToPlaceData = X3D_NODE(node);
687 res->offsetFromWhereToPlaceData = offsetof (
struct X3D_Inline, __children);
688 res->actions = resa_process;
689 node->__loadstatus = INLINE_PARSING;
691 node->__protoFlags = ciflag_set(node->__protoFlags,1,0);
692 res->complete = FALSE;
695 resitem_enqueue(ml_new(res));
697 }
else if ((res->status == ress_failed) || (res->status == ress_invalid)) {
700 node->__loadstatus = INLINE_STABLE;
706 res = node->__loadResource;
711 if (res->status == ress_parsed) {
713 node->__loadstatus = INLINE_IMPORTING;
718 case INLINE_IMPORTING:
720 context = hasContext(node->_executionContext);
722 update_weakRoutes(context);
723 node->__loadstatus = INLINE_STABLE;
728 node->__loadstatus = INLINE_UN_IMPORTING;
732 case INLINE_UN_IMPORTING:
734 context = hasContext(node->_executionContext);
736 update_weakRoutes(context);
737 node->__loadstatus = INLINE_UNLOADING;
739 case INLINE_UNLOADING:
764 unload_broto(X3D_PROTO(node));
765 node->__loadstatus = INLINE_INITIAL_STATE;
774 if(0)printf(
"in prep_inline\n");
777 if ((node->__loadstatus != INLINE_STABLE && node->load) || (node->__loadstatus != INLINE_INITIAL_STATE && !node->load)) {
784 void compile_Inline(
struct X3D_Inline *node) {
785 if(0)printf(
"in compile_inline\n");
789 REINITIALIZE_SORTED_NODES_FIELD(node->__children,node->_sortedChildren);
792 int loadchanged, urlchanged;
797 loadchanged = urlchanged = 0;
798 loadchanged = node->load != node->__oldload;
799 urlchanged = node->url.n != node->__oldurl.n || node->url.p != node->__oldurl.p;
800 if(loadchanged || urlchanged){
802 node->__loadstatus = INLINE_UN_IMPORTING;
803 if(loadchanged) node->__oldload = node->load;
804 if(urlchanged) node->__oldurl = node->url;
819 RETURN_FROM_CHILD_IF_NOT_FOR_ME
821 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
824 normalChildren(node->_sortedChildren);
825 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);