*/}}
Browse Source

Kinda ok for active camera

YimingWu 2 months ago
parent
commit
3a0d451a16
4 changed files with 63 additions and 26 deletions
  1. 33 21
      la_tns_kernel.c
  2. 4 0
      resources/la_modelling.c
  3. 13 1
      resources/la_properties.c
  4. 13 4
      resources/la_widgets_viewers.c

+ 33 - 21
la_tns_kernel.c

@@ -3641,6 +3641,7 @@ tnsCamera *tnsCreateCamera(tnsObject *under, char *Name, real FOV,
     c->ZMax = 200.0f;
     c->FocusDistance = FocusDistance;
     c->OrthScale = 1.0f;
+    if(under && under->Type==TNS_OBJECT_ROOT){ memAssignRef(under,&((tnsRootObject*)under)->ActiveCamera,c); }
 
     return c;
 }
@@ -3813,39 +3814,26 @@ void tnsLookAt(tnsObject *o, tnsVector3d Target, tnsVector3d Up){
     }
 }
 
+
 void tnsDrawCamera(tnsObject *o){
     tnsCamera *c = o;
-    real fov_2 = c->FOV / 2;
-    real ex, ey, ez; ey = 10 * sin(fov_2); ex = ey; ez = 10 * cos(fov_2);
-
-    tnsColor4d(1, 1, 1, 1);
+    real fov_2 = c->FOV / 2; real len=1;
+    real ex, ey, ez; ey = len * sin(fov_2); ex = ey; ez = len * cos(fov_2);
 
     tnsVertex3d(ex, ey, -ez); tnsVertex3d(ex, -ey, -ez); tnsVertex3d(0.0, 0.0, 0.0);
     tnsVertex3d(ex, ey, -ez); tnsVertex3d(-ex, ey, -ez); tnsVertex3d(0.0, 0.0, 0.0);
     tnsVertex3d(-ex, -ey, -ez); tnsVertex3d(-ex, ey, -ez);
-
     tnsPackAs(GL_LINE_STRIP);
-    tnsFlush();
+    tnsVertex3d(ex,ey*1.25,-ez); tnsVertex3d(0,ey*2,-ez); tnsVertex3d(-ex,ey*1.25,-ez); 
+    tnsPackAs(GL_LINE_LOOP);
+    tnsVertex3d(-ex, -ey, -ez); tnsVertex3d(ex, -ey, -ez);
+    tnsPackAs(GL_LINES);
 }
 void tnsDrawCross(real x,real y,real z,real x1,real x2,real y1,real y2,real z1,real z2){
     tnsVertex3d(x+x1, y+0.0, z+0.0); tnsVertex3d(x+x2, y+0.0, z+0.0);
     tnsVertex3d(x+0.0,y+ y1, z+0.0); tnsVertex3d(x+0.0,y+ y2, z+0.0);
     tnsVertex3d(x+0.0,y+ 0.0,z+ z1); tnsVertex3d(x+0.0,y+ 0.0,z+ z2);
 }
-void tnsDrawPlaceholder(tnsObject* o, tnsEvaluateData* ed){
-    if(T->BindedShader==T->SelectionShader){
-        int i=o->SelectID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i); tnsColor4dv(color);
-    }else{
-        if(ed->FillOutline && (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED))) return;
-        if(!ed->FillOutline && (o->Flags&TNS_OBJECT_FLAGS_SELECTED)) return;
-        if(o==ed->Active){ tnsColor4dv(laAccentColor(LA_BT_TEXT_ACTIVE)); }
-        elif(o->Flags&TNS_OBJECT_FLAGS_SELECTED){ tnsColor4dv(laAccentColor(LA_BT_NORMAL)); }
-        else tnsColor4dv(laThemeColor(_LA_THEME_3D_VIEW,LA_BT_BORDER));
-    }
-    tnsDrawCross(0,0,0,-0.2,1,-0.2,1,-0.2,1);
-    tnsPackAs(GL_LINES);
-    tnsFlush();
-}
 
 void tnsDrawEmptySelectionID(tnsEvaluatedInstance* ei, void* unused){
     int i=ei->InstanceSelectionID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i); tnsColor4dv(color);
