Browse Source

add GeometryInstance node.

SakiKawasaki 4 years ago
parent
commit
2757e03d44

+ 25 - 24
README.md

@@ -48,30 +48,30 @@
 
 ## Now available
 This section contains links to documentation for all nodes.
-|Core                |Default nodes             |Control nodes                  |2D Nodes                    |3D Nodes        |
-|:--:                |:--:                      |:--:                           |  :--:                      |:--:            |
-|[Anchor][Anchor]    |[Node][Node]              |[Control][Control]             |[Node2D][Node2D]            |[Node3D][Node3D]|
-|[Color][Color]      |[Canvas][Canvas]          |[ColorRect][ColorRect]         |[Sprite][Sprite]            |                |
-|[ColorText][clrtext]|[Scene][Scene]            |[TextureRect][TextureRect]     |[AnimatedSprite][asprite]   |                |
-|[Enums][Enums]      |[AudioStreamPlayer][aplay]|[Label][Label]                 |[YSort][YSort]              |                |
-|[Exceptions][except]|                          |[Button][Button]               |[CollisionShape2D][cshape2d]|                |
-|[Image][Image]      |                          |[EditText][EditText]           |[Camera2D][Camera2D]        |                |
-|[Input][Input]      |                          |[RichLabel][RichLabel]         |                            |                |
-|[Rect2][Rect2]      |                          |[RichEditText][RichEditText]   |                            |                |
-|[Vector2][Vector2]  |                          |[Box][Box]                     |                            |                |
-|[Circle2][Circle2]  |                          |[HBox][HBox]                   |                            |                |
-|[Polygon2][Polygon2]|                          |[VBox][VBox]                   |                            |                |
-|[AudioStream][astrm]|                          |[GridBox][GridBox]             |                            |                |
-|[Animation][anim]   |                          |[Scroll][Scroll]               |                            |                |
-|[Vector3][Vector3]  |                          |[ProgressBar][ProgressBar]     |                            |                |
-|                    |                          |[Slider][Slider]               |                            |                |
-|                    |                          |[VProgressBar][VProgressBar]   |                            |                |
-|                    |                          |[VSlider][VSlider]             |                            |                |
-|                    |                          |[Popup][Popup]                 |                            |                |
-|                    |                          |[TextureButton][TextureButton] |                            |                |
-|                    |                          |[TextureProgressBar][tprogress]|                            |                |
-|                    |                          |[Counter][Counter]             |                            |                |
-|                    |                          |[Switch][Switch]               |                            |                |
+|Core                |Default nodes             |Control nodes                  |2D Nodes                    |3D Nodes                     |
+|:--:                |:--:                      |:--:                           |  :--:                      |:--:                         |
+|[Anchor][Anchor]    |[Node][Node]              |[Control][Control]             |[Node2D][Node2D]            |[Node3D][Node3D]             |
+|[Color][Color]      |[Canvas][Canvas]          |[ColorRect][ColorRect]         |[Sprite][Sprite]            |[GeometryInstance][ginstance]|
+|[ColorText][clrtext]|[Scene][Scene]            |[TextureRect][TextureRect]     |[AnimatedSprite][asprite]   |                             |
+|[Enums][Enums]      |[AudioStreamPlayer][aplay]|[Label][Label]                 |[YSort][YSort]              |                             |
+|[Exceptions][except]|                          |[Button][Button]               |[CollisionShape2D][cshape2d]|                             |
+|[Image][Image]      |                          |[EditText][EditText]           |[Camera2D][Camera2D]        |                             |
+|[Input][Input]      |                          |[RichLabel][RichLabel]         |                            |                             |
+|[Rect2][Rect2]      |                          |[RichEditText][RichEditText]   |                            |                             |
+|[Vector2][Vector2]  |                          |[Box][Box]                     |                            |                             |
+|[Circle2][Circle2]  |                          |[HBox][HBox]                   |                            |                             |
+|[Polygon2][Polygon2]|                          |[VBox][VBox]                   |                            |                             |
+|[AudioStream][astrm]|                          |[GridBox][GridBox]             |                            |                             |
+|[Animation][anim]   |                          |[Scroll][Scroll]               |                            |                             |
+|[Vector3][Vector3]  |                          |[ProgressBar][ProgressBar]     |                            |                             |
+|                    |                          |[Slider][Slider]               |                            |                             |
+|                    |                          |[VProgressBar][VProgressBar]   |                            |                             |
+|                    |                          |[VSlider][VSlider]             |                            |                             |
+|                    |                          |[Popup][Popup]                 |                            |                             |
+|                    |                          |[TextureButton][TextureButton] |                            |                             |
+|                    |                          |[TextureProgressBar][tprogress]|                            |                             |
+|                    |                          |[Counter][Counter]             |                            |                             |
+|                    |                          |[Switch][Switch]               |                            |                             |
 
 
 
