flash animation video tutorials

 

 

 

START LEARNING
FLASH NOW

Get instant access to over
45 minutes of FREE Flash tutorials on video
 and our newsletter.

Name:
E-Mail:
Phone:
Describe

.

.

.

 
Web video-animation.com

.

.

flash tutorials flash tutorials flash tutorials flash tutorials

Flash 3D .obj files model importer

Beta This is a beta model importer for my 3D game engine. It may be useful to you. What it does is parse a .obj 3d Model file and converts it to code that will render that model in Flash.
So, what is a .obj file you may ask? .obj is a text file generated by some/most 3D programs such as Cinema4D etc. It describes the points or vertices of the faces and the order of the vertices in those faces.
Here is an example of some .obj model:

# WaveFront *.obj file

g cube5 cube1 wing1_11252290
usemtl Material__3
v -30.313866 15.788016 19.982368
v -26.139158 18.622164 15.802573
v -15.488976 18.131786 26.107304
v -19.663685 15.297638 30.2871
v -21.743971 21.338137 11.389283
v -11.093789 20.847759 21.694016

f 11 10 9
f 12 11 9
f 13 11 12
f 14 13 12
f 15 13 14
f 16 15 14
f 17 15 16
f 18 17 16

The lines starting with "v" detail the location of the point in 3D space. They are the x,y,z coordinates.
The first line would be coordinate (-30.313866, 15.788016, 19.982368).
The lines starting with "f" give the succession of point numbers that make up that face.
So the first face is made up of a triangle with vertices 11, 10 , 9. The points are numbered as they appear in the listing.

Flash Tutorials in Video Format - Watch them now at LearnFlash.com  

The PHP parser

Anyway, this is how i convert the .obj code to something usable for my flash 3D engine.
I have apache server and PHP installed on my machine and use that. If you have a php enabled website you can use that.
And here is the php script:

<?php
$fp = fopen("model.obj", "r" );

if(!$fp)
{
    echo "Couldn't open the data file. Try again later.";
    exit;
}
$counter = 0;
      while(!feof($fp))
     {
	    // get a line 
      $line = fgets($fp, 3000);
      // Split the input string into words as an array
      $arrWords = explode( ' ', $line );  
      // Count the words in the string
      $numWords = count( $arrWords ); 
      		// if first word is v - vertices
	      if($arrWords[0] == "v"){
		      // write flash code to add a vertex
		      echo "model.addPoint($arrWords[1] , 
		            $arrWords[2] , $arrWords[3] ); <br />";
	      }
	      // if first word is f - face 
	      if($arrWords[0] == "f"){
		       $counter++;
		      // write flash code to render face
		      echo "model.drawFace(\"face$counter\" ,  
		              [ $arrWords[1] , $arrWords[2], 
		              $arrWords[3] ] ,0xff0000 , $counter);<br />" ;
	      }
	           
     }
    fclose($fp);

?>

At the moment there are a number of flaws with this script so watch out.

Here is a fix from Musicman at flashkit's BackEnd forum.. thanks mate..
I havent tested it yet..

if($arrWords[0] == "f"){
   $counter++; 
   array_shift($arrWords);
   // write flash code to render face
   echo "model.drawFace(\"face$counter\" ,
   [" . implode(", ", $arrWords) . "] ,0xff0000 , $counter);\n" ;
}


And here is a further fix from al. he writes
" yop steve I'm not a php guru (i really prefer Action script dev) but I may have a solution for the parsing problem you sent me
in php the function wich return the position of a character in a string is "strpos" so, it gives:
$mystring = "f 1/1 2/2 3/3"
$findme = "/"
$pos = strpos($mystring, $findme); 
 //$pos is the int variable of position (4 inthis case)

//after, you use that number in a substring function ,in other words :

$rest = substr($mystring, 2, $pos); 
// i've put "2" because we don't need "f "

if I'm right $rest will give "1" (the 1 which is before the /)
after, you have to make a boucle(? sic) to parse all the string, this code is not tested (dont have php on that computer, maybe there is some syntax or algorythmik errors ) but I hope it will be helpful for you .
ps : "/" character may gives conflicts in php "
  • This script can only handle faces with 3 vertices.
  • At present I am working on the major flaw so that any number of vertices can be handled.
  • Some .obj file generators put something like " f 12/12 13/13 14/14 " in the face lines so that you have to strip out anything following the "/" character.
  • Also , the .obj file must be named "model.obj".

If you are a php guru please fix the above script and send it to me so that it is working much better.

Usage
Put this script in a folder. Just save it as something like "ObjConverter.php"
Put your "model.obj" file in the same folder.
Open your ObjConverter.php file in your browser and hey presto the parsed code should appear like so:

model.addPoint(-30.313866 , 15.788016 , 19.982368 ); 
model.addPoint(-26.139158 , 18.622164 , 15.802573 ); 
model.addPoint(-15.488976 , 18.131786 , 26.107304 ); 
model.addPoint(-19.663685 , 15.297638 , 30.2871 ); 
model.addPoint(-21.743971 , 21.338137 , 11.389283 ); 
model.addPoint(-11.093789 , 20.847759 , 21.694016 ); 
model.addPoint(69.457222 , -9.891102 , -63.020256 ); 
// etc ...
// etc ...
model.drawFace("face1" , [ 1 , 2, 3 ] ,0xff0000 , 1);
model.drawFace("face2" , [ 4 , 1, 3 ] ,0xff0000 , 2);
model.drawFace("face3" , [ 2 , 5, 6 ] ,0xff0000 , 3);
model.drawFace("face4" , [ 3 , 2, 6 ] ,0xff0000 , 4);
model.drawFace("face5" , [ 7 , 8, 9 ] ,0xff0000 , 5);
model.drawFace("face6" , [ 10 , 7, 9 ] ,0xff0000 , 6);
model.drawFace("face7" , [ 11 , 10, 9 ] ,0xff0000 , 7);
model.drawFace("face8" , [ 12 , 11, 9 ] ,0xff0000 , 8);
// etc ...
// etc ...

This script works with my engine. You will need to rejig the script to work with your flash code.
E.G. Your code may go something like this:

 car.addVertex(100,200,300);
 car.render(faceName, faceLIst,lineColor, fillColor, depth);
So you will have to adjust accordingly.
The above script needs major improvement and is very early days yet. If you can improve it , please do so and let everyone know. Thank you for the improvements added my musicMan and al.. When i get back from holidays, i will test the script and fix the above mess.. LMAO.. cheers , Steve

flash 8
.