From f9f6985a21ab2ac487456dc0707d37dba8da2ff6 Mon Sep 17 00:00:00 2001 From: dpethes Date: Sun, 29 Jan 2017 19:33:20 +0100 Subject: [PATCH] hob viewer: obj export (no textures) --- hob_display/hob_mesh.pas | 117 +++++++++++++++++++++++++++++++++++++ hob_display/hob_viewer.pas | 6 +- 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/hob_display/hob_mesh.pas b/hob_display/hob_mesh.pas index 7977ce0..9270627 100644 --- a/hob_display/hob_mesh.pas +++ b/hob_display/hob_mesh.pas @@ -58,6 +58,7 @@ type procedure Load(const hob_filename, hmt_filename: string); procedure InitGL; procedure DrawGL(opts: TRenderOpts); + procedure ExportObj(const obj_name: string); end; implementation @@ -335,5 +336,121 @@ begin DrawTri(_triangles[k][i]); end; + +const + HeaderComment = 'Exported with HOB viewer'; + DefaultMaterial = 'default'; + + +procedure TModel.ExportObj(const obj_name: string); +const + DesiredUnitSize = 2; +var + objfile: TextFile; + vt: TVertex; + face: TTriangle; + + x, y, z: double; + u, v: double; + + scaling_factor: double; + coord_max: double; + uv_counter: integer; + vertex3d_offset: integer; + last_material_index: integer; + + i,j,k: integer; + vertex_counter: Integer; + +function GetMaxCoord: double; +var + vt: TVertex; + i,j,k: integer; +begin + result := 0; + for i := 0 to _vertices.Count - 1 do begin + vt := _vertices[i]; + x := abs(vt.x); + y := abs(vt.y); + z := abs(vt.z); + coord_max := Max(z, Max(x, y)); + if coord_max > result then + result := coord_max; + end; +end; + +begin + AssignFile(objfile, obj_name); + Rewrite(objfile); + + writeln(objfile, '# ' + HeaderComment); + writeln(objfile, 'mtllib ', obj_name + '.mtl'); + + //scale pass + scaling_factor := 1; + //scaling_factor := DesiredUnitSize / GetMaxCoord; + + //vertex pass + for k := 0 to Length(_triangles) - 1 do + for i := 0 to _triangles[k].Count - 1 do begin + face := _triangles[k][i]; + for vt in face.vertices do begin + x := (vt.x) * scaling_factor; + y := (vt.y) * scaling_factor; + z := (vt.z) * scaling_factor; + writeln(objfile, 'v ', x:10:6, ' ', y:10:6, ' ', z:10:6); + end; + end; + + //uv pass + for k := 0 to Length(_triangles) - 1 do + for i := 0 to _triangles[k].Count - 1 do begin + face := _triangles[k][i]; + for j := 0 to 2 do begin + u := face.tex_coords[j, 0]; + v := face.tex_coords[j, 1]; + writeln(objfile, 'vt ', u:10:6, ' ', v:10:6); + end; + end; + + //face / material pass + uv_counter := 1; + vertex_counter := 1; + last_material_index := -1; + + for k := 0 to Length(_triangles) - 1 do begin + if _triangles[k].Count = 0 then + continue; + + writeln(objfile, 'g ', k); + + for i := 0 to _triangles[k].Count - 1 do begin + face := _triangles[k][i]; + + if face.material_index <> last_material_index then begin + if face.material_index = -1 then + writeln(objfile, 'usemtl ' + DefaultMaterial) + else + writeln(objfile, 'usemtl material_id', face.material_index); + last_material_index := face.material_index; + end; + + write(objfile, 'f '); + for vt in face.vertices do begin + write(objfile, vertex_counter); + write(objfile, '/', uv_counter); + write(objfile, ' '); + vertex_counter += 1; + uv_counter += 1; + end; + writeln(objfile); + end; + end; + + CloseFile(objfile); + + //SaveMaterials(pdo, obj_name); +end; + end. diff --git a/hob_display/hob_viewer.pas b/hob_display/hob_viewer.pas index fe6d17d..31a6e7e 100644 --- a/hob_display/hob_viewer.pas +++ b/hob_display/hob_viewer.pas @@ -327,7 +327,7 @@ end; //****************************************************************************** var sec, frames: integer; - hob_file, hmt_file: string; + hob_file, hmt_file, obj_file: string; begin if Paramcount < 1 then begin @@ -351,6 +351,10 @@ begin InitView; model.InitGL; + //export + obj_file := StringReplace(hob_file, '.hob', '.obj', [rfIgnoreCase]); + model.ExportObj(obj_file); + sec := SDL_GetTicks; frames := 0; Done := False;