将 .obj 转换为 .h 时无法使用“已定义(@array)”警告

2024-03-07

我正在尝试将我的 .obj 文件转换为 .h 头文件,但我收到“Can't use 'define(@array)'(也许您应该忽略 Defined()?)”警告,并且没有.h 文件已创建。

我尝试过更换@center to $center或省略defined()但它会创建 .exe 文件!
我在某处读到这可能是 perl 版本问题,我的是 5.22,我找不到更高的版本来尝试。

Update1:
我已将“obj2opengl.pl”和“myobject.obj”放在同一文件夹中。并尝试在控制台(win10)中使用此代码进行转换:c:\obj2openglfolder>obj2opengl.pl myobject.obj

Update2:
这是导致问题的第 154 行代码:

if(defined(@center)) {
    $xcen = $center[0];
    $ycen = $center[1];
    $zcen = $center[2];
}

Update3:
这是整个代码:

# -----------------------------------------------------------------

# Main Program

# -----------------------------------------------------------------

handleArguments();
# derive center coords and scale factor if neither provided nor disabled

unless(defined($scalefac) && defined($xcen)) {
calcSizeAndCenter();

}


if($verbose) {
printInputAndOptions();

}


# TODO check integrity: Does every referenced vertex, normal and coord 
exist?

loadData();

normalizeNormals();


if($verbose) {
printStatistics();

}


writeOutput();


# -----------------------------------------------------------------

# Sub Routines

# -----------------------------------------------------------------


sub handleArguments() {
my $help = 0;
my $man = 0;
my $noscale = 0;
my $nomove = 0;
$verbose = 1;
$errorInOptions = !GetOptions (
    "help" => \$help,
    "man"  => \$man,
    "noScale" => \$noscale,
    "scale=f" => \$scalefac,
    "noMove" => \$nomove,
    "center=f{3}" => \@center,
    "outputFilename=s" => \$outFilename,
    "nameOfObject=s" => \$object,
    "verbose!" => \$verbose,
    );

if($noscale) {
    $scalefac = 1;
}

if($nomove) {
    @center = (0, 0, 0);
}

if(@center) {
    $xcen = $center[0];
    $ycen = $center[1];
    $zcen = $center[2];
}

if($#ARGV == 0) {
    my ($file, $dir, $ext) = fileparse($ARGV[0], qr/\.[^.]*/);
    $inFilename = $dir . $file . $ext;
} else {
    $errorInOptions = true;
}

# (optional) derive output filename from input filename
unless($errorInOptions || defined($outFilename)) {
    my ($file, $dir, $ext) = fileparse($inFilename, qr/\.[^.]*/);
    $outFilename = $dir . $file . ".h";
}

# (optional) define object name from output filename
unless($errorInOptions || defined($object)) {
    my ($file, $dir, $ext) = fileparse($outFilename, qr/\.[^.]*/);
    $object = $file;
}

($inFilename ne $outFilename) or
    die ("Input filename must not be the same as output filename")
    unless($errorInOptions);

if($errorInOptions || $man || $help) {
    pod2usage(-verbose => 2) if $man;
    pod2usage(-verbose => 1) if $help;
    pod2usage(); 
}

# check wheter file exists
open ( INFILE, "<$inFilename" ) 
  || die "Can't find file '$inFilename' ...exiting \n";
close(INFILE);

}


# Stores center of object in $xcen, $ycen, $zcen

# and calculates scaling factor $scalefac to limit max

#   side of object to 1.0 units

