699e831f7ec79f1d999263f79e670c25d2f359d0
[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 class Cube
84 super Mesh
85
86 redef var vertices is lazy do
87 var a = [-0.5, -0.5, -0.5]
88 var b = [ 0.5, -0.5, -0.5]
89 var c = [-0.5, 0.5, -0.5]
90 var d = [ 0.5, 0.5, -0.5]
91
92 var e = [-0.5, -0.5, 0.5]
93 var f = [ 0.5, -0.5, 0.5]
94 var g = [-0.5, 0.5, 0.5]
95 var h = [ 0.5, 0.5, 0.5]
96
97 var vertices = new Array[Float]
98 for v in [a, c, d, a, d, b, # front
99 f, h, g, f, g, e, # back
100 b, d, h, b, h, f, # right
101 e, g, c, e, c, a, # left
102 e, a, b, e, b, f, # bottom
103 c, g, h, c, h, d # top
104 ] do vertices.add_all v
105 return vertices
106 end
107
108 redef var normals is lazy do
109 var normals = new Array[Float]
110 var faces_normals = [
111 [0.0, 0.0, -1.0],
112 [0.0, 0.0, 1.0],
113 [ 1.0, 0.0, 0.0],
114 [-1.0, 0.0, 0.0],
115 [0.0, -1.0, 0.0],
116 [0.0, 1.0, 0.0]]
117 for f in faces_normals do for i in 6.times do normals.add_all f
118 return normals
119 end
120
121 redef var texture_coords: Array[Float] is lazy do
122 var a = [0.0, 1.0]
123 var b = [1.0, 1.0]
124 var c = [0.0, 0.0]
125 var d = [1.0, 0.0]
126
127 var texture_coords = new Array[Float]
128 var face = [a, c, d, a, d, b]
129 for i in 6.times do for v in face do texture_coords.add_all v
130 return texture_coords
131 end
132
133 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
134 end
135
136 # Sphere with `radius` and a number of faces set by `n_meridians` and `n_parallels`
137 class UVSphere
138 super Mesh
139
140 # Distance between the center and the vertices
141 var radius: Float
142
143 # Number of vertices on a full circle around the Z axis
144 var n_meridians: Int
145
146 # Number of vertices on an arc between both poles
147 var n_parallels: Int
148
149 init
150 do
151 var w = n_meridians
152 var h = n_parallels
153
154 var vertices = new Array[Float].with_capacity(w*h*3)
155 self.vertices = vertices
156
157 var texture_coords = new Array[Float].with_capacity(w*h*2)
158 self.texture_coords = texture_coords
159
160 var normals = new Array[Float].with_capacity(w*h*3)
161 self.normals = normals
162
163 # Build vertices
164 for m in [0..w[ do
165 for p in [0..h[ do
166 var u = m.to_f * 2.0 * pi / (w-1).to_f
167 var v = p.to_f * pi / (h-1).to_f
168
169 vertices.add radius * u.cos * v.sin
170 vertices.add radius * v.cos
171 vertices.add radius * u.sin * v.sin
172
173 texture_coords.add (1.0 - m.to_f/(w-1).to_f)
174 texture_coords.add(p.to_f/(h-1).to_f)
175
176 normals.add u.cos * v.sin
177 normals.add v.cos
178 normals.add u.sin * v.sin
179 end
180 end
181
182 # Build faces
183 var indices = new Array[Int].with_capacity((w-1)*(h-1)*6)
184 self.indices = indices
185 for m in [0..w-1[ do
186 for p in [0..h-1[ do
187 var a = m*h + p
188
189 indices.add a
190 indices.add a+h
191 indices.add a+1
192
193 indices.add a+h
194 indices.add a+h+1
195 indices.add a+1
196 end
197 end
198 end
199 end