# Coordinates on the texture per vertex
var texture_coords = new Array[Float] is lazy, writable
- # Create an UV sphere of `radius` with `n_meridians` and `n_parallels`
- init uv_sphere(radius: Float, n_meridians, n_parallels: Int)
- do
- var w = n_meridians
- var h = n_parallels
-
- var vertices = new Array[Float].with_capacity(w*h*3)
- self.vertices = vertices
-
- var texture_coords = new Array[Float].with_capacity(w*h*2)
- self.texture_coords = texture_coords
-
- var normals = new Array[Float].with_capacity(w*h*3)
- self.normals = normals
-
- # Build vertices
- for m in [0..w[ do
- for p in [0..h[ do
- var u = m.to_f * 2.0 * pi / (w-1).to_f
- var v = p.to_f * pi / (h-1).to_f
-
- vertices.add radius * u.cos * v.sin
- vertices.add radius * v.cos
- vertices.add radius * u.sin * v.sin
-
- texture_coords.add (1.0 - m.to_f/(w-1).to_f)
- texture_coords.add(p.to_f/(h-1).to_f)
-
- normals.add u.cos * v.sin
- normals.add v.cos
- normals.add u.sin * v.sin
- end
- end
-
- # Build faces
- var indices = new Array[Int].with_capacity((w-1)*(h-1)*6)
- self.indices = indices
- for m in [0..w-1[ do
- for p in [0..h-1[ do
- var a = m*h + p
-
- indices.add a
- indices.add a+h
- indices.add a+1
-
- indices.add a+h
- indices.add a+h+1
- indices.add a+1
- end
- end
- end
-
- # Dimensions of this geometry using the min and max of all points on each axis
- var dimensions: Point3d[Float] is lazy, writable do
- assert vertices.length % 3 == 0
-
- var minx = inf
- var miny = inf
- var minz = inf
- var maxx = -inf
- var maxy = -inf
- var maxz = -inf
-
- var i = 0
- while i < vertices.length do
- var x = vertices[i]
- i += 1
- var y = vertices[i]
- i += 1
- var z = vertices[i]
- i += 1
-
- minx = minx.min(x)
- miny = miny.min(y)
- minz = minz.min(z)
-
- maxx = maxx.max(x)
- maxy = maxy.max(y)
- maxz = maxz.max(z)
- end
-
- return new Point3d[Float](maxx-minx, maxy-miny, maxz-minz)
- end
-
- # Center of the geometry
- var center: Point3d[Float] is lazy, writable do
- assert vertices.length % 3 == 0
-
- var minx = inf
- var miny = inf
- var minz = inf
- var maxx = -inf
- var maxy = -inf
- var maxz = -inf
-
- var i = 0
- while i < vertices.length do
- var x = vertices[i]
- i += 1
- var y = vertices[i]
- i += 1
- var z = vertices[i]
- i += 1
-
- minx = minx.min(x)
- miny = miny.min(y)
- minz = minz.min(z)
-
- maxx = maxx.max(x)
- maxy = maxy.max(y)
- maxz = maxz.max(z)
- end
-
- var center = new Point3d[Float](
- (minx+maxx)/2.0,
- (miny+maxy)/2.0,
- (minz+maxz)/2.0)
- return center
- end
+ # `GLDrawMode` used to display this mesh, defaults to `gl_TRIANGLES`
+ fun draw_mode: GLDrawMode do return gl_TRIANGLES
end
# Source of light