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"

Introduced properties

fun parse: Map[String, MtlDef]

gamnit :: MtlFileParser :: parse

Read and parse source and return all materials organized by their names

Redefined properties

redef type SELF: MtlFileParser

gamnit $ MtlFileParser :: 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 current_location: Location

parser_base :: StringProcessor :: current_location

Gives the current location in the src
protected fun eof: Bool

parser_base :: StringProcessor :: eof

Is pos at the end of the source?
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.
protected fun hot_location: Location

parser_base :: StringProcessor :: hot_location

Returns the current location as a Location object
protected fun ignore_until(s: String): Int

parser_base :: StringProcessor :: ignore_until

Reads characters until pattern s is found
protected fun ignore_until_whitespace: Int

parser_base :: StringProcessor :: ignore_until_whitespace

Ignores any printable character until a whitespace is encountered
protected fun ignore_until_whitespace_or_comment: Int

parser_base :: StringProcessor :: ignore_until_whitespace_or_comment

Advance pos until a whitespace or # is encountered
protected fun ignore_whitespaces

parser_base :: StringProcessor :: ignore_whitespaces

Advances in src until a non-whitespace character is encountered
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.
protected fun len: Int

parser_base :: StringProcessor :: len

Length of the source document
protected fun len=(len: Int)

parser_base :: StringProcessor :: len=

Length of the source document
protected fun line: Int

parser_base :: StringProcessor :: line

Current line in src
protected fun line=(line: Int)

parser_base :: StringProcessor :: line=

Current line in src
protected fun line_offset: Int

parser_base :: StringProcessor :: line_offset

Offset in the current line
protected fun line_start: Int

parser_base :: StringProcessor :: line_start

Position at which current line started
protected fun line_start=(line_start: Int)

parser_base :: StringProcessor :: line_start=

Position at which current line started
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 parse: Map[String, MtlDef]

gamnit :: MtlFileParser :: parse

Read and parse source and return all materials organized by their names
protected fun pos: Int

parser_base :: StringProcessor :: pos

Current position in src
protected fun pos=(pos: Int)

parser_base :: StringProcessor :: pos=

Current position in src
protected fun read_number: Float

parser_base :: StringProcessor :: read_number

Read a token and parse it as a Float
protected fun read_token: String

parser_base :: StringProcessor :: read_token

Read a single token after skipping preceding whitespaces
protected fun read_until_eol_or_comment: String

parser_base :: StringProcessor :: read_until_eol_or_comment

Advance pos until the next end of line or a #
protected fun read_vec3: Vec3

parser_base :: StringProcessor :: read_vec3

Read 2 or 3 numbers and return them as a Vec3
protected fun read_vec4: Vec4

parser_base :: StringProcessor :: read_vec4

Read 3 or 4 numbers and return them as a Vec4
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
protected fun skip_eol

parser_base :: StringProcessor :: skip_eol

Advance pos to skip the next end of line
protected fun src: String

parser_base :: StringProcessor :: src

Source document to parse
protected fun src=(src: String)

parser_base :: StringProcessor :: src=

Source document to parse
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::MtlFileParser MtlFileParser parser_base::StringProcessor StringProcessor gamnit::MtlFileParser->parser_base::StringProcessor core::Object Object parser_base::StringProcessor->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

class StringProcessor

parser_base :: StringProcessor

Basic facilities for common parser operations on String sources

Class definitions

gamnit $ MtlFileParser
# 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
lib/gamnit/model_parsers/mtl.nit:20,1--90,3