1c3e91679074bc22d0b5caa484f629a3335cb46b
[nit.git] / lib / gamnit / depth / depth_core.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Base entities of the depth 3D game framework
16 module depth_core
17
18 intrude import gamnit::flat
19
20 # Visible entity in the game world, represented by its `model` modified by the other attributes
21 class Actor
22
23 # Model used to dray this actor
24 var model: Model
25
26 # Position of this sprite in world coordinates
27 var center: Point3d[Float] is writable
28
29 # Rotation on the Z axis
30 var rotation = 0.0 is writable
31
32 # Scale applied to this sprite
33 var scale = 1.0 is writable
34
35 # Transparency applied to the texture on draw
36 var alpha = 1.0 is writable
37 end
38
39 # Entire 3D model defined by its `leaves`, an association of `Mesh` to `Material`
40 abstract class Model
41
42 # Load this model in memory
43 fun load do end
44
45 # All `LeafModel` composing this model
46 #
47 # Usually, there is one `LeafModel` per material.
48 # At each frame, each material is asked to draw all the live `LeafModel` instaces.
49 fun leaves: Array[LeafModel] is abstract
50 end
51
52 # Model composed of one or many other `LeafModel`
53 class CompositeModel
54 super Model
55
56 redef var leaves = new Array[LeafModel]
57 end
58
59 # Single model with a `mesh` and `material`
60 #
61 # Only leaves are actually drawn by the `material`.
62 class LeafModel
63 super Model
64
65 # Mesh forming this model
66 var mesh: Mesh
67
68 # Material applied on this model
69 var material: Material
70
71 redef var leaves = [self]
72 end
73
74 # Material for a model or how to draw the model
75 abstract class Material
76
77 # Draw `actor`
78 #
79 # This method should be refined by subclasses as the default implementation is a no-op.
80 #
81 # This method is called on many materials for many `actor` and `model` at each frame.
82 # It is expected to use a `GLProgram` and call an equivalent to `glDrawArrays`.
83 # However, it should not call `glClear` nor `GamnitDisplay::flip`.
84 fun draw(actor: Actor, model: LeafModel) do end
85 end
86
87 # Mesh with all geometry data
88 class Mesh
89
90 # Vertices coordinates
91 var vertices = new Array[Float] is lazy, writable
92
93 # Indices to draw triangles with `glDrawElements`
94 #
95 # If `not_empty`, use `glDrawElements`, otherwise use `glDrawArrays`.
96 var indices = new Array[Int] is lazy, writable
97
98 private var indices_c = new CUInt16Array.from(indices) is lazy, writable
99
100 # Normals on each vertex
101 var normals = new Array[Float] is lazy, writable
102
103 # Coordinates on the texture per vertex
104 var texture_coords = new Array[Float] is lazy, writable
105
106 # `GLDrawMode` used to display this mesh, defaults to `gl_TRIANGLES`
107 fun draw_mode: GLDrawMode do return gl_TRIANGLES
108 end
109
110 # Source of light
111 #
112 # Instances of this class define a light source position and type.
113 class Light
114
115 # TODO point light, spotlight, directional light, etc.
116
117 # Center of this light source in world coordinates
118 var position = new Point3d[Float](0.0, 1000.0, 0.0)
119 end
120
121 redef class App
122
123 # Live actors to be drawn on screen
124 var actors = new Array[Actor]
125
126 # Single light of the scene
127 var light = new Light
128
129 # TODO move `actors & light` to a scene object
130 # TODO support more than 1 light
131 end