gamnit and related: improve doc
[nit.git] / lib / gamnit / depth / more_meshes.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 # More simple geometric meshes
16 module more_meshes
17
18 import geometry
19 import depth_core
20 import model_dimensions
21
22 # Simple flat mesh, sits on the axes X and Z, normal on Y
23 class Plane
24 super Mesh
25
26 # TODO allow for complex rotation, either at creation or in Actor
27
28 # Modifier to `texture_coords` to repeat the texture on the X axis
29 #
30 # At 1.0, the texture is stretched to cover the whole plane.
31 # If greater than 1.0, the texture is repeated.
32 #
33 # This value must be set before using `texture_coords` or drawing this plane.
34 var repeat_x = 1.0 is writable
35
36 # Modifier to `texture_coords` to repeat the texture on the Y axis
37 #
38 # At 1.0, the texture is stretched to cover the whole plane.
39 # If greater than 1.0, the texture is repeated.
40 #
41 # This value must be set before using `texture_coords` or drawing this plane.
42 var repeat_y = 1.0 is writable
43
44 redef var vertices is lazy do
45 var a = [-0.5, 0.0, -0.5]
46 var b = [ 0.5, 0.0, -0.5]
47 var c = [-0.5, 0.0, 0.5]
48 var d = [ 0.5, 0.0, 0.5]
49
50 var vertices = new Array[Float]
51 for v in [c, d, a, b] do vertices.add_all v
52 return vertices
53 end
54
55 redef var normals: Array[Float] is lazy do
56 var normals = new Array[Float]
57 for i in 4.times do normals.add_all([0.0, 1.0, 0.0])
58 return normals
59 end
60
61 redef var texture_coords: Array[Float] is lazy do
62 var offset_left = 0.0
63 var offset_top = 0.0
64 var offset_right = 1.0*repeat_x
65 var offset_bottom = 1.0*repeat_y
66 var a = [offset_left, offset_bottom]
67 var b = [offset_right, offset_bottom]
68 var c = [offset_left, offset_top]
69 var d = [offset_right, offset_top]
70
71 var texture_coords = new Array[Float]
72 for v in [c, d, a, b] do texture_coords.add_all v
73 return texture_coords
74 end
75
76 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
77
78 init do indices.add_all([0, 1, 2, 2, 1, 3])
79 # TODO use gl_TRIANGLE_FAN instead
80 end
81
82 # Cube, with 6 faces
83 #
84 # Occupies `[-0.5..0.5]` on all three axes.
85 class Cube
86 super Mesh
87
88 redef var vertices is lazy do
89 var a = [-0.5, -0.5, -0.5]
90 var b = [ 0.5, -0.5, -0.5]
91 var c = [-0.5, 0.5, -0.5]
92 var d = [ 0.5, 0.5, -0.5]
93
94 var e = [-0.5, -0.5, 0.5]
95 var f = [ 0.5, -0.5, 0.5]
96 var g = [-0.5, 0.5, 0.5]
97 var h = [ 0.5, 0.5, 0.5]
98
99 var vertices = new Array[Float]
100 for v in [a, c, d, a, d, b, # front
101 f, h, g, f, g, e, # back
102 b, d, h, b, h, f, # right
103 e, g, c, e, c, a, # left
104 e, a, b, e, b, f, # bottom
105 c, g, h, c, h, d # top
106 ] do vertices.add_all v
107 return vertices
108 end
109
110 redef var normals is lazy do
111 var normals = new Array[Float]
112 var faces_normals = [
113 [0.0, 0.0, -1.0],
114 [0.0, 0.0, 1.0],
115 [ 1.0, 0.0, 0.0],
116 [-1.0, 0.0, 0.0],
117 [0.0, -1.0, 0.0],
118 [0.0, 1.0, 0.0]]
119 for f in faces_normals do for i in 6.times do normals.add_all f
120 return normals
121 end
122
123 redef var texture_coords: Array[Float] is lazy do
124 var a = [0.0, 1.0]
125 var b = [1.0, 1.0]
126 var c = [0.0, 0.0]
127 var d = [1.0, 0.0]
128
129 var texture_coords = new Array[Float]
130 var face = [a, c, d, a, d, b]
131 for i in 6.times do for v in face do texture_coords.add_all v
132 return texture_coords
133 end
134
135 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
136 end
137
138 # Sphere with `radius` and a number of faces set by `n_meridians` and `n_parallels`
139 class UVSphere
140 super Mesh
141
142 # Distance between the center and the vertices
143 var radius: Float
144
145 # Number of vertices on a full circle around the Z axis
146 var n_meridians: Int
147
148 # Number of vertices on an arc between both poles
149 var n_parallels: Int
150
151 init
152 do
153 var w = n_meridians
154 var h = n_parallels
155
156 var vertices = new Array[Float].with_capacity(w*h*3)
157 self.vertices = vertices
158
159 var texture_coords = new Array[Float].with_capacity(w*h*2)
160 self.texture_coords = texture_coords
161
162 var normals = new Array[Float].with_capacity(w*h*3)
163 self.normals = normals
164
165 # Build vertices
166 for m in [0..w[ do
167 for p in [0..h[ do
168 var u = m.to_f * 2.0 * pi / (w-1).to_f
169 var v = p.to_f * pi / (h-1).to_f
170
171 vertices.add radius * u.cos * v.sin
172 vertices.add radius * v.cos
173 vertices.add radius * u.sin * v.sin
174
175 texture_coords.add (1.0 - m.to_f/(w-1).to_f)
176 texture_coords.add(p.to_f/(h-1).to_f)
177
178 normals.add u.cos * v.sin
179 normals.add v.cos
180 normals.add u.sin * v.sin
181 end
182 end
183
184 # Build faces
185 var indices = new Array[Int].with_capacity((w-1)*(h-1)*6)
186 self.indices = indices
187 for m in [0..w-1[ do
188 for p in [0..h-1[ do
189 var a = m*h + p
190
191 indices.add a
192 indices.add a+h
193 indices.add a+1
194
195 indices.add a+h
196 indices.add a+h+1
197 indices.add a+1
198 end
199 end
200 end
201 end