Browse Source

add sprite 3d

Ethosa 3 years ago
parent
commit
49212dce42

+ 2 - 1
src/nodesnim/core/enums.nim

@@ -50,7 +50,8 @@ type
     # 3D nodes
     NODE3D_NODE,
     GEOMETRY_INSTANCE_NODE,
-    CAMERA_3D_NODE
+    CAMERA_3D_NODE,
+    SPRITE_3D_NODE
   NodeTypes* {.pure, size: sizeof(int8).} = enum
     NODE_TYPE_DEFAULT,
     NODE_TYPE_CONTROL,

+ 4 - 6
src/nodesnim/nodes/scene.nim

@@ -36,6 +36,7 @@ proc Scene*(name: string = "Scene"): SceneRef =
 method drawScene*(scene: SceneRef, w, h: GLfloat, paused: bool) {.base.} =
   ## Draws scene
   ## This used in the window.nim.
+  scene.on_process(scene)
   for child in scene.getChildIter():
     if paused and child.getPauseMode() != PROCESS:
       continue
@@ -57,17 +58,13 @@ method drawScene*(scene: SceneRef, w, h: GLfloat, paused: bool) {.base.} =
         child.Node3DRef.calcGlobalPosition3()
         gluPerspective(45.0, w/h, 1.0, 5000.0)
         if current_camera.isNil():
-          gluLookAt(0, 0, -1,
-                    0, 0, 1,
-                    0, 1, 0)
+          gluLookAt(0, 0, -1, 0, 0, 1, 0, 1, 0)
         else:
           let
             pos = current_camera.global_translation
             front = current_camera.front + pos
             up = current_camera.up
-          gluLookAt(pos.x, pos.y, pos.z,
-                    front.x, front.y, front.z,
-                    up.x, up.y, up.z)
+          gluLookAt(pos.x, pos.y, pos.z, front.x, front.y, front.z, up.x, up.y, up.z)
 
       if not child.is_ready:
         child.on_ready(child)
@@ -101,6 +98,7 @@ method exit*(scene: SceneRef) {.base.} =
 
 method handleScene*(scene: SceneRef, event: InputEvent, mouse_on: var NodeRef, paused: bool) {.base.} =
   ## Handles user input. This called on any input.
+  scene.on_input(scene, event)
   var childs = scene.getChildIter()
   for i in countdown(childs.len()-1, 0):
     if paused and childs[i].getPauseMode() != PROCESS:

+ 0 - 4
src/nodesnim/nodes2d/animated_sprite.nim

@@ -126,10 +126,6 @@ method duplicate*(self: AnimatedSpriteRef): AnimatedSpriteRef {.base.} =
   ## Duplicates AnimatedSprite object and create a new AnimatedSprite.
   self.deepCopy()
 
-method getGlobalMousePosition*(self: AnimatedSpriteRef): Vector2Obj {.inline.} =
-  ## Returns mouse position.
-  Vector2Obj(x: last_event.x, y: last_event.y)
-
 method addAnimation*(self: AnimatedSpriteRef, name: string, speed: float = 2f) {.base.} =
   ## Adds a new animation to the AnimatedSprite animations.
   ##

+ 3 - 2
src/nodesnim/nodes3d.nim

@@ -1,7 +1,8 @@
 import
   nodes3d/node3d,
   nodes3d/geometry_instance,
-  nodes3d/camera3d
+  nodes3d/camera3d,
+  nodes3d/sprite3d
 
 export
-  node3d, geometry_instance, camera3d
+  node3d, geometry_instance, camera3d, sprite3d

+ 92 - 0
src/nodesnim/nodes3d/sprite3d.nim

