More simple geometric meshes

Introduced classes

class Cube

gamnit :: Cube

Cube, with 6 faces, edges of equal length and square angles
class Cuboid

gamnit :: Cuboid

Cuboid, or rectangular prism, with 6 faces and right angles
class Plane

gamnit :: Plane

Simple flat mesh, sits on the axes X and Z, normal on Y
class UVSphere

gamnit :: UVSphere

Sphere with radius and a number of faces set by n_meridians and n_parallels

Redefined classes

redef interface Boxed3d[N: Numeric]

gamnit :: more_meshes $ Boxed3d

An 3d abstract bounded object

All class definitions

redef interface Boxed3d[N: Numeric]

gamnit :: more_meshes $ Boxed3d

An 3d abstract bounded object
class Cube

gamnit $ Cube

Cube, with 6 faces, edges of equal length and square angles
class Cuboid

gamnit $ Cuboid

Cuboid, or rectangular prism, with 6 faces and right angles
class Plane

gamnit $ Plane

Simple flat mesh, sits on the axes X and Z, normal on Y
class UVSphere

gamnit $ UVSphere

Sphere with radius and a number of faces set by n_meridians and n_parallels
package_diagram gamnit::more_meshes more_meshes gamnit::model_dimensions model_dimensions gamnit::more_meshes->gamnit::model_dimensions gamnit::depth_core depth_core gamnit::model_dimensions->gamnit::depth_core ...gamnit::depth_core ... ...gamnit::depth_core->gamnit::depth_core gamnit::more_models more_models gamnit::more_models->gamnit::more_meshes gamnit::depth depth gamnit::depth->gamnit::more_models gamnit::depth... ... gamnit::depth...->gamnit::depth

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module angles

geometry :: angles

Angle related service using Float to represent an angle in radians
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module boxes

geometry :: boxes

Provides interfaces and classes to represent basic geometry needs.
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module caching

serialization :: caching

Services for caching serialization engines
module cameras

gamnit :: cameras

Camera services producing Model-View-Projection matrices
module cameras_cache

gamnit :: cameras_cache

Cache the Matrix produced by Camera::mvp_matrix
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module depth_core

gamnit :: depth_core

Base entities of the depth 3D game framework
module display

gamnit :: display

Abstract display services
module dynamic_resolution

gamnit :: dynamic_resolution

Virtual screen with a resolution independent from the real screen
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module flat_core

gamnit :: flat_core

Core services for the flat API for 2D games
module gamnit

gamnit :: gamnit

Game and multimedia framework for Nit
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module geometry

geometry :: geometry

Provides interfaces and classes to represent basic geometry needs.
module glesv2

glesv2 :: glesv2

OpenGL graphics rendering library for embedded systems, version 2.0
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module matrix

matrix :: matrix

Services for matrices of Float values
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module performance_analysis

performance_analysis :: performance_analysis

Services to gather information on the performance of events by categories
module points_and_lines

geometry :: points_and_lines

Interfaces and classes to represent basic geometry needs.
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module programs

gamnit :: programs

Services for graphical programs with shaders, attributes and uniforms
module projection

matrix :: projection

Services on Matrix to transform and project 3D coordinates
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module realtime

realtime :: realtime

Services to keep time of the wall clock time
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module model_dimensions

gamnit :: model_dimensions

Dimensions related services for Model and Mesh

Children

module more_models

gamnit :: more_models

Services to load models from the assets folder

Descendants

module a_star-m

a_star-m

module cardboard

gamnit :: cardboard

Update the orientation of world_camera at each frame using the head position given by android::cardboard
module depth

gamnit :: depth

Framework for 3D games in Nit
module stereoscopic_view

gamnit :: stereoscopic_view

Refine EulerCamera and App::frame_core_draw to get a stereoscopic view
module vr

gamnit :: vr

VR support for gamnit depth, for Android only
# More simple geometric meshes
module more_meshes

import geometry
import depth_core
import model_dimensions