@@ -3860,6 +3848,7 @@ void tnsDrawEmptyObject(tnsEvaluatedInstance* ei, void* unused){
     tnsDrawCross(0,0,0,-0.2,1,-0.2,1,-0.2,1);tnsLineWidth(3); tnsPackAs(GL_LINES); tnsLineWidth(1); tnsFlush();
 }
 void tnsEvaluateEmptyObject(tnsObject* o, tnsEvaluateData* ed){
+    if(ed->SceneEvaluateMode){ return; }
     tnsAddEvaluatedInstance(ed,o,tnsDrawEmptyObject,TNS_EVAL_LAYER_SOLID,0,0,0);
     if(ed->FillOutline && o->Flags&TNS_OBJECT_FLAGS_SELECTED){
         tnsAddEvaluatedInstance(ed,o,tnsDrawEmptyOutline,TNS_EVAL_LAYER_OUTLINE,ed->Active==o,0,0);
@@ -3908,6 +3897,29 @@ eval_inst_cleanup:
     ed->OverrideID=origid; ed->FillOutline=origoutline;
 }
 
+void tnsDrawCameraSelectionID(tnsEvaluatedInstance* ei, void* unused){
+    int i=ei->InstanceSelectionID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i); tnsColor4dv(color);
+    tnsDrawCamera(ei->Object); tnsFlush();
+}
+void tnsDrawCameraOutline(tnsEvaluatedInstance* ei, void* unused){
+    real* color=(ei->IsActive)?laAccentColor(LA_BT_TEXT):laAccentColor(LA_BT_NORMAL); tnsColor4dv(color); tnsLineWidth(3);
+    tnsDrawCamera(ei->Object); tnsLineWidth(1); tnsFlush();
+}
+void tnsDrawCameraObject(tnsEvaluatedInstance* ei, void* unused){
+    tnsColor4dv(laThemeColor(_LA_THEME_3D_VIEW,LA_BT_BORDER)); tnsLineWidth(3);
+    tnsDrawCamera(ei->Object); tnsLineWidth(1); tnsFlush();
+}
+void tnsEvaluateCameraObject(tnsObject* o, tnsEvaluateData* ed){
+    if(ed->SceneEvaluateMode){ return; }
+    tnsAddEvaluatedInstance(ed,o,tnsDrawCameraObject,TNS_EVAL_LAYER_SOLID,0,0,0);
+    if(ed->FillOutline && o->Flags&TNS_OBJECT_FLAGS_SELECTED){
+        tnsAddEvaluatedInstance(ed,o,tnsDrawCameraOutline,TNS_EVAL_LAYER_OUTLINE,ed->Active==o,0,0);
+    }
+    if(ed->FillSelectionID){
+        tnsAddEvaluatedInstance(ed,o,tnsDrawCameraSelectionID,TNS_EVAL_LAYER_SELECTION,0,1,o->SelectID);
+    }
+}
+
 void tnsRunNode(tnsEvaluatedNode* en, int func, tnsObject* root){
     T->Runtime.CurrentEN = en;
     tnsObject* ob=root; // should be the same as en->Target.
@@ -3950,7 +3962,7 @@ void tnsEvaluateThisObject(tnsObject *o, tnsEvaluateData* ed){
         tnsEvaluateInstancerObject(o,ed);
         if(ed->SceneEvaluateMode){ ed->Scene->CurrentParent=CP; ed->Scene->CurrentChild=CC;}
         break;
-    case TNS_OBJECT_CAMERA: tnsDrawCamera(o); break;
+    case TNS_OBJECT_CAMERA: tnsEvaluateCameraObject(o,ed); break;
     case TNS_OBJECT_SHAPE: tnsEvaluateShapeObject(o,ed); break;
     default: tnsEvaluateEmptyObject(o,ed); break;
     }

+ 4 - 0
resources/la_modelling.c

@@ -1513,6 +1513,8 @@ int OPINV_Add(laOperator *a, laEvent *e){
             no=tnsCreateInstancer(root, "Instancer",0,0,0); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
         elif(strSame(str,"SQUARE")){ tnsDeselectAllObjects(root);
             no=tnsCreateShapeSquare(root, "Square",0,0,0,1); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
+        elif(strSame(str,"CAMERA")){ tnsDeselectAllObjects(root);
+            no=tnsCreateCamera(root, "Camera",rad(75),0,0,5,0,0,0,5); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
         else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
         if(ran){ laRecordAndPush(0,"tns.world","Add object",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
     }elif(mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){ ad->Context=LA_ADD_CTX_MESH;
@@ -1543,6 +1545,8 @@ void laui_Add(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extr
         laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=PLANE;text=Plane",0,0);
         laShowLabel(uil,c,"Shapes",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
         laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=SQUARE;text=Square",0,0);
+        laShowLabel(uil,c,"Camera",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
+        laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=CAMERA;text=Camera",0,0);
     }laEndCondition(uil,b);
 
     b=laOnConditionThat(uil,c,laEqual(laPropExpression(actinst,"context"),laIntExpression(LA_ADD_CTX_MESH)));{

+ 13 - 1
resources/la_properties.c

@@ -997,6 +997,18 @@ tnsMaterial* tnsget_FirstMaterial(void* unused1, void* unused2){
 tnsMaterial* tnsgetactive_SlotMaterial(tnsMaterialSlot* ms, void* unused){
     return ms->Material;
 }
+tnsCamera* tnsget_CameraInRoot(tnsRootObject* ro, void* unused){
+    for(tnsObject* o=T->World->AllObjects.pFirst;o;o=o->Item.pNext){ if(o->InRoot!=ro) continue;
+        if(o->Type==TNS_OBJECT_CAMERA) return o;
+    }
+    return 0;
+}
+tnsCamera* tnsgetnext_CameraInRoot(tnsCamera* c, void* unused){
+    for(tnsObject* o=c->Base.Item.pNext;o;o=o->Item.pNext){ if(o->InRoot!=c->Base.InRoot) continue;
+        if(o->Type==TNS_OBJECT_CAMERA) return o;
+    }
+    return 0;
+}
 
 laPropContainer* tnsget_ObjectType(tnsObject* o){
     switch(o->Type){
@@ -1301,7 +1313,7 @@ void la_RegisterTNSProps(){
         laAddSubGroup(p, "base", "Base", "Object base", "tns_object",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
         laAddSubGroup(p, "__actions__", "Actions", "Animation actions", "la_animation_action",0,0,0,-1,0,laget_CurrentAnimationAction,0,laset_CurrentAnimationAction,0,0,offsetof(tnsRootObject, Actions), 0);
         laAddSubGroup(p, "__action_props__", "Action Props", "Action properties", "la_animation_prop",0,0,0,-1,0,0,0,0,0,0,offsetof(tnsRootObject, ActionProps), 0);
-        laAddSubGroup(p, "active_camera", "Active Camera", "Active camera of this root object", "tns_object",0,0,0,offsetof(tnsRootObject, ActiveCamera),0,0,0,0,0,0,0,LA_UDF_REFER|LA_READ_ONLY);
+        laAddSubGroup(p, "active_camera", "Active Camera", "Active camera of this root object", "tns_object",0,0,0,offsetof(tnsRootObject, ActiveCamera),tnsget_CameraInRoot,0,tnsgetnext_CameraInRoot,0,0,0,0,LA_UDF_REFER);
         ep = laAddEnumProperty(p, "is_2d", "Is 2D", "Is 2D root object", 0,0,0,0,0,offsetof(tnsRootObject, Is2D), 0,tnsset_RootObjectIs2D,0,0,0,0,0,0,0,0);{
             laAddEnumItemAs(ep, "3D", "3D", "Root object is in 3D", 0, 0);
             laAddEnumItemAs(ep, "2D", "2D", "Root object is in 2D", 1, 0);

+ 13 - 4
resources/la_widgets_viewers.c

@@ -175,7 +175,10 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
     NVGcontext* vg=MAIN.CurrentWindow->nvg;
     tnsMatrix44d mview,mproj;
     if(ro && ro->Is2D){ la_SetCanvasOrtho(e,W,H); de.PointScale=e->ZoomX; }
-    else{ tnsApplyCameraView(W, H, c, mview,mproj); tnsMultiply44d(de.mViewProj,mproj,mview); de.Is3D=1; }
+    else{
+        if(ro && ro->ActiveCamera && e->AsPlayer){ c=ro->ActiveCamera; }
+        tnsApplyCameraView(W, H, c, mview,mproj); tnsMultiply44d(de.mViewProj,mproj,mview); de.Is3D=1;
+    }
 
     tnsPushMatrix(); tnsPopMatrix(); //those are necessary when ui is the first in list;
 
@@ -186,6 +189,10 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
             tnsEvaluateObjectTree(root,0,0);
         }
 
+        if(ro && e->AsPlayer && ro->ActiveCamera && !ro->Is2D){ c=ro->ActiveCamera; c=c->Base.PlayDuplicate?c->Base.PlayDuplicate:c;
+            tnsApplyCameraView(W, H, c,0,0);
+        }
+
         for(int i=0;i<2;i++){
             int Do2DInstance=(i==1)?TNS_EVAL_LAYER_SHOW_2D:0;
             int Is2D=(i==1)?1:ro->Is2D;
@@ -219,7 +226,7 @@ void la_RootObjectDraw(laBoxedTheme *bt, tnsObject *root, laUiItem* ui){
 
     if(!(ro && ro->Is2D)){ de.Is3D=1; tnsVector3d dir={0,0,0}; tnsSetCameraViewDir(dir); }
 
-    if (e->ShowFloorGrid){
+    if (e->ShowFloorGrid && (!e->AsPlayer)){
         tnsUseNoTexture();
         real* color=laThemeColor(bt,LA_BT_BORDER); tnsColor4d(LA_COLOR3(color),0.4);
         tnsDrawFloor(e->ViewingCamera->Base.GLocation, e->ViewingCamera->ZMax, e->ShowAxis);
@@ -848,8 +855,10 @@ int OPMOD_Canvas(laOperator *a, laEvent *e){
 
     if(ui->CanvasTemplate->ExtraModal){ if(!(ui->CanvasTemplate->ExtraModal(a,e)&LA_PASS_ON)) return LA_RUNNING; }
 
-    if (laKeyMapExecuteEventEx(a, &ui->ExtraPP, &ui->CanvasTemplate->KeyMapper, e)) return LA_RUNNING;
-    if (laKeyMapExecuteEventEx(a, &ui->ExtraPP, &ui->Type->KeyMapper, e)) return LA_RUNNING;
+    if(!ex->AsPlayer){
+        if (laKeyMapExecuteEventEx(a, &ui->ExtraPP, &ui->CanvasTemplate->KeyMapper, e)) return LA_RUNNING;
+        if (laKeyMapExecuteEventEx(a, &ui->ExtraPP, &ui->Type->KeyMapper, e)) return LA_RUNNING;
+    }
 
     return LA_RUNNING_PASS;
 }