1c6edf4c765b43a8c42146529726ac2b5eadd06d
[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
21 # Simple flat mesh, sits on the axes X and Z, normal on Y
22 class Plane
23 super Mesh
24
25 # TODO allow for complex rotation, either at creation or in Actor
26
27 redef var vertices is lazy do
28 var a = [-0.5, 0.0, -0.5]
29 var b = [ 0.5, 0.0, -0.5]
30 var c = [-0.5, 0.0, 0.5]
31 var d = [ 0.5, 0.0, 0.5]
32
33 var vertices = new Array[Float]
34 for v in [c, d, a, b] do vertices.add_all v
35 return vertices
36 end
37
38 redef var normals: Array[Float] is lazy do
39 var normals = new Array[Float]
40 for i in 4.times do normals.add_all([0.0, 1.0, 0.0])
41 return normals
42 end
43
44 redef var texture_coords: Array[Float] is lazy do
45 var offset_left = 0.0
46 var offset_top = 0.0
47 var offset_right = 1.0
48 var offset_bottom = 1.0
49 var a = [offset_left, offset_bottom]
50 var b = [offset_right, offset_bottom]
51 var c = [offset_left, offset_top]
52 var d = [offset_right, offset_top]
53
54 var texture_coords = new Array[Float]
55 for v in [c, d, a, b] do texture_coords.add_all v
56 return texture_coords
57 end
58
59 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
60
61 init do indices.add_all([0, 1, 2, 2, 1, 3])
62 # TODO use gl_TRIANGLE_FAN instead
63 end
64
65 # Cube, with 6 faces
66 class Cube
67 super Mesh
68
69 redef var vertices is lazy do
70 var a = [-0.5, -0.5, -0.5]
71 var b = [ 0.5, -0.5, -0.5]
72 var c = [-0.5, 0.5, -0.5]
73 var d = [ 0.5, 0.5, -0.5]
74
75 var e = [-0.5, -0.5, 0.5]
76 var f = [ 0.5, -0.5, 0.5]
77 var g = [-0.5, 0.5, 0.5]
78 var h = [ 0.5, 0.5, 0.5]
79
80 var vertices = new Array[Float]
81 for v in [a, c, d, a, d, b, # front
82 f, h, g, f, g, e, # back
83 b, d, h, b, h, f, # right
84 e, g, c, e, c, a, # left
85 e, a, b, e, b, f, # bottom
86 c, g, h, c, h, d # top
87 ] do vertices.add_all v
88 return vertices
89 end
90
91 redef var normals is lazy do
92 var normals = new Array[Float]
93 var faces_normals = [
94 [0.0, 0.0, -1.0],
95 [0.0, 0.0, 1.0],
96 [ 1.0, 0.0, 0.0],
97 [-1.0, 0.0, 0.0],
98 [0.0, -1.0, 0.0],
99 [0.0, 1.0, 0.0]]
100 for f in faces_normals do for i in 6.times do normals.add_all f
101 return normals
102 end
103
104 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
105 end