@@ -0,0 +1,92 @@
+# author: Ethosa
+## It provides display sprites.
+import
+  ../thirdparty/opengl,
+
+  ../core/vector3,
+  ../core/anchor,
+  ../core/input,
+  ../core/enums,
+  ../core/image,
+  ../core/color,
+
+  ../nodes/node,
+  node3d
+
+
+type
+  Sprite3DObj* = object of Node3DObj
+    filter*: ColorRef
+    texture*: GlTextureObj
+  Sprite3DRef* = ref Sprite3DObj
+
+
+
+proc Sprite3D*(name: string = "Sprite3D"): Sprite3DRef =
+  ## Creates a new Sprite3D.
+  ##
+  ## Arguments:
+  ## - `name` is a node name.
+  runnableExamples:
+    var node = Sprite3D("Sprite3D")
+  nodepattern(Sprite3DRef)
+  node3dpattern()
+  result.texture = GlTextureObj()
+  result.filter = Color(1f, 1f, 1f)
+  result.kind = SPRITE_3D_NODE
+
+
+method draw*(self: Sprite3DRef, w, h: GLfloat) =
+  ## this method uses in the `window.nim`.
+  {.warning[LockLevel]: off.}
+  # Recalculate position.
+  procCall self.Node3DRef.draw(w, h)
+
+  # Draw
+  if self.texture.texture > 0'u32:
+    let height = (self.texture.size.y/self.texture.size.x).float
+    glPushMatrix()
+    glTranslatef(self.global_translation.x, self.global_translation.y, self.global_translation.z)
+    glRotatef(self.global_rotation.x, 1, 0, 0)
+    glRotatef(self.global_rotation.y, 0, 1, 0)
+    glRotatef(self.global_rotation.z, 0, 0, 1)
+    glScalef(self.scale.x, self.scale.y, self.scale.z)
+    glColor4f(self.filter.r, self.filter.g, self.filter.b, self.filter.a)
+
+    glEnable(GL_TEXTURE_2D)
+    glEnable(GL_DEPTH_TEST)
+    glBindTexture(GL_TEXTURE_2D, self.texture.texture)
+
+    glBegin(GL_QUADS)
+    glTexCoord2f(0, 0)
+    glVertex3f(-1, height, 0)
+    glTexCoord2f(0, 1)
+    glVertex3f(-1, -height, 0)
+    glTexCoord2f(1, 1)
+    glVertex3f(1, -height, 0)
+    glTexCoord2f(1, 0)
+    glVertex3f(1, height, 0)
+    glEnd()
+
+    glDisable(GL_DEPTH_TEST)
+    glDisable(GL_TEXTURE_2D)
+    glPopMatrix()
+
+method duplicate*(self: Sprite3DRef): Sprite3DRef {.base.} =
+  ## Duplicates Sprite object and create a new Sprite.
+  self.deepCopy()
+
+method loadTexture*(self: Sprite3DRef, file: string, mode = GL_RGB) {.base.} =
+  ## Loads a new texture from file.
+  ##
+  ## Arguments:
+  ## - `file` is a texture path.
+  ## - `mode` is a GLenum. can be GL_RGB or GL_RGBA.
+  self.texture = load(file, mode)
+
+method setTexture*(self: Sprite3DRef, texture: GlTextureObj) {.base.} =
+  ## Loads a new texture from file.
+  ##
+  ## Arguments:
+  ## - `texture` is a GlTexture object.
+  self.texture = texture

+ 1 - 1
src/nodesnim/window.nim

@@ -112,7 +112,7 @@ proc keyboardpress(c: cint) {.cdecl.} =
   ## Called when press any key on keyboard.
   if c < 0:
     return
-  var key = $c
+  let key = $c
   check(InputEventKeyboard, last_event.pressed, true)
   last_event.key = key
   last_event.key_int = c

+ 19 - 0
tests/test51.nim

@@ -0,0 +1,19 @@
+# --- Use Sprite 3D node. --- #
+import nodesnim
+
+
+Window("Sprite 3D")
+
+build:
+  - Scene main:
+    - Sprite3D sprite:
+      call loadTexture("assets/anim/0.jpg", GL_RGB)
+      call translateZ(2)
+
+sprite@on_input(self, event):
+  if event.isInputEventMouseMotion() and event.pressed:
+    sprite.rotateX(-event.yrel)
+    sprite.rotateY(-event.xrel)
+
+addMainScene(main)
+windowLaunch()