Thursday, August 25, 2011

Simple Ray Tracing Voxel Demo


Just a simple demo using Ray Tracing to render Voxels.
I use a 1D array - voxelData[] to represent voxels/3D points with colors. It's easier for understanding if I use a 3D array, voxelData[x][y][z], where x, y, z is the position of the voxel in 3D space and the value of the array is color for that voxel. Note that voxelData[x][y][z] = voxelData[x<<16|y<<8|z] if the dimension is set to 256 at max.

The rendering idea is simple, since we want to fill the screen using colors, for each pixel in the screen, trace a ray, and march the ray in the voxel space, until the ray hit a voxel, then we get the color and write it to the screen. This is the so-called "first-hit" algorithm.

Ray tracing is slow,  a fast way to render voxel is ray casting, such as the algorithm used in Bengine. But using ray tracing, you can easily add some reflection/shadow effect after calculate normal vectors of the voxels.

Source code: https://flaswf.googlecode.com/svn/trunk/RaytracingVoxel
(similar to the source code of the voxel ray tracing demo in my earlier post)

====================
 Update: November, 27, 2011
====================

Ray Tracing Voxel Demo using 3D DDA/Bresenham's line algorithm


Source code: https://flaswf.googlecode.com/svn/trunk/RaytracingVoxel/3DDDA
More about Bresenham's line algorithm:
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
http://www.cobrabytes.com/index.php?topic=1150.0
http://www.jonof.id.au/forum/index.php?topic=1338.msg9213;topicseen#msg9213

Friday, August 19, 2011

Use vLib.swc to manage flash project resources and protect swf assets

====================
Update: 2012/12/21
vLib v0.15 released!
https://flaswf.googlecode.com/svn/trunk/vLib/bin/v0.15
What's new:
Fixed several bugs, disable useless traces.
Add new sound API of Flash 11.

myLib.playSound("MP3SoundName");//Play the mp3 sound from mp3 file directly! 
====================

vLib - the virtual library project, is a AS3 swc lib to handle your flash project resources, such as bitmaps, swfs, mp3 sounds, texts, xmls and binary files.
It will ease you from importing assets into your .fla's library, writing lots of Embed stuffs for the Flex project, and save your efforts of using lots of URLLoader and Loader codes.
Only need to write a XML file and markup the name and URL address for all flash resources, you can then access them using simple AS3 api provided by vLib.swc.

Tutorial of using vLib.swc, Step by Step:

Step 1[Prepare the test resources files]:
Create a folder and put all the resources(such as Binary files, mp3 sounds, swfs, text files, xml files, .JPG and .PNG pictures) into it.

Step 2[Write the description xml]:
Write a xml file of the resources' names and URLs.
The XML must begin and end with

There're 6 kinds of resources, each kind has a tag, they're:
 for all text files
 for all picture files
 for all swf files
 for all mp3 files
 for all binary files
All resource files should be written into the corresponding tag of the format
  
The details of the description XML's format are showed in the example.
Example["vLib.xml" from vLib\examples\bin\DataFiles]:




  
  



  
  




  
  















  
  





Note: Due to formatting issues, the above xmls don't display correctly, all tag names should be in upper case in the first character, for details, please see the examples in the svn.


Step 3[Add the vLib.swc to library]:
Add the "vLib.swc" to your flash/flex project, so you can use it in your AS3 code.

Step 4[Initialize the virtual library]:

First declare an instance and provide the the description xml file "vLib.xml" to it.
import Lib.*;
var myLib:Lib = new Lib();
myLib.addEventListener(Event.COMPLETE, Libloaded);
myLib.init0("./DataFiles/Lib.xml");
function Libloaded(event:Event):void
{
//All Resources Loaded!
}

Step 5[Access resources from the virtual library]:
As showed in the code, once all resources are loaded, it will triger the callback function "Libloaded", that's the time you can use them.
function Libloaded(event:Event):void
{
//All Resources Loaded!
test();
}
function test():void
{
myLib.load(new DataFiles_Class());
addChild(myLib.getSwf("enemyfire"));
var test = myLib.getSwf("enemydead");
addChild(test);
test.x = 100;

trace(myLib.getXML("Xmlfile_0"));
trace(myLib.getXML("Xmlfile_1"));
   
trace(myLib.getText("Textfile_0"));
trace(myLib.getText("Textfile_1"));
   
trace(myLib.getBin("Binaryfile_0"));
trace(myLib.getBin("Binaryfile_1").length);
   
addChild(new Bitmap(myLib.getBitmapData("PNGfile")));
}//end of test

