"0 0 0"); //obj file causes index to be off by one $normal=array(0 => "0 0 0"); //this is a dummy vertex to line it back up $texcoord=array(0 => "0 0 0"); $quadcorner=array(); $tristrip_count=0; $tristrip=array(); $matref="default"; $material=array(); $poly_count=0; foreach($file as $i => $value){ if(substr($value, 0, 2) == "v "){ //Create an array of the vertices list($junk, $x, $y, $z) = split(" ", $value); $z = trim($z); //get rid of the line break if($flipverts){ $vertex[]="$x $z $y"; }else{ $vertex[]="$x $y $z"; } }elseif(substr($value, 0, 3) == "vn "){ //create vertex normals list($junk, $x, $z, $y) = split(" ", $value); $y = trim($y); if($flipverts){ $normal[]="$x $z $y"; }else{ $normal[]="$x $y $z"; } }elseif(substr($value, 0, 3) == "vt "){ //create texcoords list($junk, $x, $y) = split(" ", $value); $y = trim($y); $texcoord[]="$x $y"; //Corner Definitions, put into an array, and reference them later }elseif(substr($value, 0, 2) == "f "|| substr($value, 0, 2) == "t " || substr($value, 0, 2) =="q "){ //Find only face data $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw =="f" || $cornerraw == "t" || $cornerraw =="q"){ //ignore }elseif(in_array($cornerraw, $corner_def)){ //dup check, do nothing }else{ $corner_def[]=$cornerraw; }//cornerraw if }//cornerinit foreach }//finding face data }unset($value); //end foreach line parser //This next bit makes drawInfo happy by creating the same number of each element //the dummy vertices do not seem to affect the rendering efficiency for($i = count($vertex);$i< count($texcoord); $i++){ $vertex[] ="0 0 0"; } for($i = count($vertex);$i< count($normal); $i++){ $vertex[] ="0 0 0"; } for($i = count($normal);$i< count($texcoord); $i++){ $normal[] ="0 0 0"; } for($i = count($texcoord);$i< count($normal); $i++){ $texcoord[] ="0 0 0"; } //Create the Polygons foreach($file as $i=> $value){ if(substr($value, 0, 6) =="usemtl"){ $matref = trim(substr($value, 7)); } //Tris if(substr($value, 0, 2) == "f " && substr_count($value, " ") ==3){ //only tris faces $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw == "f"){ //ignore }else{ $material[$matref]["tris"][]=array_search($cornerraw, $corner_def); }//defined corners, they can be all in one big strip, so we're done with tris }//foreach cornerraws } //Quads if(substr($value, 0, 2) == "f " && substr_count($value, " ") ==4){ //only quads $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw == "f"){ //ignore }else{ $cornerraw=trim($cornerraw); $material[$matref]["quads"][]=array_search($cornerraw, $corner_def); }//end quadcorners }//foreach cornerraws } //end quadfaces if(substr($value, 0, 2) == "f " && substr_count($value, " ") > 4){ //only polys $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw == "f"){ //ignore $poly_count++; }else{ $cornerraw=trim($cornerraw); $material[$matref]["polygon"][$poly_count][]=array_search($cornerraw, $corner_def); }//end quadcorners }//foreach cornerraws } //end quadfaces //tristrips if(substr($value, 0, 2) == "t " || substr($value, 0, 2) == "q "){ //only tristrips if(substr($value, 0, 2) == "t "){ $tristrip_count++; $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw == "t"){ //ignore }else{ $cornerraw=trim($cornerraw); $tristrip[$tristrip_count][]=array_search($cornerraw, $corner_def); }//end cornerraw if }//end cornerinit foreach }elseif(substr($value, 0, 2) == "q "){ //continuation of tristrip $cornerinit = split(" ", trim($value)); foreach($cornerinit as $cornerraw){ if($cornerraw == "q"){ //ignore }else{ $cornerraw=trim($cornerraw); $tristrip[$tristrip_count][]=array_search($cornerraw, $corner_def); }//cornerraw if }unset($value);//cornerinit foreach }//continuation elseif } //trifans //quadstrips } echo "#######################################\n"; echo "# Created with PHP DrawInfo-Machine #\n"; echo "# (c)2007 Ted Smoker (Tedius) #\n"; echo "#######################################\n\n\n"; echo "material\nname default\naddtexture rabbit_tank\nend\n"; echo "define object\n"; echo "#### copy from this point on if you are inserting multiple lods\n"; echo "mesh\n"; echo "\tdrawInfo\n"; echo"\tdlist\n"; echo "\t#angvel 120\t#uncomment this line to cause the object to spin at 120 degrees/second\n"; //Print the vertex data foreach($vertex as $i => $value){ echo "\tvertex $value\t\t#$i\n"; }unset($value); foreach($normal as $i => $value){ echo "\tnormal $value\t\t#$i\n"; }unset($value); foreach($texcoord as $i => $value){ echo "\ttexcoord $value\t\t#$i\n"; }unset($value); //Print out the corners $n_count=1; $t_count=1; foreach($corner_def as $key => $value){ @list($v, $n, $t)=split("/",$value); if(!$v){ exit("missing vertex ($value)"); } if(!$n){ $n=$n_count; $n_count++; } if(!$t){ $t=$t_count; $t_count++; } echo "\tcorner $v $n $t\t\t#$key\n"; // $corner=str_replace("/"," ", $value); // echo "\tcorner $corner\t\t#$key\n"; }unset($value); //foreach corner echo "\t#lod #uncomment these lines for lods following the first one\n"; echo "\t\t#lengthPerPixel 0\t\n"; echo "\t#end\t#lod\n"; echo "\tlod\n"; echo "\t\tlengthPerPixel $min_loddistance\t#the distance from which the object appears (default 0)\n"; //echo "\t\tmatref default\t#material name"; //rsort($tricorner); //reverse them if needed foreach ($material as $matkey => $matvalue){ echo "\n\t\tmatref $matkey"; foreach($matvalue as $key => $poly){ if($key == "polygon"){ foreach($poly as $polygon){ echo "\n\t\t\t$key "; foreach ($polygon as $value){ echo "$value "; } }unset($value); }else{ echo "\n\t\t\t$key "; foreach($poly as $value){ echo "$value "; } } }unset($value); echo "\n\t\tend\t#mat\n"; } if ($tristrip){ foreach($tristrip as $tristrip_index){ echo "\n\t\t\ttristrip "; foreach($tristrip_index as $value){ echo "$value "; } }unset($value); } echo "\tend\t#lod\n"; echo "\tlod\n"; echo "\t\tlengthPerPixel $max_loddistance\t#the distance from which the object ceases to be seen (default 1)\n"; echo "\tend\t#lod\n"; echo "end\t#drawInfo\n"; echo "end\t#mesh\n"; echo "###### end copy for multiple lods here\n"; echo "enddef\t#object definition\n"; echo "group object\nscale 1 1 1\nshift 0 0 10\nend\n"; //Bonus track: the Randomizer, enable by changing $randomizer variable to 1 if($randomizer){ for ($i = 0; $i <= $objects; $i++){ $x = (RAND(-$worldsize*10,$worldsize*10)/10); $y = (RAND(-$worldsize*10,$worldsize*10)/10); $spin = RAND(1,1800)/10; echo "group object #$i\n\tshift $x $y 0\n\tspin $spin 0 0 1\nend\n"; }; }; //var_dump ($material); //////////////////////////////////////////////////////////////////////////// // This work is licensed under the Creative Commons Attribution 3.0 // // Unported License. To view a copy of this license, visit // // http://creativecommons.org/licenses/by/3.0/ or send a letter // // to Creative Commons, 171 Second Street, Suite 300, San Francisco, // // California, 94105, USA. // /////////////////////////////////////////////////////////////////////////// ?>