Material with potential diffuse_texture and specular_texture

Introduced properties

fun ambient_texture: nullable Texture

gamnit :: TexturedMaterial :: ambient_texture

Texture applied to the ambient_color
fun ambient_texture=(ambient_texture: nullable Texture)

gamnit :: TexturedMaterial :: ambient_texture=

Texture applied to the ambient_color
init defaultinit(ambient_color: Array[Float], diffuse_color: Array[Float], specular_color: Array[Float])

gamnit :: TexturedMaterial :: defaultinit

fun diffuse_texture: nullable Texture

gamnit :: TexturedMaterial :: diffuse_texture

Texture applied to the diffuse color
fun diffuse_texture=(diffuse_texture: nullable Texture)

gamnit :: TexturedMaterial :: diffuse_texture=

Texture applied to the diffuse color
fun normals_texture=(normals_texture: nullable Texture)

gamnit :: TexturedMaterial :: normals_texture=

Bump map TODO
fun specular_texture: nullable Texture

gamnit :: TexturedMaterial :: specular_texture

Texture applied to the specular color
fun specular_texture=(specular_texture: nullable Texture)

gamnit :: TexturedMaterial :: specular_texture=

Texture applied to the specular color

Redefined properties

redef type SELF: TexturedMaterial

gamnit $ TexturedMaterial :: SELF

Type of this instance, automatically specialized in every class
redef fun draw(actor: Actor, model: LeafModel, camera: Camera)

gamnit $ TexturedMaterial :: draw

Draw a model from actor

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
fun ambient_color: Array[Float]

gamnit :: SmoothMaterial :: ambient_color

Ambient color, always visible
fun ambient_color=(ambient_color: Array[Float])

gamnit :: SmoothMaterial :: ambient_color=

Ambient color, always visible
fun ambient_texture: nullable Texture

gamnit :: TexturedMaterial :: ambient_texture

Texture applied to the ambient_color
fun ambient_texture=(ambient_texture: nullable Texture)

gamnit :: TexturedMaterial :: ambient_texture=

Texture applied to the ambient_color
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.
init defaultinit(ambient_color: Array[Float], diffuse_color: Array[Float], specular_color: Array[Float])

gamnit :: TexturedMaterial :: defaultinit

init defaultinit(ambient_color: Array[Float], diffuse_color: Array[Float], specular_color: Array[Float])

gamnit :: SmoothMaterial :: defaultinit

fun diffuse_color: Array[Float]

gamnit :: SmoothMaterial :: diffuse_color

Diffuse color when covered by a light source
fun diffuse_color=(diffuse_color: Array[Float])

gamnit :: SmoothMaterial :: diffuse_color=

Diffuse color when covered by a light source
fun diffuse_texture: nullable Texture

gamnit :: TexturedMaterial :: diffuse_texture

Texture applied to the diffuse color
fun diffuse_texture=(diffuse_texture: nullable Texture)

gamnit :: TexturedMaterial :: diffuse_texture=

Texture applied to the diffuse color
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
fun normals_texture=(normals_texture: nullable Texture)

gamnit :: TexturedMaterial :: normals_texture=

Bump map TODO
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
fun specular_color: Array[Float]

gamnit :: SmoothMaterial :: specular_color

Specular color affecting reflections
fun specular_color=(specular_color: Array[Float])

gamnit :: SmoothMaterial :: specular_color=

Specular color affecting reflections
fun specular_texture: nullable Texture

gamnit :: TexturedMaterial :: specular_texture

Texture applied to the specular color
fun specular_texture=(specular_texture: nullable Texture)

gamnit :: TexturedMaterial :: specular_texture=

Texture applied to the specular color
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::TexturedMaterial TexturedMaterial gamnit::SmoothMaterial SmoothMaterial gamnit::TexturedMaterial->gamnit::SmoothMaterial gamnit::Material Material gamnit::SmoothMaterial->gamnit::Material ...gamnit::Material ... ...gamnit::Material->gamnit::Material

Ancestors

abstract class Material

gamnit :: Material

Material for models, or how to draw the model
interface Object

core :: Object

The root of the class hierarchy.

Parents

class SmoothMaterial

gamnit :: SmoothMaterial

Simple material with static colors

Class definitions

