lib/gamnit depth: intro `Plane::repeat_x|y`
[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 # Modifier to `texture_coords` to repeat the texture on the X axis
28 #
29 # At 1.0, the texture is stretched to cover the whole plane.
30 # If greater than 1.0, the texture is repeated.
31 #
32 # This value must be set before using `texture_coords` or drawing this plane.
33 var repeat_x = 1.0 is writable
34
35 # Modifier to `texture_coords` to repeat the texture on the Y axis
36 #
37 # At 1.0, the texture is stretched to cover the whole plane.
38 # If greater than 1.0, the texture is repeated.
39 #
40 # This value must be set before using `texture_coords` or drawing this plane.
41 var repeat_y = 1.0 is writable
42
43 redef var vertices is lazy do
44 var a = [-0.5, 0.0, -0.5]
45 var b = [ 0.5, 0.0, -0.5]
46 var c = [-0.5, 0.0, 0.5]
47 var d = [ 0.5, 0.0, 0.5]
48
49 var vertices = new Array[Float]
50 for v in [c, d, a, b] do vertices.add_all v
51 return vertices
52 end
53
54 redef var normals: Array[Float] is lazy do
55 var normals = new Array[Float]
56 for i in 4.times do normals.add_all([0.0, 1.0, 0.0])
57 return normals
58 end
59
60 redef var texture_coords: Array[Float] is lazy do
61 var offset_left = 0.0
62 var offset_top = 0.0
63 var offset_right = 1.0*repeat_x
64 var offset_bottom = 1.0*repeat_y
65 var a = [offset_left, offset_bottom]
66 var b = [offset_right, offset_bottom]
67 var c = [offset_left, offset_top]
68 var d = [offset_right, offset_top]
69
70 var texture_coords = new Array[Float]
71 for v in [c, d, a, b] do texture_coords.add_all v
72 return texture_coords
73 end
74
75 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
76
77 init do indices.add_all([0, 1, 2, 2, 1, 3])
78 # TODO use gl_TRIANGLE_FAN instead
79 end
80
81 # Cube, with 6 faces
82 class Cube
83 super Mesh
84
85 redef var vertices is lazy do
86 var a = [-0.5, -0.5, -0.5]
87 var b = [ 0.5, -0.5, -0.5]
88 var c = [-0.5, 0.5, -0.5]
89 var d = [ 0.5, 0.5, -0.5]
90
91 var e = [-0.5, -0.5, 0.5]
92 var f = [ 0.5, -0.5, 0.5]
93 var g = [-0.5, 0.5, 0.5]
94 var h = [ 0.5, 0.5, 0.5]
95
96 var vertices = new Array[Float]
97 for v in [a, c, d, a, d, b, # front
98 f, h, g, f, g, e, # back
99 b, d, h, b, h, f, # right
100 e, g, c, e, c, a, # left
101 e, a, b, e, b, f, # bottom
102 c, g, h, c, h, d # top
103 ] do vertices.add_all v
104 return vertices
105 end
106
107 redef var normals is lazy do
108 var normals = new Array[Float]
109 var faces_normals = [
110 [0.0, 0.0, -1.0],
111 [0.0, 0.0, 1.0],
112 [ 1.0, 0.0, 0.0],
113 [-1.0, 0.0, 0.0],
114 [0.0, -1.0, 0.0],
115 [0.0, 1.0, 0.0]]
116 for f in faces_normals do for i in 6.times do normals.add_all f
117 return normals
118 end
119
120 redef var texture_coords: Array[Float] is lazy do
121 var a = [0.0, 1.0]
122 var b = [1.0, 1.0]
123 var c = [0.0, 0.0]
124 var d = [1.0, 0.0]
125
126 var texture_coords = new Array[Float]
127 for v in [c, d, a, a, d, b] do for i in 6.times do texture_coords.add_all v
128 return texture_coords
129 end
130
131 redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
132 end