#!/usr/bin/perl
|
#!/usr/bin/perl
|
|
|
#=======================================================================
|
#=======================================================================
|
# Project Monophony
|
# Project Monophony
|
# Wire-Frame 3D Graphics Accelerator IP Core
|
# Wire-Frame 3D Graphics Accelerator IP Core
|
#
|
#
|
# File:
|
# File:
|
# objcnv.pl
|
# objcnv.pl
|
#
|
#
|
# Abstract:
|
# Abstract:
|
# Wavefront OBJ converter
|
# Wavefront OBJ converter
|
# input isOBJ file, output C include header file
|
# input isOBJ file, output C include header file
|
#
|
#
|
# Author:
|
# Author:
|
# Kenji Ishimaru (kenji.ishimaru@prtissimo.com)
|
# Kenji Ishimaru (kenji.ishimaru@prtissimo.com)
|
#
|
#
|
#======================================================================
|
#======================================================================
|
#
|
#
|
# Copyright (c) 2015, Kenji Ishimaru
|
# Copyright (c) 2015, Kenji Ishimaru
|
# All rights reserved.
|
# All rights reserved.
|
#
|
#
|
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
# modification, are permitted provided that the following conditions are met:
|
# modification, are permitted provided that the following conditions are met:
|
#
|
#
|
# -Redistributions of source code must retain the above copyright notice,
|
# -Redistributions of source code must retain the above copyright notice,
|
# this list of conditions and the following disclaimer.
|
# this list of conditions and the following disclaimer.
|
# -Redistributions in binary form must reproduce the above copyright notice,
|
# -Redistributions in binary form must reproduce the above copyright notice,
|
# this list of conditions and the following disclaimer in the documentation
|
# this list of conditions and the following disclaimer in the documentation
|
# and/or other materials provided with the distribution.
|
# and/or other materials provided with the distribution.
|
#
|
#
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
#
|
#
|
# Revision History
|
# Revision History
|
|
|
use strict;
|
use strict;
|
#$, = '_'; #separater
|
#$, = '_'; #separater
|
$, = ' '; #separater
|
$, = ' '; #separater
|
$\ = "\n";
|
$\ = "\n";
|
|
|
if (@ARGV < 1) {
|
if (@ARGV < 1) {
|
die "usage : objcnv.pl [input file.obj] [output file.h]\n";
|
die "usage : objcnv.pl [input file.obj] [output file.h]\n";
|
}
|
}
|
|
|
# global registers
|
# global registers
|
|
|
my $vcnt = 0;
|
my $vcnt = 0;
|
my $fcnt = 0;
|
my $fcnt = 0;
|
|
|
my @vertices_x;
|
my @vertices_x;
|
my @vertices_y;
|
my @vertices_y;
|
my @vertices_z;
|
my @vertices_z;
|
my @faces_0;
|
my @faces_0;
|
my @faces_1;
|
my @faces_1;
|
my @faces_2;
|
my @faces_2;
|
|
|
# command analize
|
# command analize
|
my $in_file;
|
my $in_file;
|
my $out_file;
|
my $out_file;
|
my $cc = 0;
|
my $cc = 0;
|
while (defined(my $carg = shift @ARGV)) {
|
while (defined(my $carg = shift @ARGV)) {
|
if ($cc == 0) {
|
if ($cc == 0) {
|
$in_file = $carg;
|
$in_file = $carg;
|
}
|
}
|
if ($cc == 1) {
|
if ($cc == 1) {
|
$out_file = $carg;
|
$out_file = $carg;
|
}
|
}
|
$cc++;
|
$cc++;
|
}
|
}
|
|
|
print "OBJ file :" . $in_file;
|
print "OBJ file :" . $in_file;
|
print "output file :" . $out_file;
|
print "output file :" . $out_file;
|
open(INFILE, $in_file);
|
open(INFILE, $in_file);
|
|
|
# v: vertex
|
# v: vertex
|
# f: face
|
# f: face
|
# report number of vertices
|
# report number of vertices
|
# report number of faces
|
# report number of faces
|
|
|
while (<INFILE>) {
|
while (<INFILE>) {
|
chop;
|
chop;
|
my $in_str = $_;
|
my $in_str = $_;
|
if ($in_str =~ m/^vn/) {
|
if ($in_str =~ m/^vn/) {
|
die("vn:vertex normals not supported.");
|
die("vn:vertex normals not supported.");
|
}
|
}
|
if ($in_str =~ m/^vt/) {
|
if ($in_str =~ m/^vt/) {
|
die("vt:texture vertices not supported.");
|
die("vt:texture vertices not supported.");
|
}
|
}
|
if ($in_str =~ m/^v/) {
|
if ($in_str =~ m/^v/) {
|
my @item = split(/ /); # split
|
my @item = split(/ /); # split
|
@vertices_x = (@vertices_x, $item[1]);
|
@vertices_x = (@vertices_x, $item[1]);
|
@vertices_y = (@vertices_y, $item[2]);
|
@vertices_y = (@vertices_y, $item[2]);
|
@vertices_z = (@vertices_z, $item[3]);
|
@vertices_z = (@vertices_z, $item[3]);
|
$vcnt++;
|
$vcnt++;
|
print "processing vertex : " . $vcnt;
|
print "processing vertex : " . $vcnt;
|
}
|
}
|
|
|
}
|
}
|
print "Number of vertices : " . $vcnt;
|
print "Number of vertices : " . $vcnt;
|
seek(INFILE,0,0); # rewind
|
seek(INFILE,0,0); # rewind
|
while (<INFILE>) {
|
while (<INFILE>) {
|
chop;
|
chop;
|
my $in_str = $_;
|
my $in_str = $_;
|
if ($in_str =~ m/^f/) {
|
if ($in_str =~ m/^f/) {
|
my @item = split(/ /); # split
|
my @item = split(/ /); # split
|
@faces_0 = (@faces_0, $item[1]);
|
@faces_0 = (@faces_0, $item[1]);
|
@faces_1 = (@faces_1, $item[2]);
|
@faces_1 = (@faces_1, $item[2]);
|
@faces_2 = (@faces_2, $item[3]);
|
@faces_2 = (@faces_2, $item[3]);
|
&check_orientation($faces_0[$fcnt],
|
&check_orientation($faces_0[$fcnt],
|
$faces_1[$fcnt],
|
$faces_1[$fcnt],
|
$faces_2[$fcnt]
|
$faces_2[$fcnt]
|
);
|
);
|
$fcnt++;
|
$fcnt++;
|
print "processing face : " . $fcnt;
|
print "processing face : " . $fcnt;
|
|
|
}
|
}
|
}
|
}
|
print "Number of faces: " . $fcnt;
|
print "Number of faces: " . $fcnt;
|
|
|
close(INFILE);
|
close(INFILE);
|
|
|
print "Writing C header file: " . $out_file;
|
print "Writing C header file: " . $out_file;
|
open(OUTFILE, ">$out_file");
|
open(OUTFILE, ">$out_file");
|
|
|
print OUTFILE "//Number of faces: " . $fcnt;
|
print OUTFILE "//Number of faces: " . $fcnt;
|
print OUTFILE "int num_vtx = " . @faces_0 . ";";
|
print OUTFILE "int num_vtx = " . @faces_0 . ";";
|
print OUTFILE "float vtx_array[] = {";
|
print OUTFILE "float vtx_array[] = {";
|
|
|
for (my $i = 0; $i < @faces_0; $i++) {
|
for (my $i = 0; $i < @faces_0; $i++) {
|
print OUTFILE "// face " . $i;
|
print OUTFILE "// face " . $i;
|
# v0 face index starts from 1, not 0
|
# v0 face index starts from 1, not 0
|
print OUTFILE "// v0 ";
|
print OUTFILE "// v0 ";
|
print OUTFILE $vertices_x[$faces_0[$i]-1] . ",";
|
print OUTFILE $vertices_x[$faces_0[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_0[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_0[$i]-1] . ",";
|
print OUTFILE $vertices_z[$faces_0[$i]-1] . ",";
|
print OUTFILE $vertices_z[$faces_0[$i]-1] . ",";
|
# v1
|
# v1
|
print OUTFILE "// v1 ";
|
print OUTFILE "// v1 ";
|
print OUTFILE $vertices_x[$faces_1[$i]-1] . ",";
|
print OUTFILE $vertices_x[$faces_1[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_1[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_1[$i]-1] . ",";
|
print OUTFILE $vertices_z[$faces_1[$i]-1] . ",";
|
print OUTFILE $vertices_z[$faces_1[$i]-1] . ",";
|
# v2
|
# v2
|
print OUTFILE "// v2 ";
|
print OUTFILE "// v2 ";
|
print OUTFILE $vertices_x[$faces_2[$i]-1] . ",";
|
print OUTFILE $vertices_x[$faces_2[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_2[$i]-1] . ",";
|
print OUTFILE $vertices_y[$faces_2[$i]-1] . ",";
|
if ($i == @faces_0 - 1) {
|
if ($i == @faces_0 - 1) {
|
print OUTFILE $vertices_z[$faces_2[$i]-1];
|
print OUTFILE $vertices_z[$faces_2[$i]-1];
|
} else {
|
} else {
|
print OUTFILE $vertices_z[$faces_2[$i]-1] . ",";
|
print OUTFILE $vertices_z[$faces_2[$i]-1] . ",";
|
}
|
}
|
|
|
}
|
}
|
print OUTFILE "};";
|
print OUTFILE "};";
|
close(OUTFILE);
|
close(OUTFILE);
|
|
|
print "done.";
|
print "done.";
|
|
|
sub check_orientation {
|
sub check_orientation {
|
my $vx = $_[0];
|
my $vx = $_[0];
|
my $vy = $_[1];
|
my $vy = $_[1];
|
my $vz = $_[2];
|
my $vz = $_[2];
|
# not implemented
|
# not implemented
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
|
|
|
|