*/}}

la_tns_drivers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "../la_5.h"
  2. extern LA MAIN;
  3. extern struct _tnsMain *T;
  4. laBaseNodeType TNS_IDN_TRANSFORM;
  5. laBaseNodeType TNS_IDN_MAKE_TRANSFORM;
  6. laPropContainer* TNS_PC_IDN_TRANSFORM;
  7. laPropContainer* TNS_PC_IDN_MAKE_TRANSFORM;
  8. void IDN_TransformInit(tnsTransformNode* n){
  9. n->Mat=laCreateInSocket("MAT",0); strSafeSet(&n->Base.Name,"Transform");
  10. }
  11. void IDN_TransformDestroy(tnsTransformNode* n){
  12. laDestroyInSocket(n->Mat); strSafeDestroy(&n->Base.Name);
  13. }
  14. int IDN_TransformVisit(tnsTransformNode* n, laListHandle* l){
  15. if(LA_SRC_AND_PARENT(n->Mat)){ laBaseNode*bn=n->Mat->Source->Parent; LA_VISIT_NODE(bn); }
  16. n->Base.Eval=LA_DAG_FLAG_PERM;
  17. lstAppendPointer(l, n);
  18. return LA_DAG_FLAG_PERM;
  19. }
  20. int IDN_TransformEval(tnsTransformNode* n){
  21. if(!n->Target) return 0;
  22. if((!n->Mat->Source) || (n->Mat->Source->DataType!=(LA_PROP_FLOAT|LA_PROP_ARRAY)) || (n->Mat->Source->ArrLen!=16)){
  23. tnsLoadIdentity44d(&n->Target->DeltaTransform); tnsSelfMatrixChanged(n->Target, 1);
  24. }else{
  25. memcpy(n->Target->DeltaTransform, n->Mat->Source->Data, sizeof(tnsMatrix44d)); tnsSelfMatrixChanged(n->Target, 1);
  26. }
  27. laNotifyInstanceUsers(n->Target);
  28. return 1;
  29. }
  30. void tnsui_TransformNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  31. laColumn* c=laFirstColumn(uil); tnsTransformNode*n=This->EndInstance;
  32. LA_BASE_NODE_HEADER(uil,c,This);
  33. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  34. laUiItem* b=laBeginRow(uil,cl,0,0);
  35. laShowNodeSocket(uil,cl,This,"mat",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  36. laEndRow(uil,b);
  37. laShowItem(uil,cr,This,"target");
  38. }
  39. void IDN_MakeTransformInit(tnsMakeTransformNode* n){
  40. n->Out=laCreateOutSocket(n,"MAT",0); strSafeSet(&n->Base.Name,"Make Transform");
  41. n->Loc=laCreateInSocket("LOC",0);
  42. n->Rot=laCreateInSocket("ROT",0); n->Angle=laCreateInSocket("ANGLE",0); n->UseRot[2]=1;
  43. n->Sca=laCreateInSocket("SCALE",0); n->UseSca=1;
  44. }
  45. void IDN_MakeTransformDestroy(tnsMakeTransformNode* n){
  46. laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name);
  47. laDestroyInSocket(n->Loc);
  48. laDestroyInSocket(n->Rot); laDestroyInSocket(n->Angle);
  49. laDestroyInSocket(n->Sca);
  50. }
  51. int IDN_MakeTransformVisit(tnsMakeTransformNode* n, laListHandle* l){
  52. LA_GUARD_THIS_NODE(n);
  53. if(LA_SRC_AND_PARENT(n->Loc)){ laBaseNode*bn=n->Loc->Source->Parent; LA_VISIT_NODE(bn); }
  54. if(LA_SRC_AND_PARENT(n->Rot)){ laBaseNode*bn=n->Rot->Source->Parent; LA_VISIT_NODE(bn); }
  55. if(LA_SRC_AND_PARENT(n->Sca)){ laBaseNode*bn=n->Sca->Source->Parent; LA_VISIT_NODE(bn); }
  56. if(LA_SRC_AND_PARENT(n->Angle)){ laBaseNode*bn=n->Angle->Source->Parent; LA_VISIT_NODE(bn); }
  57. n->Base.Eval=LA_DAG_FLAG_PERM; lstAppendPointer(l, n);
  58. return LA_DAG_FLAG_PERM;
  59. }
  60. int IDN_MakeTransformEval(tnsMakeTransformNode* n){
  61. tnsMatrix44d mm,mt,mr,ms; real* UseT,*UseR,*UseA,*UseS; tnsVector3d nr;
  62. if(LA_SRC_AND_PARENT(n->Loc) && n->Loc->Source->ArrLen>=3 && n->Loc->Source->DataType==LA_PROP_FLOAT|LA_PROP_ARRAY){ UseT=n->Loc->Source->Data; }else{ UseT=n->UseLoc; }
  63. if(LA_SRC_AND_PARENT(n->Rot) && n->Rot->Source->ArrLen>=3 && n->Rot->Source->DataType==LA_PROP_FLOAT|LA_PROP_ARRAY){ UseR=n->Rot->Source->Data; }else{ UseR=n->UseRot; }
  64. if(LA_SRC_AND_PARENT(n->Angle) && n->Angle->Source->DataType&LA_PROP_FLOAT){ UseA=n->Angle->Source->Data; }else{ UseA=&n->UseAngle; }
  65. if(LA_SRC_AND_PARENT(n->Sca) && n->Sca->Source->DataType&LA_PROP_FLOAT){ UseS=n->Sca->Source->Data; }else{ UseS=&n->UseSca; }
  66. tnsMakeTranslationMatrix44d(mt,LA_COLOR3(UseT));
  67. tnsNormalize3d(nr, UseR); tnsMakeRotationMatrix44d(mr,*UseA, LA_COLOR3(nr));
  68. tnsMakeScaleMatrix44d(ms,*UseS,*UseS,*UseS);
  69. tnsMultiply44d(mm,mt,mr); tnsMultiply44d(n->Mat,mm,ms);
  70. n->Out->ArrLen=16; n->Out->Data=n->Mat; n->Out->DataType=LA_PROP_FLOAT|LA_PROP_ARRAY;
  71. return 1;
  72. }
  73. void tnsui_MakeTransformNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  74. laColumn* c=laFirstColumn(uil); tnsMakeTransformNode*n=This->EndInstance;
  75. laUiItem* b2, *rui;
  76. LA_BASE_NODE_HEADER(uil,c,This);
  77. laUiItem* b=laBeginRow(uil,c,0,0);
  78. laShowNodeSocket(uil,c,This,"loc",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"loc.source")));{
  79. laShowItem(uil,c,This,"use_loc")->Expand=1;
  80. }laEndCondition(uil,b2);
  81. laEndRow(uil,b);
  82. b=laBeginRow(uil,c,0,0);
  83. laShowNodeSocket(uil,c,This,"rot",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"rot.source")));{
  84. laShowItem(uil,c,This,"use_rot")->Expand=1;
  85. }laEndCondition(uil,b2);
  86. laEndRow(uil,b);
  87. b=laBeginRow(uil,c,0,0);
  88. laShowNodeSocket(uil,c,This,"angle",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"angle.source")));{
  89. laShowItem(uil,c,This,"use_angle")->Expand=1;
  90. }laEndCondition(uil,b2);
  91. laEndRow(uil,b);
  92. b=laBeginRow(uil,c,0,0);
  93. laShowNodeSocket(uil,c,This,"sca",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"sca.source")));{
  94. laShowItem(uil,c,This,"use_sca")->Expand=1;
  95. }laEndCondition(uil,b2);
  96. laEndRow(uil,b);
  97. b=laBeginRow(uil,c,0,0);
  98. laShowSeparator(uil,c)->Expand=1;;
  99. laShowNodeSocket(uil,c,This,"out",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  100. laEndRow(uil,b);
  101. }
  102. int OPINV_AddDriverPage(laOperator* a, laEvent *e){
  103. laRackPage* dp=memAcquire(sizeof(laRackPage));
  104. strSafeSet(&dp->Name,"New Page");
  105. lstAppendItem(&MAIN.Drivers->Pages, dp); MAIN.Drivers->CurrentPage=dp;
  106. laNotifyUsers("la.drivers.pages"); laRecordAndPush(0,"la.drivers","Add page", 0);
  107. return LA_FINISHED;
  108. }
  109. int OPINV_RebuildDrivers(laOperator* a, laEvent *e){
  110. laDriverRequestRebuild();
  111. return LA_FINISHED;
  112. }
  113. tnsObject* tnsget_FirstObject(void* unused, void* unused2){
  114. return T->World.AllObjects.pFirst;
  115. }
  116. void tns_RegisterNodes(){
  117. laPropContainer *pc; laProp *p;
  118. laOperatorType *at;
  119. laEnumProp *ep;
  120. laCreateOperatorType("LA_add_driver_page", "New Page", "Add a driver page", 0,0,0,OPINV_AddDriverPage,0,'+',0);
  121. laCreateOperatorType("LA_driver_rebuild", "Rebuild Drivers", "Rebuild drivers for evaluation",0,0,0,OPINV_RebuildDrivers,0,L'⭮',0);
  122. pc=laAddPropertyContainer("tns_transform_node", "Transform", "Transform objects",0,tnsui_TransformNode,sizeof(tnsTransformNode),0,0,1);
  123. TNS_PC_IDN_TRANSFORM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  124. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  125. laAddSubGroup(pc,"mat", "Mat","Input matrix","la_in_socket",0,0,0,offsetof(tnsTransformNode,Mat),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  126. laAddSubGroup(pc,"target", "Target","Target object","tns_object",0,LA_WIDGET_COLLECTION_SELECTOR,laui_IdentifierOnly,offsetof(tnsTransformNode,Target),tnsget_FirstObject,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  127. pc=laAddPropertyContainer("tns_make_transform_node", "Make Transform", "Make Transform matrix",0,tnsui_MakeTransformNode,sizeof(tnsMakeTransformNode),0,0,1);
  128. TNS_PC_IDN_MAKE_TRANSFORM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  129. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  130. laAddSubGroup(pc,"out", "Out","Output matrix","la_out_socket",0,0,0,offsetof(tnsMakeTransformNode,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  131. laAddSubGroup(pc,"loc", "Location","Location","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Loc),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  132. laAddSubGroup(pc,"rot", "Rotation","Rotation","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Rot),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  133. laAddSubGroup(pc,"sca", "Scale","Scale","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Sca),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  134. laAddSubGroup(pc,"angle", "Angle","Rotation Angle","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Angle),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  135. laAddFloatProperty(pc,"use_loc", "Location", "Use Location",0,"X,Y,Z",0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseLoc),0,0,3,0,0,0,0,0,0,0,0);
  136. laAddFloatProperty(pc,"use_rot", "Rotation", "Use Rotation",0,"X,Y,Z",0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseRot),0,0,3,0,0,0,0,0,0,0,0);
  137. laAddFloatProperty(pc,"use_sca", "Scale", "Use Scale",0,0,0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseSca),0,0,0,0,0,0,0,0,0,0,0);
  138. laAddFloatProperty(pc,"use_angle", "Angle", "Use Angle",0,0,0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseAngle),0,0,0,0,0,0,0,0,0,0,0);
  139. LA_IDN_REGISTER("Transform",0,TNS_IDN_TRANSFORM,TNS_PC_IDN_TRANSFORM, IDN_TransformInit, IDN_TransformDestroy, IDN_TransformVisit, IDN_TransformEval, tnsTransformNode);
  140. LA_IDN_REGISTER("Make Transform",0,TNS_IDN_MAKE_TRANSFORM,TNS_PC_IDN_MAKE_TRANSFORM, IDN_MakeTransformInit, IDN_MakeTransformDestroy, IDN_MakeTransformVisit, IDN_MakeTransformEval, tnsMakeTransformNode);
  141. LA_NODE_CATEGORY_DRIVER=laAddNodeCategory("Driver",0,LA_RACK_TYPE_DRIVER);
  142. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_MATH, &TNS_IDN_MAKE_TRANSFORM,0);
  143. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_DRIVER, &TNS_IDN_TRANSFORM,0);
  144. }
  145. void laDriverRequestRebuild(){ MAIN.Drivers->NeedRebuild=1; }
  146. void laDriverRequestEval(){ MAIN.Drivers->NeedEval=1; }
  147. int la_RunDrivers(){
  148. MAIN.Drivers->NeedEval = 0;
  149. for(laListItemPointer*lip=MAIN.Drivers->Eval.pFirst;lip;lip=lip->pNext){
  150. laBaseNode* n=lip->p; n->Type->Eval(n);
  151. }
  152. return 1;
  153. }
  154. int la_RebuildDriverEval(){
  155. MAIN.Drivers->NeedRebuild = 0;
  156. while(lstPopPointer(&MAIN.Drivers->Eval));
  157. laListHandle pending={0};
  158. for(laRackPage* dp=MAIN.Drivers->Pages.pFirst;dp;dp=dp->Item.pNext){
  159. for(laNodeRack* ir=dp->Racks.pFirst;ir;ir=ir->Item.pNext){
  160. for(laBaseNode*bn=ir->Nodes.pFirst;bn;bn=bn->Item.pNext){ lstAppendPointer(&pending,bn); bn->Eval=0; }
  161. }
  162. }
  163. laBaseNode*n;int result=LA_DAG_FLAG_PERM; laListItemPointer*NextLip;
  164. for(laListItemPointer*lip=pending.pFirst;lip;lip=NextLip){ n=lip->p; NextLip=lip->pNext;
  165. if(n->Eval&LA_DAG_FLAG_PERM) continue;
  166. result=n->Type->Visit(n,&MAIN.Drivers->Eval); if(result==LA_DAG_FLAG_ERR){ while(lstPopPointer(&pending)); break; }
  167. }
  168. if(result==LA_DAG_FLAG_ERR){ while(lstPopPointer(&MAIN.Drivers->Eval)); return LA_DAG_FLAG_ERR; }
  169. return LA_DAG_FLAG_PERM;
  170. }