gamnit $ TexturedMaterial
# Material with potential `diffuse_texture` and `specular_texture`
class TexturedMaterial
	super SmoothMaterial

	# Texture applied to the ambient_color
	var ambient_texture: nullable Texture = null is writable

	# Texture applied to the diffuse color
	var diffuse_texture: nullable Texture = null is writable

	# Texture applied to the specular color
	var specular_texture: nullable Texture = null is writable

	# Bump map TODO
	private var normals_texture: nullable Texture = null is writable

	redef fun draw(actor, model, camera)
	do
		var mesh = model.mesh

		var program = app.blinn_phong_program
		program.use

		# One of the textures used, if any
		var sample_used_texture = null

		var texture = ambient_texture
		if texture != null then
			glActiveTexture gl_TEXTURE0
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_map_ambient.uniform true
			program.map_ambient.uniform 0
			sample_used_texture = texture
		else
			program.use_map_ambient.uniform false
		end

		texture = diffuse_texture
		if texture != null then
			glActiveTexture gl_TEXTURE1
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_map_diffuse.uniform true
			program.map_diffuse.uniform 1
			sample_used_texture = texture
		else
			program.use_map_diffuse.uniform false
		end

		texture = specular_texture
		if texture != null then
			glActiveTexture gl_TEXTURE2
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_map_specular.uniform true
			program.map_specular.uniform 2
			sample_used_texture = texture
		else
			program.use_map_specular.uniform false
		end

		texture = normals_texture
		if texture != null then
			glActiveTexture gl_TEXTURE3
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_map_bump.uniform true
			program.map_bump.uniform 3
			sample_used_texture = texture
		else
			program.use_map_bump.uniform false
		end

		glDisableVertexAttribArray program.translation.location
		glDisableVertexAttribArray program.scale.location

		program.mvp.uniform camera.mvp_matrix
		program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
		program.scale.uniform actor.scale
		program.alpha.uniform actor.alpha

		# If using a texture, set `texture_coords`
		program.tex_coord.array_enabled = sample_used_texture != null
		if sample_used_texture != null then
			if sample_used_texture isa RootTexture then
				# Coordinates are directly valid
				program.tex_coord.array(mesh.texture_coords, 2)
			else
				# Correlate texture coordinates from the substexture and the mesh.
				# This is slow, but should be cached on the GPU.
				var xa = sample_used_texture.offset_left
				var xd = sample_used_texture.offset_right - xa
				var ya = sample_used_texture.offset_top
				var yd = sample_used_texture.offset_bottom - ya

				var tex_coords = new Array[Float].with_capacity(mesh.texture_coords.length)
				for i in [0..mesh.texture_coords.length/2[ do
					tex_coords[i*2]   = xa + xd * mesh.texture_coords[i*2]
					tex_coords[i*2+1] = 1.0 - (ya + yd * mesh.texture_coords[i*2+1])
				end

				program.tex_coord.array(tex_coords, 2)
			end
		end

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

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

		program.ambient_color.uniform(ambient_color[0], ambient_color[1],
		                              ambient_color[2], ambient_color[3])
		program.diffuse_color.uniform(diffuse_color[0], diffuse_color[1],
		                              diffuse_color[2], diffuse_color[3])
		program.specular_color.uniform(specular_color[0], specular_color[1],
		                               specular_color[2], specular_color[3])

		program.normal.array_enabled = true
		program.normal.array(mesh.normals, 3)

		# Light
		setup_lights(camera, program)

		# Camera
		program.camera.uniform(camera.position.x, camera.position.y, camera.position.z)

		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/more_materials.nit:144,1--273,3

gamnit :: selection $ TexturedMaterial
redef class TexturedMaterial
	redef fun draw_selection_texture(actor, model)
	do
		var program = app.selection_program
		var mesh = model.mesh

		# One of the textures used, if any
		var sample_used_texture = null
		var texture = diffuse_texture
		if texture != null then
			glActiveTexture gl_TEXTURE1
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_map_diffuse.uniform true
			program.map_diffuse.uniform 1
			sample_used_texture = texture
		else
			program.use_map_diffuse.uniform false
		end

		# If using a texture, set `texture_coords`
		program.tex_coord.array_enabled = sample_used_texture != null
		if sample_used_texture != null then
			if sample_used_texture isa RootTexture then
				# Coordinates are directly valid
				program.tex_coord.array(mesh.texture_coords, 2)
			else
				# Correlate texture coordinates from the subtexture sand the mesh.
				# This is slow, but should be cached on the GPU.
				var xa = sample_used_texture.offset_left
				var xd = sample_used_texture.offset_right - xa
				var ya = sample_used_texture.offset_top
				var yd = sample_used_texture.offset_bottom - ya

				var tex_coords = new Array[Float].with_capacity(mesh.texture_coords.length)
				for i in [0..mesh.texture_coords.length/2[ do
					tex_coords[i*2]   = xa + xd * mesh.texture_coords[i*2]
					tex_coords[i*2+1] = ya + yd * mesh.texture_coords[i*2+1]
				end

				program.tex_coord.array(tex_coords, 2)
			end
		end
	end
end
lib/gamnit/depth/selection.nit:191,1--234,3