From: Alexis Laferrière Date: Sun, 7 May 2017 03:45:24 +0000 (-0400) Subject: gamnit: move `Model::uv_sphere` to `UVSphere` X-Git-Url: http://nitlanguage.org gamnit: move `Model::uv_sphere` to `UVSphere` Signed-off-by: Alexis Laferrière --- diff --git a/contrib/model_viewer/src/globe.nit b/contrib/model_viewer/src/globe.nit index 5ad0407..8fe1158 100644 --- a/contrib/model_viewer/src/globe.nit +++ b/contrib/model_viewer/src/globe.nit @@ -63,13 +63,13 @@ class GlobeModel redef fun load do leaves.add new LeafModel( - new Mesh.uv_sphere(1.0, 2*n_parallels, n_parallels), + new UVSphere(1.0, 2*n_parallels, n_parallels), new GlobeMaterial.surface) leaves.add new LeafModel( - new Mesh.uv_sphere(1.1, 2*n_parallels, n_parallels), + new UVSphere(1.1, 2*n_parallels, n_parallels), new GlobeMaterial.clouds) leaves.add new LeafModel( - new Mesh.uv_sphere(1.2, 2*n_parallels, n_parallels), + new UVSphere(1.2, 2*n_parallels, n_parallels), new GlobeMaterial.atmo) end end diff --git a/contrib/model_viewer/src/model_viewer.nit b/contrib/model_viewer/src/model_viewer.nit index d61b3fe..0c2cae6 100644 --- a/contrib/model_viewer/src/model_viewer.nit +++ b/contrib/model_viewer/src/model_viewer.nit @@ -30,9 +30,9 @@ redef class App # All available models var models: Array[Model] = [ - new LeafModel(new Cube, new SmoothMaterial.default), - new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new SmoothMaterial.default), - new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new NormalsMaterial), + new LeafModel(new Cube, new Material), + new LeafModel(new UVSphere(4.0, 32, 16), new Material), + new LeafModel(new UVSphere(4.0, 32, 16), new NormalsMaterial), new Model("models/Tree_01.obj"), new Model("models/Oak_Fall_01.obj"), new Model("models/Quandtum_BA-2_v1_1.obj"), diff --git a/lib/gamnit/depth/depth_core.nit b/lib/gamnit/depth/depth_core.nit index 45863dc..1c3e916 100644 --- a/lib/gamnit/depth/depth_core.nit +++ b/lib/gamnit/depth/depth_core.nit @@ -105,58 +105,6 @@ class Mesh # `GLDrawMode` used to display this mesh, defaults to `gl_TRIANGLES` fun draw_mode: GLDrawMode do return gl_TRIANGLES - - # 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 end # Source of light diff --git a/lib/gamnit/depth/more_meshes.nit b/lib/gamnit/depth/more_meshes.nit index eeeb258..699e831 100644 --- a/lib/gamnit/depth/more_meshes.nit +++ b/lib/gamnit/depth/more_meshes.nit @@ -132,3 +132,68 @@ class Cube redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy end + +# Sphere with `radius` and a number of faces set by `n_meridians` and `n_parallels` +class UVSphere + super Mesh + + # Distance between the center and the vertices + var radius: Float + + # Number of vertices on a full circle around the Z axis + var n_meridians: Int + + # Number of vertices on an arc between both poles + var n_parallels: Int + + init + 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 +end