Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); tinyobj · AdeptLanguage/Adept Wiki · GitHub
Skip to content
IsaacShelton edited this page Nov 13, 2022 · 5 revisions

tinyobj

tinyobj/tinyobj.adept is a port of the TinyOBJLoader-C library.

It can be used to load basic .obj 3D model files.

Structures

struct tinyobj_material_t (
name *ubyte,
ambient 3 float,
diffuse 3 float,
specular 3 float,
transmittance 3 float,
emission 3 float,
shininess float,
ior float, // index of refraction
dissolve float, // 1 == opaque, 0 == transparent
illum int, // illumination
pad0 int,
ambient_texname *ubyte,
diffuse_texname *ubyte,
specular_texname *ubyte,
specular_highlight_texname *ubyte,
bump_texname *ubyte,
displacement_texname *ubyte,
alpha_texname *ubyte
)
struct tinyobj_shape_t (
name *ubyte, // group or object name
face_offset uint,
length uint
)
struct tinyobj_vertex_index_t (v_idx, vt_idx, vn_idx int)
struct tinyobj_attrib_t (
num_vertices uint,
num_normals uint,
num_texcoords uint,
num_faces uint,
num_face_num_verts uint,
pad0 int,
vertices *float,
normals *float,
texcoords *float,
faces *tinyobj_vertex_index_t,
face_num_verts *int,
material_ids *int
)

Constants

define TINYOBJ_FLAG_TRIANGULATE = 1
define TINYOBJ_INVALID_INDEX = 0x80000000
define TINYOBJ_SUCCESS = 0
define TINYOBJ_ERROR_EMPTY = -1
define TINYOBJ_ERROR_INVALID_PARAMETER = -2
define TINYOBJ_ERROR_FILE_OPERATION = -3
define TINYOBJ_MAX_FACES_PER_F_LINE = 16

Functions

  • func tinyobj_parse_obj(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint) int

  • func tinyobj_parse_obj_ex(attrib *tinyobj_attrib_t, shapes **tinyobj_shape_t, num_shapes *usize, materials_out **tinyobj_material_t, num_materials_out *usize, buf *ubyte, len usize, flags uint, material_path *ubyte) int

  • func tinyobj_attrib_free(attrib *tinyobj_attrib_t) void

  • func tinyobj_shapes_free(shapes *tinyobj_shape_t, num_shapes usize) void

  • func tinyobj_materials_free(materials *tinyobj_material_t, num_materials usize) void

  • Lesser used functions omitted

Some Sample Code


