OpenCores
URL https://opencores.org/ocsvn/wf3d/wf3d/trunk

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [tool/] [objcnv.pl] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
#!/usr/bin/perl
2
 
3
#=======================================================================
4
# Project Monophony
5
#   Wire-Frame 3D Graphics Accelerator IP Core
6
#
7
# File:
8
#   objcnv.pl
9
#
10
# Abstract:
11
#    Wavefront OBJ converter
12
#    input isOBJ file, output C include header file
13
#
14
# Author:
15 9 specular
#   Kenji Ishimaru (info.info.wf3d@gmail.com)
16 2 specular
#
17
#======================================================================
18
#
19
# Copyright (c) 2015, Kenji Ishimaru
20
# All rights reserved.
21
#
22
# Redistribution and use in source and binary forms, with or without
23
# modification, are permitted provided that the following conditions are met:
24
#
25
#  -Redistributions of source code must retain the above copyright notice,
26
#   this list of conditions and the following disclaimer.
27
#  -Redistributions in binary form must reproduce the above copyright notice,
28
#   this list of conditions and the following disclaimer in the documentation
29
#   and/or other materials provided with the distribution.
30
#
31
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
33
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
35
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
#
43
# Revision History
44
 
45
use strict;
46
#$, = '_';       #separater
47
$, = ' ';       #separater
48
$\ = "\n";
49
 
50
if (@ARGV < 1) {
51
  die "usage : objcnv.pl [input file.obj] [output file.h]\n";
52
}
53
 
54
# global registers
55
 
56
my $vcnt = 0;
57
my $fcnt = 0;
58
 
59
my @vertices_x;
60
my @vertices_y;
61
my @vertices_z;
62
my @faces_0;
63
my @faces_1;
64
my @faces_2;
65
 
66
# command analize
67
my $in_file;
68
my $out_file;
69
my $cc = 0;
70
while (defined(my $carg = shift @ARGV)) {
71
    if ($cc == 0) {
72
      $in_file = $carg;
73
    }
74
    if ($cc == 1) {
75
      $out_file = $carg;
76
    }
77
    $cc++;
78
}
79
 
80
print "OBJ file :" . $in_file;
81
print "output file :" . $out_file;
82
open(INFILE, $in_file);
83
 
84
# v: vertex
85
# f: face
86
# report number of vertices
87
# report number of faces
88
 
89
while (<INFILE>) {
90
  chop;
91
  my $in_str = $_;
92
  if ($in_str =~ m/^vn/) {
93
    die("vn:vertex normals not supported.");
94
  }
95
  if ($in_str =~ m/^vt/) {
96
    die("vt:texture vertices not supported.");
97
  }
98
  if ($in_str =~ m/^v/) {
99
    my @item = split(/ /);     # split
100
    @vertices_x = (@vertices_x, $item[1]);
101
    @vertices_y = (@vertices_y, $item[2]);
102
    @vertices_z = (@vertices_z, $item[3]);
103
    $vcnt++;
104
    print "processing vertex : " . $vcnt;
105
  }
106
 
107
}
108
print "Number of vertices : " . $vcnt;
109
seek(INFILE,0,0); # rewind
110
while (<INFILE>) {
111
  chop;
112
  my $in_str = $_;
113
  if ($in_str =~ m/^f/) {
114
    my @item = split(/ /);     # split
115
    @faces_0 = (@faces_0, $item[1]);
116
    @faces_1 = (@faces_1, $item[2]);
117
    @faces_2 = (@faces_2, $item[3]);
118
    &check_orientation($faces_0[$fcnt],
119
                       $faces_1[$fcnt],
120
                       $faces_2[$fcnt]
121
                       );
122
    $fcnt++;
123
    print "processing face : " . $fcnt;
124
 
125
  }
126
}
127
print "Number of faces: " . $fcnt;
128
 
129
close(INFILE);
130
 
131
print "Writing C header file: " . $out_file;
132
open(OUTFILE, ">$out_file");
133
 
134
print OUTFILE "//Number of faces: " . $fcnt;
135
print OUTFILE "int num_vtx = " . @faces_0 . ";";
136
print OUTFILE "float vtx_array[] = {";
137
 
138
for (my $i = 0; $i < @faces_0; $i++) {
139
  print OUTFILE "// face " . $i;
140
  # v0 face index starts from 1, not 0
141
  print OUTFILE "//   v0 ";
142
  print OUTFILE $vertices_x[$faces_0[$i]-1] . ",";
143
  print OUTFILE $vertices_y[$faces_0[$i]-1] . ",";
144
  print OUTFILE $vertices_z[$faces_0[$i]-1] . ",";
145
  # v1
146
  print OUTFILE "//   v1 ";
147
  print OUTFILE $vertices_x[$faces_1[$i]-1] . ",";
148
  print OUTFILE $vertices_y[$faces_1[$i]-1] . ",";
149
  print OUTFILE $vertices_z[$faces_1[$i]-1] . ",";
150
  # v2
151
  print OUTFILE "//   v2 ";
152
  print OUTFILE $vertices_x[$faces_2[$i]-1] . ",";
153
  print OUTFILE $vertices_y[$faces_2[$i]-1] . ",";
154
  if ($i == @faces_0 - 1) {
155
    print OUTFILE $vertices_z[$faces_2[$i]-1];
156
  } else {
157
    print OUTFILE $vertices_z[$faces_2[$i]-1] . ",";
158
  }
159
 
160
}
161
print OUTFILE "};";
162
close(OUTFILE);
163
 
164
print "done.";
165
 
166
sub check_orientation {
167
   my $vx = $_[0];
168
   my $vy = $_[1];
169
   my $vz = $_[2];
170
   # not implemented
171
   return 1;
172
}
173
 
174
 
175
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.