浏览代码

small update camera3d

Ethosa 3 年之前
父节点
当前提交
25637728b2
共有 3 个文件被更改,包括 22 次插入12 次删除
  1. 3 5
      src/nodesnim/nodes/scene.nim
  2. 15 2
      src/nodesnim/nodes3d/camera3d.nim
  3. 4 5
      tests/test48.nim

+ 3 - 5
src/nodesnim/nodes/scene.nim

@@ -10,9 +10,7 @@ import
   ../core/vector2,
   ../core/vector3,
   ../nodes3d/node3d,
-  ../nodes3d/camera3d,
-  times,
-  math
+  ../nodes3d/camera3d
 
 
 type
@@ -51,9 +49,9 @@ method drawScene*(scene: SceneRef, w, h: GLfloat, paused: bool) {.base.} =
       # when 2D
       if child.type_of_node == NODE_TYPE_CONTROL:
         child.CanvasRef.calcGlobalPosition()
-        glOrtho(-w.GLdouble/2, w.GLdouble/2, -h.GLdouble/2, h.GLdouble/2, -w.GLdouble, w.GLdouble)
+        glOrtho(-w/2, w/2, -h/2, h/2, -w, w)
       elif child.type_of_node == NODE_TYPE_2D:
-        glOrtho(-w.GLdouble/2, w.GLdouble/2, -h.GLdouble/2, h.GLdouble/2, -w.GLdouble, w.GLdouble)
+        glOrtho(-w/2, w/2, -h/2, h/2, -w, w)
       # when 3D
       elif child.type_of_node == NODE_TYPE_3D:
         child.Node3DRef.calcGlobalPosition3()

+ 15 - 2
src/nodesnim/nodes3d/camera3d.nim

@@ -17,6 +17,7 @@ type
     target*: Node3DRef
     front*: Vector3Obj
     up*: Vector3Obj
+    right*: Vector3Obj
   Camera3DRef* = ref Camera3DObj
 
 
@@ -38,6 +39,7 @@ proc Camera3D*(name: string = "Camera3D"): Camera3DRef =
   result.kind = CAMERA_3D_NODE
   result.front = Vector3(0, 0, 1)
   result.up = Vector3(0, 1, 0)
+  result.right = Vector3()
   result.pitch = 0f
   result.yaw = 90f
   nodes.add(result)
@@ -60,8 +62,8 @@ method draw*(self: Camera3DRef, w, h: GLfloat) =
   self.front = Vector3(
     cos(degToRad(self.yaw)) * cos(degToRad(self.pitch)),
     sin(degToRad(self.pitch)),
-    sin(degToRad(self.yaw)) * cos(degToRad(self.pitch)))
-  self.front.normalize()
+    sin(degToRad(self.yaw)) * cos(degToRad(self.pitch))).normalized()
+  self.right = self.front.cross(self.up).normalized()
 
 method setCurrent*(self: Camera3DRef) {.base.} =
   ## Changes the current camera. It also automatically disable other cameras.
@@ -69,3 +71,14 @@ method setCurrent*(self: Camera3DRef) {.base.} =
     c.current = false
   self.current = true
   current_camera = self
+
+method rotateCameraX*(self: Camera3DRef, val: float) {.base.} =
+  self.yaw += val
+
+method rotateCameraY*(self: Camera3DRef, val: float) {.base.} =
+  self.pitch += val
+
+method rotateCamera*(self: Camera3DRef, x, y: float) {.base.} =
+  ## Rotates camera by `x` and `y` values.
+  self.yaw += x
+  self.pitch += y

+ 4 - 5
tests/test48.nim

@@ -1,7 +1,7 @@
 # --- Test 48. Use Camera3D node. --- #
 import nodesnim
 
-Window("camera 3d test")
+Window("camera 3d test", 1024, 640)
 
 
 build:
@@ -38,12 +38,11 @@ Input.addKeyAction("right", "d")
 
 root@on_input(self, event):
   if event.isInputEventMouseMotion() and event.pressed:
-    camera.pitch += event.yrel*0.1  # Y
-    camera.yaw -= event.xrel*0.1    # X
+    camera.rotateCamera(-event.xrel*0.1, event.yrel*0.1)
   if Input.isActionPressed("left"):
-    root.translate(camera.front.cross(camera.up).normalized() * -0.1)
+    root.translate(camera.right * -0.1)
   if Input.isActionPressed("right"):
-    root.translate(camera.front.cross(camera.up).normalized() * 0.1)
+    root.translate(camera.right * 0.1)
   if Input.isActionPressed("forward"):
     root.translate(camera.front*0.1)
   if Input.isActionPressed("back"):