Services to parse .mtl material files

Introduced classes

class MtlDef

gamnit :: MtlDef

Material defined in a .mtl file
class MtlFileParser

gamnit :: MtlFileParser

Parser of .mtl files

All class definitions

class MtlDef

gamnit $ MtlDef

Material defined in a .mtl file
class MtlFileParser

gamnit $ MtlFileParser

Parser of .mtl files
package_diagram gamnit::mtl mtl gamnit::model_parser_base model_parser_base gamnit::mtl->gamnit::model_parser_base parser_base parser_base gamnit::model_parser_base->parser_base ...parser_base ... ...parser_base->parser_base gamnit::more_models more_models gamnit::more_models->gamnit::mtl 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 array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
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 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 gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
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 meta

meta :: meta

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

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module parser_base

parser_base :: parser_base

Simple base for hand-made parsers of all kinds
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 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 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_parser_base

gamnit :: model_parser_base

Services to parse models from a text description

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
# Services to parse .mtl material files
module mtl

import model_parser_base

# Parser of `.mtl` files
#
# ~~~
# var mtl_src = """
# # Green material with low reflections
# newmtl GreenMaterial
# Ns 96.078431
# Ka 0.000000 0.000000 0.000000
# Kd 0.027186 0.180434 0.012088
# Ks 0.500000 0.500000 0.500000
# Ni 1.000000
# d 1.000000
# illum 2
# """
#
# var parser = new MtlFileParser(mtl_src)
# var name_to_mtls = parser.parse
# assert name_to_mtls.keys.join == "GreenMaterial"
# ~~~
class MtlFileParser
	super StringProcessor

	# Read and parse source and return all materials organized by their names
	fun parse: Map[String, MtlDef]
	do
		var mat_lib = new Map[String, MtlDef]
		var material: nullable MtlDef = null

		while not eof do
			var token = read_token
			if token == "newmtl" then
				var name = read_until_eol_or_comment
				material = new MtlDef(name)
				mat_lib[name] = material
			else if material != null then
				if token == "Ka" then
					material.ambient = read_vec3
				else if token == "Kd" then
					material.diffuse = read_vec3
				else if token == "Ks" then
					material.specular = read_vec3
				else if token == "d" then
					material.dissolved = read_number
				else if token == "Tr" then
					material.dissolved = 1.0 - read_number
				else if token == "illum" then
					material.illumination_model = read_number.to_i
				else if token == "map_Ka" then
					material.map_ambient = read_until_eol_or_comment
				else if token == "map_Kd" then
					material.map_diffuse = read_until_eol_or_comment
				else if token == "map_Bump" then
					material.map_bump = read_until_eol_or_comment
				else if token == "map_Ks" then
					material.map_specular = read_until_eol_or_comment
				else if token == "map_Ns" then
					material.map_exponent = read_until_eol_or_comment

				# TODO other line type headers
				else if token == "Ns" then
				else if token == "Ni" then
				else if token == "sharpness" then
				else if token == "bump" then
				end
			end
			skip_eol
		end

		return mat_lib
	end
end

# Material defined in a `.mtl` file
class MtlDef

	# Name of this material
	var name: String

	# Ambient color
	var ambient = new Vec3 is lazy

	# Diffuse color
	var diffuse = new Vec3 is lazy

	# Specular color
	var specular = new Vec3 is lazy

	# Dissolved level, where 1.0 is fully visible
	var dissolved = 1.0

	# Transparency level, where 1.0 is fully invisible
	fun transparency: Float do return 1.0 - dissolved

	# Illumination model
	var illumination_model = 0

	# Ambient map
	var map_ambient: nullable String = null

	# Diffuse map
	var map_diffuse: nullable String = null

	# Bump map or normals texture
	var map_bump: nullable String = null

	# Specular reflectivity map
	var map_specular: nullable String = null

	# Specular exponent map
	var map_exponent: nullable String = null

	# Collect non-null maps from `map_diffuse, map_bump, map_specular, map_exponent`
	fun maps: Array[String]
	do
		return [for m in [map_ambient, map_diffuse, map_bump, map_specular, map_exponent] do if m != null then m]
	end
end
lib/gamnit/model_parsers/mtl.nit:15,1--136,3