@@ -167,3 +167,4 @@ Use the [`Nim compiler user guide`](https://nim-lang.org/docs/nimc.html#dynlibov
 [Node2D]:https://ethosa.github.io/nodesnim/nodesnim/nodes2d/node2d.html
 
 [Node3D]:https://ethosa.github.io/nodesnim/nodesnim/nodes3d/node3d.html
+[ginstance]:https://ethosa.github.io/nodesnim/nodesnim/nodes3d/geometry_instance.html

+ 3 - 2
src/nodesnim.nim

@@ -70,7 +70,8 @@ import
   nodesnim/nodes2d/kinematic_body2d,
   nodesnim/nodes2d/camera2d,
 
-  nodesnim/nodes3d/node3d
+  nodesnim/nodes3d/node3d,
+  nodesnim/nodes3d/geometry_instance
 
 export
   # Third party
@@ -89,4 +90,4 @@ export
   # 2D nodes
   node2d, sprite, animated_sprite, ysort, collision_shape2d, kinematic_body2d, camera2d,
   # 3D nodes
-  node3d
+  node3d, geometry_instance

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

@@ -45,7 +45,8 @@ type
     VBOX_NODE,
     VPROGRESS_BAR_NODE,
     VSLIDER_NODE,
-    NODE3D_NODE
+    NODE3D_NODE,
+    GEOMETRY_INSTANCE_NODE
   NodeTypes* {.pure.} = enum
     NODE_TYPE_DEFAULT,
     NODE_TYPE_CONTROL,

+ 2 - 1
src/nodesnim/nodes/node.nim

@@ -15,7 +15,7 @@ import
 type
   NodeObj* = object of RootObj
     kind*: NodeKind
-    node_type*: NodeTypes
+    type_of_node*: NodeTypes
     visible*: bool
     is_ready*: bool
     can_use_anchor*: bool
@@ -54,6 +54,7 @@ template nodepattern*(nodetype: untyped): untyped =
     anchor: Anchor(0, 0, 0, 0), size_anchor: Vector2(), can_use_anchor: false,
     can_use_size_anchor: false, z_index: 0f, z_index_global: 0f, relative_z_index: true
   )
+  result.type_of_node = NODE_TYPE_DEFAULT
 
 proc Node*(name: string = "Node"): NodeRef =
   ## Creates a new Node.

+ 1 - 1
src/nodesnim/nodes2d/node2d.nim

@@ -25,7 +25,7 @@ type
 template node2dpattern*: untyped =
   result.centered = true
   result.timed_position = Vector2()
-  result.node_type = NODE_TYPE_2D
+  result.type_of_node = NODE_TYPE_2D
 
 proc Node2D*(name: string = "Node2D"): Node2DRef =
   ## Creates a new Node2D.

+ 91 - 0
src/nodesnim/nodes3d/geometry_instance.nim