#default get_file_contents_32_bit false
#unless get_file_contents_32_bit
#if __windows__
foreign fseeko64(*FILE, long, int) int
foreign ftello64(*FILE) long
#else
import 'unix/off_t.adept'
foreign fseeko(*FILE, off_t, int) int
foreign ftello(*FILE) off_t
#end
#end
import cstdio
import cstdlib
import String
import List
import Array
import Vector2f
import Vector3f
import array_util
import list_util
// Some Abstract OpenGL Helper Utilities
import 'VAO.adept'
import 'EBO.adept'
import 'Texture.adept'
// A Generic 3D Model with a Single Texture
struct Model (vao VAO, ebo EBO, texture *Texture) {
func destroy {
this.vao.destroy()
this.ebo.destroy()
// Texture is not destroyed
}
func render {
this.vao.bind()
this.vao.enableAttribArrays()
this.ebo.bind()
if this.texture {
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, this.texture.id)
}
glDrawElements(GL_TRIANGLES, this.ebo.size, GL_UNSIGNED_INT, null)
this.ebo.unbind()
this.vao.disableAttribArrays()
this.vao.unbind()
}
}
func model(vao POD VAO, ebo POD EBO, texture *Texture) Model {
model POD Model
model.vao = POD vao
model.ebo = POD ebo
model.texture = texture
return model
}
func model(filename String, texture *Texture) Model {
empty_model POD Model
filename_cstr *ubyte = filename.cstr()
defer delete filename_cstr
attrib tinyobj_attrib_t
shapes *tinyobj_shape_t = null
num_shapes usize = undef
materials *tinyobj_material_t = null
num_materials usize = undef
data_len usize = 0
data *ubyte = getFileContentsAsNullTerminatedString(filename_cstr, &data_len)
if data == null {
fprintf(stderr, 'model(String, *Texture) failed to read file "%s"\n', filename_cstr)
return empty_model
}
defer delete data
material_path_cstr *ubyte = null
defer delete material_path_cstr
last_slash long = max(filename.last('/'ub), filename.last('\\'ub))
if last_slash >= 0 {
material_path String = filename.range(0, last_slash + 1)
material_path_cstr = material_path.cstr()
}
flags uint = TINYOBJ_FLAG_TRIANGULATE
ret int = tinyobj_parse_obj_ex(&attrib, &shapes, &num_shapes, &materials, &num_materials, data, data_len, flags, material_path_cstr)
if ret != TINYOBJ_SUCCESS, fprintf(stderr, 'model(String, *Texture) failed to load model "%s"\n', filename_cstr); return empty_model
vertices <float> List
texture_coords <float> List
normals <float> List
indices <uint> List
tangents <float> List
each tinyobj_vertex_index_t in [attrib.faces, attrib.num_faces] {
vertices.add(attrib.vertices[it.v_idx * 3 + 0])
vertices.add(attrib.vertices[it.v_idx * 3 + 1])
vertices.add(attrib.vertices[it.v_idx * 3 + 2])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 0])
texture_coords.add(attrib.texcoords[it.vt_idx * 2 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 0])
normals.add(attrib.normals[it.vn_idx * 3 + 1])
normals.add(attrib.normals[it.vn_idx * 3 + 2])
// Lazy indices
indices.add(indices.length as uint)
}
tangents = computeTangents(vertices, texture_coords, normals, indices)
vertices_vbo POD VBO = vbo(VBOType::VERTICES, array(vertices.items, vertices.length))
texture_coords_vbo POD VBO = vbo(VBOType::TEXTURE_COORDS, array(texture_coords.items, texture_coords.length))
normals_vbo POD VBO = vbo(VBOType::NORMALS, array(normals.items, normals.length))
tangents_vbo POD VBO = vbo(VBOType::TANGENTS, array(tangents.items, tangents.length))
vao POD VAO = POD vao()
vao.addVBO(vertices_vbo)
vao.addVBO(texture_coords_vbo)
vao.addVBO(normals_vbo)
vao.addVBO(tangents_vbo)
ebo POD EBO = ebo(array(indices.items, indices.length))
defer tinyobj_attrib_free(&attrib)
defer tinyobj_shapes_free(shapes, num_shapes)
defer tinyobj_materials_free(materials, num_materials)
return model(vao, ebo, texture)
}
func computeTangents(vertices, uvs, normals <float> List, indices <uint> List) <float> List {
vertex_count usize = vertices.length / 3
tan_a <Vector3f> List
repeat vertex_count, tan_a.add(vector3f(0.0f, 0.0f, 0.0f))
i usize = 0; while i < indices.length {
i0 uint = indices.get(i)
i1 uint = indices.get(i + 1)
i2 uint = indices.get(i + 2)
pos0 Vector3f = vector3f(vertices.get(i0 * 3 + 0), vertices.get(i0 * 3 + 1), vertices.get(i0 * 3 + 2))
pos1 Vector3f = vector3f(vertices.get(i1 * 3 + 0), vertices.get(i1 * 3 + 1), vertices.get(i1 * 3 + 2))
pos2 Vector3f = vector3f(vertices.get(i2 * 3 + 0), vertices.get(i2 * 3 + 1), vertices.get(i2 * 3 + 2))
tex0 Vector2f = vector2f(uvs.get(i0 * 2 + 0), uvs.get(i0 * 2 + 1))
tex1 Vector2f = vector2f(uvs.get(i1 * 2 + 0), uvs.get(i1 * 2 + 1))
tex2 Vector2f = vector2f(uvs.get(i2 * 2 + 0), uvs.get(i2 * 2 + 1))
edge1 Vector3f = pos1 - pos0
edge2 Vector3f = pos2 - pos0
uv1 Vector2f = tex1 - tex0
uv2 Vector2f = tex2 - tex0
r float = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x)
tangent Vector3f = vector3f(
((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,
((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,
((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r)
tan_a.getPointer(i0).add(tangent)
tan_a.getPointer(i1).add(tangent)
tan_a.getPointer(i2).add(tangent)
i += 3
}
result <float> List
repeat vertex_count {
n Vector3f = vector3f(normals.get(idx * 3), normals.get(idx * 3 + 1), normals.get(idx * 3 + 2))
t0 Vector3f = tan_a.get(idx)
t Vector3f = t0 - (n * n.dot(t0))
t.normalize()
result.add(t.x)
result.add(t.y)
result.add(t.z)
}
return result
}
func getFileContentsAsNullTerminatedString(in filename *ubyte, out size *usize) *ubyte {
// NOTE: Only works for files smaller than ~4GB
// NOTE: Might work with files larger than ~4GB now, haven't tested yet
buffer *ubyte = null
length long = undef
f *FILE = fopen(filename, 'r')
unless f, return null
defer fclose(f)
#if get_file_contents_32_bit
fseek(f, 0, SEEK_END)
length = ftell(f)
fseek(f, 0, SEEK_SET)
#elif __windows__
fseeko64(f, 0, SEEK_END)
length = ftello64(f)
fseeko64(f, 0, SEEK_SET)
#else
fseeko(f, 0, SEEK_END)
length = ftello(f)
fseeko(f, 0, SEEK_SET)
#end
buffer = malloc(length + 1)
if buffer {
if fread(buffer, 1, length, f) != length, return null
buffer[length] = 0x00ub
}
*size = length
return buffer
}

Clone this wiki locally