Material for models, or how to draw the model

To create a simple basic blueish material, use new Material.

Each class of material is associated to a GLProgram and its GPU shaders. The simple material SmoothMaterial allows to set an ambient, diffuse and specular color. To which TextureMaterial adds three textures, for each kind of light. The NormalsMaterial may be useful for debugging, it show the orientation of the normal vectors as colors.

import gamnit::depth

var blueish_material = new Material
var redish_material = new SmoothMaterial([0.3, 0.0, 0.0],
                                         [0.6, 0.0, 0.0],
                                         [1.0, 1.0, 1.0])
var normals_material = new NormalsMaterial

Introduced properties

fun draw(actor: Actor, model: LeafModel, camera: Camera)

gamnit :: Material :: draw

Draw a model from actor
protected fun draw_depth(actor: Actor, model: LeafModel, camera: Camera)

gamnit :: Material :: draw_depth

Optimized draw of model, a part of actor, from the view of camera
protected fun draw_selection(actor: Actor, model: LeafModel, id: Int)

gamnit :: Material :: draw_selection

Draw actor to selection values
init new: Material

gamnit :: Material :: new

Get the default blueish material

Redefined properties

redef type SELF: Material

gamnit $ Material :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun draw(actor: Actor, model: LeafModel, camera: Camera)

gamnit :: Material :: draw

Draw a model from actor
protected fun draw_depth(actor: Actor, model: LeafModel, camera: Camera)

gamnit :: Material :: draw_depth

Optimized draw of model, a part of actor, from the view of camera
protected fun draw_selection(actor: Actor, model: LeafModel, id: Int)

gamnit :: Material :: draw_selection

Draw actor to selection values
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
init new: Material

gamnit :: Material :: new

Get the default blueish material
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::Material Material core::Object Object gamnit::Material->core::Object gamnit::SmoothMaterial SmoothMaterial gamnit::SmoothMaterial->gamnit::Material gamnit::NormalsMaterial NormalsMaterial gamnit::NormalsMaterial->gamnit::Material gamnit::TexturedMaterial TexturedMaterial gamnit::TexturedMaterial->gamnit::SmoothMaterial gamnit::TexturedMaterial... ... gamnit::TexturedMaterial...->gamnit::TexturedMaterial

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class NormalsMaterial

gamnit :: NormalsMaterial

Simple material using the normals of the surface as color
class SmoothMaterial

gamnit :: SmoothMaterial

Simple material with static colors

Descendants

class TexturedMaterial

gamnit :: TexturedMaterial

Material with potential diffuse_texture and specular_texture

Class definitions

gamnit $ Material
# Material for models, or how to draw the model
#
# To create a simple basic blueish material, use `new Material`.
#
# Each class of material is associated to a `GLProgram` and its GPU shaders.
# The simple material `SmoothMaterial` allows to set an ambient, diffuse and specular color.
# To which `TextureMaterial` adds three textures, for each kind of light.
# The `NormalsMaterial` may be useful for debugging, it show the orientation of
# the normal vectors as colors.
#
# ~~~
# import gamnit::depth
#
# var blueish_material = new Material
# var redish_material = new SmoothMaterial([0.3, 0.0, 0.0],
#                                          [0.6, 0.0, 0.0],
#                                          [1.0, 1.0, 1.0])
# var normals_material = new NormalsMaterial
# ~~~
abstract class Material

	# Draw a `model` from `actor`
	#
	# This method should be refined by subclasses as the default implementation is a no-op.
	#
	# This method is called on many materials for many `actor` and `model` at each frame.
	# It is expected to use a `GLProgram` and call an equivalent to `glDrawArrays`.
	# However, it should not call `glClear` nor `GamnitDisplay::flip`.
	fun draw(actor: Actor, model: LeafModel, camera: Camera) do end
end
lib/gamnit/depth/depth_core.nit:143,1--172,3

gamnit :: shadow $ Material
redef class Material
	# Optimized draw of `model`, a part of `actor`, from the view of `camera`
	#
	# This drawing should only produce usable depth data. The default behavior,
	# uses `shadow_depth_program`.
	protected fun draw_depth(actor: Actor, model: LeafModel, camera: Camera)
	do
		var program = app.shadow_depth_program
		program.use
		program.mvp.uniform camera.mvp_matrix

		var mesh = model.mesh

		program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
		program.scale.uniform actor.scale
		program.use_map_diffuse.uniform false

		program.tex_coord.array_enabled = true
		program.tex_coord.array(mesh.texture_coords, 2)

		program.coord.array_enabled = true
		program.coord.array(mesh.vertices, 3)

		program.rotation.uniform new Matrix.gamnit_euler_rotation(actor.pitch, actor.yaw, actor.roll)

		if mesh.indices.is_empty then
			glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
		else
			glDrawElements(mesh.draw_mode, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
		end
	end

end
lib/gamnit/depth/shadow.nit:310,1--342,3

gamnit :: more_materials $ Material
redef class Material
	# Get the default blueish material
	new do return new SmoothMaterial(
		[0.0, 0.0, 0.3, 1.0],
		[0.0, 0.0, 0.6, 1.0],
		[1.0, 1.0, 1.0, 1.0])
end
lib/gamnit/depth/more_materials.nit:23,1--29,3

gamnit :: selection $ Material
redef class Material

	# Draw `actor` to selection values
	protected fun draw_selection(actor: Actor, model: LeafModel, id: Int)
	do
		var program = app.selection_program
		var mesh = model.mesh

		draw_selection_texture(actor, model)

		program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
		program.scale.uniform actor.scale

		program.coord.array_enabled = true
		program.coord.array(mesh.vertices, 3)
		program.rotation.uniform new Matrix.gamnit_euler_rotation(actor.pitch, actor.yaw, actor.roll)

		var display = app.display
		assert display != null
		var r = display.red_bits
		var g = display.green_bits
		var b = display.blue_bits

		# Build ID as a color
		var p1 = id & ((2**r)-1)
		var p2 = id >> r & ((2**g)-1)
		var p3 = id >> (r+g) & ((2**b)-1)
		program.color_id.uniform(
			p1.to_f/((2**r)-1).to_f,
			p2.to_f/((2**g)-1).to_f,
			p3.to_f/((2**b)-1).to_f, 1.0)

		if mesh.indices.is_empty then
			glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
		else
			glDrawElements(mesh.draw_mode, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
		end
	end

	private fun draw_selection_texture(actor: Actor, model: LeafModel)
	do
		var program = app.selection_program
		program.use_map_diffuse.uniform false
	end
end
lib/gamnit/depth/selection.nit:145,1--189,3