03a39702acd6a21832e64d6723600ac1c0798d6e
[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 # Create an UV sphere of `radius` with `n_meridians` and `n_parallels`
107 init uv_sphere(radius: Float, n_meridians, n_parallels: Int)
108 do
109 var w = n_meridians
110 var h = n_parallels
111
112 var vertices = new Array[Float].with_capacity(w*h*3)
113 self.vertices = vertices
114
115 var texture_coords = new Array[Float].with_capacity(w*h*2)
116 self.texture_coords = texture_coords
117
118 var normals = new Array[Float].with_capacity(w*h*3)
119 self.normals = normals
120
121 # Build vertices
122 for m in [0..w[ do
123 for p in [0..h[ do
124 var u = m.to_f * 2.0 * pi / (w-1).to_f
125 var v = p.to_f * pi / (h-1).to_f
126
127 vertices.add radius * u.cos * v.sin
128 vertices.add radius * v.cos
129 vertices.add radius * u.sin * v.sin
130
131 texture_coords.add (1.0 - m.to_f/(w-1).to_f)
132 texture_coords.add(p.to_f/(h-1).to_f)
133
134 normals.add u.cos * v.sin
135 normals.add v.cos
136 normals.add u.sin * v.sin
137 end
138 end
139
140 # Build faces
141 var indices = new Array[Int].with_capacity((w-1)*(h-1)*6)
142 self.indices = indices
143 for m in [0..w-1[ do
144 for p in [0..h-1[ do
145 var a = m*h + p
146
147 indices.add a
148 indices.add a+h
149 indices.add a+1
150
151 indices.add a+h
152 indices.add a+h+1
153 indices.add a+1
154 end
155 end
156 end
157
158 # Dimensions of this geometry using the min and max of all points on each axis
159 var dimensions: Point3d[Float] is lazy, writable do
160 assert vertices.length % 3 == 0
161
162 var minx = inf
163 var miny = inf
164 var minz = inf
165 var maxx = -inf
166 var maxy = -inf
167 var maxz = -inf
168
169 var i = 0
170 while i < vertices.length do
171 var x = vertices[i]
172 i += 1
173 var y = vertices[i]
174 i += 1
175 var z = vertices[i]
176 i += 1
177
178 minx = minx.min(x)
179 miny = miny.min(y)
180 minz = minz.min(z)
181
182 maxx = maxx.max(x)
183 maxy = maxy.max(y)
184 maxz = maxz.max(z)
185 end
186
187 return new Point3d[Float](maxx-minx, maxy-miny, maxz-minz)
188 end
189
190 # Center of the geometry
191 var center: Point3d[Float] is lazy, writable do
192 assert vertices.length % 3 == 0
193
194 var minx = inf
195 var miny = inf
196 var minz = inf
197 var maxx = -inf
198 var maxy = -inf
199 var maxz = -inf
200
201 var i = 0
202 while i < vertices.length do
203 var x = vertices[i]
204 i += 1
205 var y = vertices[i]
206 i += 1
207 var z = vertices[i]
208 i += 1
209
210 minx = minx.min(x)
211 miny = miny.min(y)
212 minz = minz.min(z)
213
214 maxx = maxx.max(x)
215 maxy = maxy.max(y)
216 maxz = maxz.max(z)
217 end
218
219 var center = new Point3d[Float](
220 (minx+maxx)/2.0,
221 (miny+maxy)/2.0,
222 (minz+maxz)/2.0)
223 return center
224 end
225 end
226
227 # Source of light
228 #
229 # Instances of this class define a light source position and type.
230 class Light
231
232 # TODO point light, spotlight, directional light, etc.
233
234 # Center of this light source in world coordinates
235 var position = new Point3d[Float](0.0, 1000.0, 0.0)
236 end
237
238 redef class App
239
240 # Live actors to be drawn on screen
241 var actors = new Array[Actor]
242
243 # Single light of the scene
244 var light = new Light
245
246 # TODO move `actors & light` to a scene object
247 # TODO support more than 1 light
248 end