Step 6[Export Data files as compressed binary package]:
Export all resources as a single compressed binary file and protect your swf assets.
Add this line:
myLib.save("myLib.vLib"); 
in function "Libloaded".
You will get a binary file named "myLib.vLib" which contains all the resources.

Step 7[Use the compressed data file package]:
myLib.addEventListener(Event.COMPLETE, Libloaded);
myLib.init1("./DataFiles/myLib.vLib");
function Libloaded(event:Event):void
{
//All Resources Loaded!
test();
}
Or
Embed the file "myLib.vLib" as a binary asset:
[Embed(source="../bin/DataFiles/myLib.vLib",mimeType="application/octet-stream")]
private static const DataFiles_Class:Class;
myLib.load(new DataFiles_Class());
test();
You can use the data files directly after calling load().
If you Embed all resources using Embed tag or import them into .fla's library, all resources will be compiled into SWF tags. SWF decompilers can read those tags and extract the resources directly.
But in this step, because all resources are encoded into a single binary file, it's a good way to protect the swf assets.

Step 8[Export Data files as a .as class]:
Export .as Class with Embeds:
myLib.XML2AS("./DataFiles/Lib.xml", "myLibT" /*, "myPackageT"*/);
You will get a .as file named "myLibT.as" as follows:
package 
{
     import flash.display.*;
     import flash.media.*;
     import flash.text.*;
     import flash.utils.*;
//Export from vLib-v0.1 by Bruce Jawn (zhoubu1988@gmail.com)

     public class myLibT
     {
     [Embed(source="DataFiles/texts/Textfile_0.txt", mimeType="application/octet-stream")]
     private static const Textfile_0:Class;
     [Embed(source="DataFiles/texts/Textfile_1.txt", mimeType="application/octet-stream")]
     private static const Textfile_1:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_0.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_0:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_1.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_1:Class;
     [Embed(source="DataFiles/textures/JPGfile.JPG")]
     private static const JPGfile:Class;
     [Embed(source="DataFiles/textures/PNGfile.PNG")]
     private static const PNGfile:Class;
     [Embed(source="DataFiles/swfs/enemydead.swf")]
     private static const enemydead:Class;
     [Embed(source="DataFiles/swfs/enemyfire.swf")]
     private static const enemyfire:Class;
     [Embed(source="DataFiles/sounds/sound1.mp3")]
     private static const S0:Class;
     [Embed(source="DataFiles/sounds/sound2.mp3")]
     private static const S1:Class;
     [Embed(source="DataFiles/sounds/sound3.mp3")]
     private static const S2:Class;
     [Embed(source="DataFiles/bins/Binaryfile_0.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_0:Class;
     [Embed(source="DataFiles/bins/Binaryfile_1.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_1:Class;

        private function getClass(name:String):Class
        {
            var ClassReference:Class = getDefinitionByName("myLibT_"+name) as Class;
            return ClassReference;
        }      

  private function getInstance(name:String):Object
  {
   var ClassReference:Class = getClass(name);
   return new ClassReference();
  }
  public function getBin (name:String) : ByteArray
  {
   return ByteArray(getInstance(name));
  }
  
  public function getBitmapData (name:String) : BitmapData
  {
   return Bitmap(getInstance(name)).bitmapData;
  }

        public function getFont (name:String) : Class
  {
   return getClass(name);
   //return Font(getInstance(name));
   //Font.registerFont(EmbeddedFont); 
  }
  
  public function getSound (name:String) : Sound
  {
   return Sound(getInstance(name));
  }

  public function getSwf (name:String) : Sprite
  {
   return Sprite(getInstance(name));
  }

  public function getText (name:String) : String
  {
   return String(getInstance(name));
  }

  public function getXML (name:String) : XML
  {
   return XML(getInstance(name));
  }
 }//end of class 
}//end of package
Then you can import this class into your AS3 projects and compile everything into the final single SWF.

Links:
vLib.swc API references:
http://code.google.com/p/flaswf/wiki/vLib
vLib.swc v0.1 download:
http://flaswf.googlecode.com/svn/trunk/vLib/bin/
Source code of vLib.swc examples:
https://flaswf.googlecode.com/svn/trunk/vLib/examples

Sponsors