sub calcSizeAndCenter() {
open ( INFILE, "<$inFilename" ) 
  || die "Can't find file $inFilename...exiting \n";

$numVerts = 0;

my (
    $xsum, $ysum, $zsum, 
    $xmin, $ymin, $zmin,
    $xmax, $ymax, $zmax,
    );

while ( $line = <INFILE> ) 
{
  chop $line;

  if ($line =~ /v\s+.*/)
  {

    $numVerts++;
    @tokens = split(' ', $line);

    $xsum += $tokens[1];
    $ysum += $tokens[2];
    $zsum += $tokens[3];

    if ( $numVerts == 1 )
    {
      $xmin = $tokens[1];
      $xmax = $tokens[1];
      $ymin = $tokens[2];
      $ymax = $tokens[2];
      $zmin = $tokens[3];
      $zmax = $tokens[3];
    }
    else
    {   
        if ($tokens[1] < $xmin)
      {
        $xmin = $tokens[1];
      }
      elsif ($tokens[1] > $xmax)
      {
        $xmax = $tokens[1];
      }

      if ($tokens[2] < $ymin) 
      {
        $ymin = $tokens[2];
      }
      elsif ($tokens[2] > $ymax) 
      {
        $ymax = $tokens[2];
      }

      if ($tokens[3] < $zmin) 
      {
        $zmin = $tokens[3];
      }
      elsif ($tokens[3] > $zmax) 
      {
        $zmax = $tokens[3];
      }

    }

  }

}
close INFILE;

#  Calculate the center
unless(defined($xcen)) {
    $xcen = $xsum / $numVerts;
    $ycen = $ysum / $numVerts;
    $zcen = $zsum / $numVerts;
}

#  Calculate the scale factor
unless(defined($scalefac)) {
    my $xdiff = ($xmax - $xmin);
    my $ydiff = ($ymax - $ymin);
    my $zdiff = ($zmax - $zmin);

    if ( ( $xdiff >= $ydiff ) && ( $xdiff >= $zdiff ) ) 
    {
      $scalefac = $xdiff;
    }
    elsif ( ( $ydiff >= $xdiff ) && ( $ydiff >= $zdiff ) ) 
    {
      $scalefac = $ydiff;
    }
    else 
    {
      $scalefac = $zdiff;
    }
    $scalefac = 1.0 / $scalefac;
}

}


sub printInputAndOptions() {
print "Input file     : $inFilename\n";
print "Output file    : $outFilename\n";
print "Object name    : $object\n";
print "Center         : <$xcen, $ycen, $zcen>\n";
print "Scale by       : $scalefac\n";

}


sub printStatistics() {
print "----------------\n";
print "Vertices       : $numVerts\n";
print "Faces          : $numFaces\n";
print "Texture Coords : $numTexture\n";
print "Normals        : $numNormals\n";

}


# reads vertices into $xcoords[], $ycoords[], $zcoords[]

#   where coordinates are moved and scaled according to

#   $xcen, $ycen, $zcen and $scalefac

# reads texture coords into $tx[], $ty[] 

#   where y coordinate is mirrowed

# reads normals into $nx[], $ny[], $nz[]

#   but does not normalize, see normalizeNormals()

# reads faces and establishes lookup data where

#   va_idx[], vb_idx[], vc_idx[] for vertices

#   ta_idx[], tb_idx[], tc_idx[] for texture coords

#   na_idx[], nb_idx[], nc_idx[] for normals

#   store indizes for the former arrays respectively

#   also, $face_line[] store actual face string

