summaryrefslogtreecommitdiff
path: root/bin/obj2hackvr.pl
blob: 7a60cd030fd53c97b4b38ea263f7d3ef2df58ec9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl

use strict;

die "usage: obj2hackvr.pl name file\n" unless $ARGV[0];

my $i;
my $j;
my $com;
my @vertices;
my @faces;
my @linepart;
my @parts;
my @points;
my @tmp;
my $tmp;
open(FILE,$ARGV[1]) if $ARGV[1];
while(<FILE>) {
 $_ =~ s/[\n\r]//g;
 @linepart=split(/ /,$_,2);
 $com=$linepart[0];
 if($com eq "v") {
  push(@vertices,$linepart[1]);
 }
 if($com eq "f") {
  push(@faces,$linepart[1]);
 }
}
#foreach $tmp (@vertices) {
# foreach(@$tmp) {
# print $tmp . "\n";
# }
#}

#foreach $tmp (@faces) {
# @points=split(/ /,$tmp);
# @points = map { $_ =~ s/\/.+$//g; $_; } @points;
## print @points;
# print $ARGV[0];
# print " addshape ";
# print @points+0;
# for($i=0;$i<(@points+0);$i++) {
#  print " ";
#  print $vertices[$points[$i]];
# }
# print "\n";
#}

#convert to triangles
foreach $tmp (@faces) {
 @points=split(/ /,$tmp);
## this map is to split off the vertex texture and vertex normal parts of the face.
 @points = map { $_ =~ s/\/.+$//g; $_; } @points;
# print @points;
 for($i=2;$i<(@points);$i++) {
  print $ARGV[0];
  print " addshape 2 3 ";
  print $vertices[$points[0]-1];
  print " ";
  print $vertices[$points[$i-1]-1];
  print " ";
  print $vertices[$points[$i]-1];
  print "\n";
 }
}