# Simple flat mesh, sits on the axes X and Z, normal on Y
class Plane
	super Mesh

	# TODO allow for complex rotation, either at creation or in Actor

	# Modifier to `texture_coords` to repeat the texture on the X axis
	#
	# At 1.0, the texture is stretched to cover the whole plane.
	# If greater than 1.0, the texture is repeated.
	#
	# This value must be set before using `texture_coords` or drawing this plane.
	var repeat_x = 1.0 is writable

	# Modifier to `texture_coords` to repeat the texture on the Y axis
	#
	# At 1.0, the texture is stretched to cover the whole plane.
	# If greater than 1.0, the texture is repeated.
	#
	# This value must be set before using `texture_coords` or drawing this plane.
	var repeat_y = 1.0 is writable

	redef var vertices is lazy do
		var a = [-0.5, 0.0, -0.5]
		var b = [ 0.5, 0.0, -0.5]
		var c = [-0.5, 0.0,  0.5]
		var d = [ 0.5, 0.0,  0.5]

		var vertices = new Array[Float]
		for v in [c, d, a, b] do vertices.add_all v
		return vertices
	end

	redef var normals: Array[Float] is lazy do
		var normals = new Array[Float]
		for i in 4.times do normals.add_all([0.0, 1.0, 0.0])
		return normals
	end

	redef var texture_coords: Array[Float] is lazy do
		var offset_left = 0.0
		var offset_top = 0.0
		var offset_right = 1.0*repeat_x
		var offset_bottom = 1.0*repeat_y
		var a = [offset_left,  offset_bottom]
		var b = [offset_right, offset_bottom]
		var c = [offset_left,  offset_top]
		var d = [offset_right, offset_top]

		var texture_coords = new Array[Float]
		for v in [c, d, a, b] do texture_coords.add_all v
		return texture_coords
	end

	redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy

	init do indices.add_all([0, 1, 2, 2, 1, 3])
	# TODO use gl_TRIANGLE_FAN instead
end

# Cuboid, or rectangular prism, with 6 faces and right angles
#
# Can be created from a `Boxed3d` using `to_mesh`.
class Cuboid
	super Mesh

	# Width, on the X axis
	var width: Float

	# Height, on the Y axis
	var height: Float

	# Depth, on the Z axis
	var depth: Float

	redef var vertices is lazy do
		var a = [-0.5*width, -0.5*height, -0.5*depth]
		var b = [ 0.5*width, -0.5*height, -0.5*depth]
		var c = [-0.5*width,  0.5*height, -0.5*depth]
		var d = [ 0.5*width,  0.5*height, -0.5*depth]

		var e = [-0.5*width, -0.5*height,  0.5*depth]
		var f = [ 0.5*width, -0.5*height,  0.5*depth]
		var g = [-0.5*width,  0.5*height,  0.5*depth]
		var h = [ 0.5*width,  0.5*height,  0.5*depth]

		var vertices = new Array[Float]
		for v in [a, c, d, a, d, b, # front
		          f, h, g, f, g, e, # back
		          b, d, h, b, h, f, # right
		          e, g, c, e, c, a, # left
		          e, a, b, e, b, f, # bottom
		          c, g, h, c, h, d  # top
				  ] do vertices.add_all v
		return vertices
	end

	redef var normals is lazy do
		var normals = new Array[Float]
		var faces_normals = [
			[0.0, 0.0, -1.0],
			[0.0, 0.0,  1.0],
			[ 1.0, 0.0, 0.0],
			[-1.0, 0.0, 0.0],
			[0.0, -1.0, 0.0],
			[0.0,  1.0, 0.0]]
		for f in faces_normals do for i in 6.times do normals.add_all f
		return normals
	end

	redef var texture_coords: Array[Float] is lazy do
		var a = [0.0, 1.0]
		var b = [1.0, 1.0]
		var c = [0.0, 0.0]
		var d = [1.0, 0.0]

		var texture_coords = new Array[Float]
		var face = [a, c, d, a, d, b]
		for i in 6.times do for v in face do texture_coords.add_all v
		return texture_coords
	end

	redef var center = new Point3d[Float](0.0, 0.0, 0.0) is lazy
end

# Cube, with 6 faces, edges of equal length and square angles
#
# Occupies `[-0.5..0.5]` on all three axes.
class Cube
	super Cuboid

	noautoinit

	init
	do
		width = 1.0
		height = 1.0
		depth = 1.0
	end
end

redef class Boxed3d[N]
	# Create a `Cuboid` mesh with the dimension of `self`
	#
	# Does not use the position of `self`, but it can be given to an `Actor`.
	fun to_mesh: Cuboid
	do
		var width = right.to_f-left.to_f
		var height = top.to_f-bottom.to_f
		var depth = front.to_f-back.to_f
		return new Cuboid(width, height, depth)
	end
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
lib/gamnit/depth/more_meshes.nit:15,1--239,3