sub loadData {
$numVerts = 0;
$numFaces = 0;
$numTexture = 0;
$numNormals = 0;

open ( INFILE, "<$inFilename" )
  || die "Can't find file $inFilename...exiting \n";

while ($line = <INFILE>) 
{
  chop $line;

  # vertices
  if ($line =~ /v\s+.*/)
  {
    @tokens= split(' ', $line);
    $x = ( $tokens[1] - $xcen ) * $scalefac;
    $y = ( $tokens[2] - $ycen ) * $scalefac;
    $z = ( $tokens[3] - $zcen ) * $scalefac;    
    $xcoords[$numVerts] = $x; 
    $ycoords[$numVerts] = $y;
    $zcoords[$numVerts] = $z;

    $numVerts++;
  }

  # texture coords
  if ($line =~ /vt\s+.*/)
  {
    @tokens= split(' ', $line);
    $x = $tokens[1];
    $y = 1 - $tokens[2];
    $tx[$numTexture] = $x;
    $ty[$numTexture] = $y;

    $numTexture++;
  }

  #normals
  if ($line =~ /vn\s+.*/)
  {
    @tokens= split(' ', $line);
    $x = $tokens[1];
    $y = $tokens[2];
    $z = $tokens[3];
    $nx[$numNormals] = $x; 
    $ny[$numNormals] = $y;
    $nz[$numNormals] = $z;

    $numNormals++;
  }

  # faces
  if ($line =~ /f\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)(\s+([^ ]+))?/) 
  {
    @a = split('/', $1);
    @b = split('/', $2);
    @c = split('/', $3);
    $va_idx[$numFaces] = $a[0]-1;
    $ta_idx[$numFaces] = $a[1]-1;
    $na_idx[$numFaces] = $a[2]-1;

    $vb_idx[$numFaces] = $b[0]-1;
    $tb_idx[$numFaces] = $b[1]-1;
    $nb_idx[$numFaces] = $b[2]-1;

    $vc_idx[$numFaces] = $c[0]-1;
    $tc_idx[$numFaces] = $c[1]-1;
    $nc_idx[$numFaces] = $c[2]-1;

    $face_line[$numFaces] = $line;

    $numFaces++;

    # ractangle => second triangle
    if($5 != "")
    {
        @d = split('/', $5);
        $va_idx[$numFaces] = $a[0]-1;
        $ta_idx[$numFaces] = $a[1]-1;
        $na_idx[$numFaces] = $a[2]-1;

        $vb_idx[$numFaces] = $d[0]-1;
        $tb_idx[$numFaces] = $d[1]-1;
        $nb_idx[$numFaces] = $d[2]-1;

        $vc_idx[$numFaces] = $c[0]-1;
        $tc_idx[$numFaces] = $c[1]-1;
        $nc_idx[$numFaces] = $c[2]-1;

        $face_line[$numFaces] = $line;

        $numFaces++;
    }

  }  
}

close INFILE;

}


sub normalizeNormals {
for ( $j = 0; $j < $numNormals; ++$j) 
{
 $d = sqrt ( $nx[$j]*$nx[$j] + $ny[$j]*$ny[$j] + $nz[$j]*$nz[$j] );

  if ( $d == 0 )
  {
    $nx[$j] = 1;
    $ny[$j] = 0;
    $nz[$j] = 0;
  }
  else
  {
    $nx[$j] = $nx[$j] / $d;
    $ny[$j] = $ny[$j] / $d;
    $nz[$j] = $nz[$j] / $d;
  }

}

}


sub fixedIndex {
local $idx = $_[0];
local $num = $_[1];
if($idx >= 0)
{
    $idx;
} else {
    $num + $idx + 1;
}

}


