, and render that object on the canvas.Note: This import routine only imports basic vertex and face data. Normals and textures are ignored.# Title: opengl_render_obj_file
# Arguments:
# canvas - A canvas3d widget
# tags - Tags to apply to the newly created object
# filename - A path to a file in Wavefront Obj format
proc opengl_render_obj_file {canvas tags filename} {
set line_count 0
set vertex_count 0
set face_count 0
set group {}
set object {}
foreach line [split $text \n] {
switch [string range $line 0 1] {
"g" {
# Faces can be broken up into groups
# We implement them as tags
set group [lrange $line 1 end]
}
"o " {
# Modes can be broken up into multiple objects
# We implement them as tags
set object [lrange $line 1 end]
}
"v " {
set vertex_xyz([incr vertex_count]) [lrange $line 1 end]
}
"f " {
set coords {}
foreach point [lrange $line 1 end] {
set vertex [lindex [split $point /] 0]
lappend coords {*}$vertex_xyz($vertex)
}
$canvas create polygon ${coords} -tags [list {*}$tags {*}$group {*}$object]
}
}
}
}
}AK - 2013-05-01 16:00:17Other wiki pages of interest:
- A 3D Model File Loader-and-Examiner - for OBJ, PLY, OFF, STL, FEA files
- A 3D Terrain Generator-and-Examiner - using Image Files
- A 3D Generator-and-Examiner for Parametric Surfaces of 2 Variables