@@ -0,0 +1,91 @@
+# author: Ethosa
+import
+  ../thirdparty/opengl,
+
+  ../core/vector2,
+  ../core/vector3,
+  ../core/enums,
+  ../core/anchor,
+  ../core/input,
+
+  ../nodes/node,
+  node3d
+
+
+type
+  GeometryType* {.pure.} = enum
+    GEOMETRY_CUBE
+  GeometryInstanceObj* = object of Node3DObj
+    geometry*: GeometryType
+  GeometryInstanceRef* = ref GeometryInstanceObj
+
+
+proc GeometryInstance*(name: string = "GeometryInstance"): GeometryInstanceRef =
+  ## Creates a new GeometryInstance object.
+  ##
+  ## Arguments:
+  ## - `name` is a node name.
+  runnableExamples:
+    var node = GeometryInstance("GeometryInstance")
+  nodepattern(GeometryInstanceRef)
+  node3dpattern()
+  result.geometry = GEOMETRY_CUBE
+  result.kind = GEOMETRY_INSTANCE_NODE
+
+
+method draw*(self: GeometryInstanceRef, w, h: Glfloat) =
+  ## This method uses in the `window.nim`.
+  {.warning[LockLevel]: off.}
+
+  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(1, 1, 1, 1)
+  glEnable(GL_DEPTH_TEST)
+
+  case self.geometry
+  of GEOMETRY_CUBE:
+    glBegin(GL_QUADS)
+    # back
+    glVertex3f(-50, -50, -50)
+    glVertex3f( 50, -50, -50)
+    glVertex3f( 50,  50, -50)
+    glVertex3f(-50,  50, -50)
+
+    # front
+    glVertex3f(-50, -50, 50)
+    glVertex3f( 50, -50, 50)
+    glVertex3f( 50,  50, 50)
+    glVertex3f(-50,  50, 50)
+
+    # top
+    glVertex3f(-50, -50, -50)
+    glVertex3f( 50, -50, -50)
+    glVertex3f( 50, -50,  50)
+    glVertex3f(-50, -50,  50)
+
+    # bottom
+    glVertex3f(-50, 50, -50)
+    glVertex3f( 50, 50, -50)
+    glVertex3f( 50, 50,  50)
+    glVertex3f(-50, 50,  50)
+
+    # left side
+    glVertex3f(-50, -50, -50)
+    glVertex3f(-50, -50,  50)
+    glVertex3f(-50,  50,  50)
+    glVertex3f(-50,  50, -50)
+
+    # right side
+    glVertex3f(50, -50, -50)
+    glVertex3f(50, -50,  50)
+    glVertex3f(50,  50,  50)
+    glVertex3f(50,  50, -50)
+    glEnd()
+
+
+  glDisable(GL_DEPTH_TEST)
+  glPopMatrix()

+ 4 - 6
src/nodesnim/nodes3d/node3d.nim

@@ -1,7 +1,5 @@
 # author: Ethosa
 import
-  ../thirdparty/opengl,
-
   ../core/vector2,
   ../core/vector3,
   ../core/enums,
@@ -21,11 +19,11 @@ type
 template node3dpattern* =
   result.rotation = Vector3()
   result.translation = Vector3()
-  result.scale = Vector3()
+  result.scale = Vector3(1, 1, 1)
   result.global_rotation = Vector3()
   result.global_translation = Vector3()
-  result.global_scale = Vector3()
-  result.node_type = NODE_TYPE_3D
+  result.global_scale = Vector3(1, 1, 1)
+  result.type_of_node = NODE_TYPE_3D
 
 
 proc Node3D*(name: string = "Node3D"): Node3DRef =
@@ -48,7 +46,7 @@ method calcGlobalPosition3*(self: Node3DRef) =
 
   while current.parent != nil:
     current = current.parent
-    if current.node_type == NODE_TYPE_3D:
+    if current.type_of_node == NODE_TYPE_3D:
       self.global_translation += current.Node3DRef.translation
       self.global_rotation += current.Node3DRef.rotation
       self.global_scale += current.Node3DRef.scale

+ 1 - 1
src/nodesnim/nodescontrol/control.nim

@@ -48,7 +48,7 @@ template controlpattern*: untyped =
   result.on_release = proc(self: ControlRef, x, y: float) = discard
   result.on_focus = proc(self: ControlRef) = discard
   result.on_unfocus = proc(self: ControlRef) = discard
-  result.node_type = NODE_TYPE_CONTROL
+  result.type_of_node = NODE_TYPE_CONTROL
 
 proc Control*(name: string = "Control"): ControlRef =
   ## Creates a new Control.

+ 1 - 0
tests/README.md

@@ -38,3 +38,4 @@
 36. [Rotation.](https://github.com/Ethosa/nodesnim/blob/master/tests/test36.nim)
 37. [Use Camera2D node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test37.nim)
 38. [Use Node3D.](https://github.com/Ethosa/nodesnim/blob/master/tests/test38.nim)
+39. [Use GeometryInstance node.](https://github.com/Ethosa/nodesnim/blob/master/tests/test39.nim)

+ 23 - 0
tests/test39.nim

@@ -0,0 +1,23 @@
+# --- Test 39. Use GeometryInstance node. --- #
+import nodesnim
+
+
+Window("smth")
+
+var
+  scene = Scene("Main")
+  geometry = GeometryInstance("Cube")
+
+
+scene.addChild(geometry)
+
+geometry.translateX(-50)
+geometry.transformX(1)
+
+geometry@on_input(self, event):
+  if event.isInputEventMouseMotion() and event.pressed:
+    geometry.rotateX(-event.yrel)
+    geometry.rotateY(-event.xrel)
+
+addMainScene(scene)
+windowLaunch()