sub writeOutput {
open ( OUTFILE, ">$outFilename" ) 
  || die "Can't create file $outFilename ... exiting\n";

print OUTFILE "/*\n";
print OUTFILE "created with obj2opengl.pl\n\n";

# some statistics
print OUTFILE "source file    : $inFilename\n";
print OUTFILE "vertices       : $numVerts\n";
print OUTFILE "faces          : $numFaces\n";
print OUTFILE "normals        : $numNormals\n";
print OUTFILE "texture coords : $numTexture\n";
print OUTFILE "\n\n";

# example usage
print OUTFILE "// include generated arrays\n";
print OUTFILE "#import \"".$outFilename."\"\n";
print OUTFILE "\n";
print OUTFILE "// set input data to arrays\n";
print OUTFILE "glVertexPointer(3, GL_FLOAT, 0, ".$object."Verts);\n";
print OUTFILE "glNormalPointer(GL_FLOAT, 0, ".$object."Normals);\n"
    if $numNormals > 0;
print OUTFILE "glTexCoordPointer(2, GL_FLOAT, 0, ".$object."TexCoords);\n"
    if $numTexture > 0;
print OUTFILE "\n";
print OUTFILE "// draw data\n";
print OUTFILE "glDrawArrays(GL_TRIANGLES, 0, ".$object."NumVerts);\n";
print OUTFILE "*/\n\n";

# needed constant for glDrawArrays
print OUTFILE "unsigned int ".$object."NumVerts = ".($numFaces * 3).";\n\n";

# write verts
print OUTFILE "float ".$object."Verts \[\] = {\n"; 
for( $j = 0; $j < $numFaces; $j++)
{
    $ia = fixedIndex($va_idx[$j], $numVerts);
    $ib = fixedIndex($vb_idx[$j], $numVerts);
    $ic = fixedIndex($vc_idx[$j], $numVerts);
    print OUTFILE "  // $face_line[$j]\n";
    print OUTFILE "  $xcoords[$ia], $ycoords[$ia], $zcoords[$ia],\n";
    print OUTFILE "  $xcoords[$ib], $ycoords[$ib], $zcoords[$ib],\n";
    print OUTFILE "  $xcoords[$ic], $ycoords[$ic], $zcoords[$ic],\n";
}
print OUTFILE "};\n\n";

# write normals
if($numNormals > 0) {
    print OUTFILE "float ".$object."Normals \[\] = {\n"; 
    for( $j = 0; $j < $numFaces; $j++) {
        $ia = fixedIndex($na_idx[$j], $numNormals);
        $ib = fixedIndex($nb_idx[$j], $numNormals);
        $ic = fixedIndex($nc_idx[$j], $numNormals);
        print OUTFILE "  // $face_line[$j]\n";
        print OUTFILE "  $nx[$ia], $ny[$ia], $nz[$ia],\n";
        print OUTFILE "  $nx[$ib], $ny[$ib], $nz[$ib],\n";
        print OUTFILE "  $nx[$ic], $ny[$ic], $nz[$ic],\n";
    }

    print OUTFILE "};\n\n";
}

# write texture coords
if($numTexture) {
    print OUTFILE "float ".$object."TexCoords \[\] = {\n"; 
    for( $j = 0; $j < $numFaces; $j++) {
        $ia = fixedIndex($ta_idx[$j], $numTexture);
        $ib = fixedIndex($tb_idx[$j], $numTexture);
        $ic = fixedIndex($tc_idx[$j], $numTexture);
        print OUTFILE "  // $face_line[$j]\n";
        print OUTFILE "  $tx[$ia], $ty[$ia],\n";
        print OUTFILE "  $tx[$ib], $ty[$ib],\n";
        print OUTFILE "  $tx[$ic], $ty[$ic],\n";
    }

    print OUTFILE "};\n\n";
}

close OUTFILE;

}

defined(@array)用于返回数组是否为空。这是一个错误。

defined(@array)应始终返回 true (因为数组中的元素数量始终已定义)。

这种特定的(无用的)使用不是修复错误(导致代码无声地发生故障)defined现在让用户知道他们做错了什么以及如何修复它。

正如错误消息所示,只需使用if (@array)检查数组是否为空。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 .obj 转换为 .h 时无法使用“已定义(@array)”警告 的相关文章

