699e831f7ec79f1d999263f79e670c25d2f359d0
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # More simple geometric meshes
20 import model_dimensions
22 # Simple flat mesh, sits on the axes X and Z, normal on Y
26 # TODO allow for complex rotation, either at creation or in Actor
28 # Modifier to `texture_coords` to repeat the texture on the X axis
30 # At 1.0, the texture is stretched to cover the whole plane.
31 # If greater than 1.0, the texture is repeated.
33 # This value must be set before using `texture_coords` or drawing this plane.
34 var repeat_x
= 1.0 is writable
36 # Modifier to `texture_coords` to repeat the texture on the Y axis
38 # At 1.0, the texture is stretched to cover the whole plane.
39 # If greater than 1.0, the texture is repeated.
41 # This value must be set before using `texture_coords` or drawing this plane.
42 var repeat_y
= 1.0 is writable
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]
50 var vertices
= new Array[Float]
51 for v
in [c
, d
, a
, b
] do vertices
.add_all v
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])
61 redef var texture_coords
: Array[Float] is lazy
do
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
]
71 var texture_coords
= new Array[Float]
72 for v
in [c
, d
, a
, b
] do texture_coords
.add_all v
76 redef var center
= new Point3d[Float](0.0, 0.0, 0.0) is lazy
78 init do indices
.add_all
([0, 1, 2, 2, 1, 3])
79 # TODO use gl_TRIANGLE_FAN instead
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]
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]
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
108 redef var normals
is lazy
do
109 var normals
= new Array[Float]
110 var faces_normals
= [
117 for f
in faces_normals
do for i
in 6.times
do normals
.add_all f
121 redef var texture_coords
: Array[Float] is lazy
do
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
133 redef var center
= new Point3d[Float](0.0, 0.0, 0.0) is lazy
136 # Sphere with `radius` and a number of faces set by `n_meridians` and `n_parallels`
140 # Distance between the center and the vertices
143 # Number of vertices on a full circle around the Z axis
146 # Number of vertices on an arc between both poles
154 var vertices
= new Array[Float].with_capacity
(w
*h
*3)
155 self.vertices
= vertices
157 var texture_coords
= new Array[Float].with_capacity
(w
*h
*2)
158 self.texture_coords
= texture_coords
160 var normals
= new Array[Float].with_capacity
(w
*h
*3)
161 self.normals
= normals
166 var u
= m
.to_f
* 2.0 * pi
/ (w-1
).to_f
167 var v
= p
.to_f
* pi
/ (h-1
).to_f
169 vertices
.add radius
* u
.cos
* v
.sin
170 vertices
.add radius
* v
.cos
171 vertices
.add radius
* u
.sin
* v
.sin
173 texture_coords
.add
(1.0 - m
.to_f
/(w-1
).to_f
)
174 texture_coords
.add
(p
.to_f
/(h-1
).to_f
)
176 normals
.add u
.cos
* v
.sin
178 normals
.add u
.sin
* v
.sin
183 var indices
= new Array[Int].with_capacity
((w-1
)*(h-1
)*6)
184 self.indices
= indices