随机推荐

  • 推荐的 VBA IDE [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有推荐的 IDE 用于开发 Excel VBA 宏 提供合理的错误报告和代码完成 目前 我使用的是
  • 插入到android sqlite数据库中的特定行

    我正在寻找一种方法将新条目插入到 android 中的 sqlite 数据库中的特定行中 基本思想是数据库存储如下条目 id day time 1 Monday 09 00 2 Monday 11 00 3 Tuesday 10 00 ID
  • Amazon CloudSearch 从 DynamoDB 创建 Null ID

    我正在尝试开始使用 Amazon CloudSearch 我的数据位于我想要搜索的 DynamoDB 表中 我能够设置云搜索域 它从表中提取字段并让我设置它们 等等 但是 我上传数据时遇到了一些问题 我告诉它要从中提取 DynamoDB 表
  • 结果值在 '? :' 表达式的类型 '()' 和 'Bool' 不匹配[重复]

    这个问题在这里已经有答案了 我有一个双精度数组和一个按钮 按下该按钮会清空数组 我希望仅当数组的计数大于零时才启用该按钮 代码如下 var numbers Double At some point I add some numbers he
  • KnockoutJS 的购物车逻辑(?)问题

    The Goal 制作动态产品列表 场景 我有一个包含产品的购物应用程序 当我点击add button的产品 我想在侧边栏中显示我添加的产品 问题总结 您只需阅读此内容 我的代码中有以下代码ProductsSummary Index csh
  • 开始后跟冒号和变量是什么意思?

    什么是data mux意思是这里 它只是块的名称吗 if PORT CONFIG 32 P0 1 b1 begin data mux end 这些是块名称 它们特别适用于generate块 例如 您可以定义一个generate块如 genv
  • user.status 始终返回“离线”

    我知道user status应该用于返回用户状态 但它总是返回离线状态 无论我或其他参与者更改状态 代码 app commands command name user description f async def user self in
  • std::function 的开销

    我见过很多人们建议不要使用的例子std function lt gt 因为它是一个重量级的机制 有人可以解释一下为什么会这样吗 std function是一个类型擦除类 它采用任何它的构造材料 并删除除以下内容之外的所有内容 使用有问题的签
  • Prettytensor:尝试使用未初始化的值

    我正在关注这些教程 教程4中介绍了prettytensor 按照教程 我编写了这段代码来运行一个小型神经网络 import tensorflow as tf Use PrettyTensor to simplify Neural Netwo
  • 使用控制台应用程序调用非静态类

    我正在尝试使用控制台应用程序调用另一个类的方法 我尝试调用的类不是静态的 class Program static void Main string args Program p new Program var myString p Non
  • 如何使用具有多个同名值的 RouteValues

    在我的 ASP NET MVC 4 应用程序中 我可以过滤多个标签 在 HTML 中 它看起来像这样
  • 没有名为“后端”的模块

    我正在做这个简单的 django 教程http www madewithtea com simple todo api with django and oauth2 html http www madewithtea com simple
  • 标签地址 (MSVC)

    我们正在为高级编译语言编写字节码 经过一些分析和优化后 很明显 当前最大的性能开销是我们用来跳转到字节码情况的 switch 语句 我们研究了提取每个 case 标签的地址并将其存储在字节码流本身中 而不是我们通常打开的指令 ID 如果这样
  • 在 eclipse 中运行 jms 示例时,资源注入不起作用。

    我想在 eclipse 中运行 jms 教程文件 我已正确配置 glassfish 并添加了 jms 资源 如果我在 netbeans 中运行它 它工作正常 我在 eclipse 中创建了一个 应用程序客户端项目 这是资源注入的地方 Res
  • AFNetworking (AFHttpClient) 离线模式不适用于 NSURLRequestReturnCacheDataDontLoad 策略

    我在我的应用程序中使用 AFNetworking 并尝试通过使用缓存数据 如果可用 使其在离线模式下工作 我预计在将请求缓存策略设置为 NSURLRequestReturnCacheDataDontLoad 后 getPath parame
  • Java线程生产者消费者算法无法正常工作

    我正在尝试学习线程 因此我编写了一个示例生产者消费者问题 其中生产者生成从 1 到 10 的数字 而消费者必须显示它们 但只有消费者显示数字 1 并停止 正如我所说 该程序写得不好 可能很荒谬 但我仍然想弄清楚为什么从 1 到 10 的所有
  • 有条件地调用 React hook

    从react官方文档我们知道 React 依赖于 Hook 的调用顺序 https reactjs org docs hooks rules html explanation 那么 如果我想有条件地调用它 那么为钩子 保留 一个位置有什么问
  • jQuery 对话框第二次未打开

    I found 这个线程 https stackoverflow com questions 366696 jquery dialog box这基本上和我有同样的问题 但他们的解决方案对我不起作用 我第一次单击提交按钮时会出现该对话框 但第
  • 如何让子菜单浮动在div上?

    大家好 我有一个带有下拉菜单的子菜单 但是当菜单下降时 它会将下面的 div 向下推 知道如何修复它以便它加载到 div 上吗 还有一个是水平的 一个是垂直的 如何解决这个问题 谢谢 JSFiddle http jsfiddle net p
  • 将 .obj 转换为 .h 时无法使用“已定义(@array)”警告

    我正在尝试将我的 obj 文件转换为 h 头文件 但我收到 Can t use define array 也许您应该忽略 Defined 警告 并且没有 h 文件已创建 我尝试过更换 center to center或省略defined 但