Tuesday, February 17, 2009

Flash webcam tracking experiment

Used the same logic in Touchless SDK. port it into Flash. tracking using colour histogram.
Use the mouse to drag and select an object to track.

Monday, January 26, 2009

Flash 10, 3D and BSP Tree

Used a Binary Space Partitioning (BSP) Tree to sort the order of the triangles to be drawn on the screen using Flash 10 3D API.

http://psalmhundred.net/experiment/flash3d/coffee_table_bsp/
BSP Tree Component: BSPTree.swc
Updated BSPTree.swc: 01 Feb 2009. Resolved some bugs.
02 Feb 2009. optimised code
24 Feb 2009. changed code. using timer-based instead of loops to add triangles to tree nodes. to avoid script timeout limit.

1) Import package
import dmp.*;
2) Create a BSPTree object
var tree:BSPTree = new BSPTree();
3) Add the vertices and uvt data of the model
tree.add(vertices, uvt);
4) Sort by model composite transform or by camera location
tree.sortByModelTransform(compositeM, outVertices, outUVT);
OR
tree.sortByCameraLoc(cameraLoc, outVertices, outUVT);
5) Transform & project outVertices and outUVT to draw model

Source:
BSP Tree library
BSP Tree sample program

Tuesday, January 20, 2009

Security Error 2148: while loading XML

http://adobeflash.wordpress.com/2007/01/13/error-2148-loading-xml-with-flex2-security-error/

adding the "-use-network=false" argument works in Flex Builder.

hmmm. strange. sometimes, it works without adding the argument.

Monday, January 19, 2009

OBJ to DMP format

A convertor I wrote using Actionscript 3.0 and Flex to convert from OBJ to a custom XML format i used for my students' assignment
link to OBJ format: http://en.wikipedia.org/wiki/Obj
Export from 3D tools, eg.: Maya, to OBJ format
Open the OBJ file in a text editor, eg.: Notepad. Copy and paste the data into this application. Enter the texture filename. and click "Convert"

Tuesday, January 06, 2009

3D in Flash 10

Loaded a COLLADA model from an external DAE file.
Use cursor keys, W,A,S,D and the mouse to manipulate the model.
Flash 10 player required. http://get.adobe.com/flashplayer/
Applied simple clipping, sorting of triangles.
Frame rate will drop when number of triangles is about 1K+.


http://psalmhundred.net/experiment/flash3d/TrianglesApp.html

Wednesday, November 26, 2008

Sunday, November 16, 2008

Flash AS3 Custom Component - parameters

If the parameters are modified during authoring time,
they are not ready til after the constructor is called.


package{
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class Testing extends MovieClip{
[Inspectable]
public var id:Number = 0;
function Testing(){
this.addEventListener(MouseEvent.CLICK, mouseclick);
}
function mouseclick(e:MouseEvent):void{
trace("id" + id);
}
}
}

Tuesday, August 12, 2008

Elastic Flash Demo

Key in a number from 2-20 in the textbox and click the button to generate an elastic object. Use the mouse to drag the dots.

Tuesday, July 08, 2008

Microsoft.Win32.OpenFileDialog affect FileIO operation

Just found out that if I use the following code to retrieve a filename, the FileStream will be affected as the current directory will be set to where the filename was taken from, eg.: "My Documents", instead of the Application Path.

OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All files (*.*)|*.*";
if ((bool)dialog.ShowDialog())
{
// store filename
}

// and later in the code
FileStream stream = new FileStream(
"mydata.bin", FileMode.Create);
BinaryWriter writer = new BinaryWriter(stream);
// write some data
stream.Close();
// "mydata.bin" will be saved in the current directory of OpenFileDialog

One solution will be to use the application path and extract the directory:
String path = System.Reflection.Assembly.GetExecutingAssembly().Location;

another will be to use RestoreDirectory property of the OpenFileDialog. set it to true.

Thursday, June 19, 2008

Flash 10 3D Demo - partial cube

NOT working. Flash 10 player has been updated to official release.

Requires Flash 10 (currently beta)
http://labs.adobe.com/downloads/flashplayer10.html

Demo of rotating cube(partial to see culling effect):
http://psalmhundred.net/experiment/flash3d/Flash10App.html

Flash 10 3D API - Utils3D.projectVectors(...)

Utils3D.projectVectors(m:Matrix3D, verts:Vector, projectedVerts:Vector, uvts:Vector) causes Flash player 10 beta to crash currently.

so just a workaround, a custom method to do similar thing:

public function projectVectors(projMatrix:Matrix3D, vert:Vector.<Number>,
v2D:Vector.<Number>, uvt:Vector.<Number>):void{

var temp:Vector.<Number> = new Vector.<Number>(vert.length);
projMatrix.transformVectors(vert, temp);
var z:Number = 0;
for(var i:int = 0; i<temp.length/3; i++){
z = temp[i*3+2];
z = focalLength / (focalLength+z);
// x' = f*x/(f+z), y' = f*y/(f+z)
v2D[i*2] = temp[i*3]*z;
v2D[i*2+1] = temp[i*3+1]*z;
// t = f/(f+z)
uvt[i*3+2] = z;
}
}

Thursday, April 12, 2007

WPF: Media Player

MediaPlayer player;
Uri uri = new Uri("pack://siteoforigin:,,,/music.mp3");
player = new MediaPlayer();
player.Open(uri);
player.Play();

Wednesday, April 11, 2007

WPF: setting cursor

eg:
void Page1_MouseMove(object sender, MouseEventArgs e)
{
e.MouseDevice.SetCursor(Cursors.Hand); //-- hand cursor
e.MouseDevice.SetCursor(Cursors.None); //-- none, invisible
}

WPF: Image Drawing

XAML:
<Image Name="image1" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" />

C#:
DrawingGroup group = new DrawingGroup();
DrawingImage di = new DrawingImage(group);
//-- create clipping bound for drawing group
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = new Rect(0,0,250,250);
group.ClipGeometry = myRectangleGeometry;
image1.Source = di;

//-- load image source (refer to prev post)
ImageSource img2 = ...

//-- to draw something
using (DrawingContext dc = group.Open())
{
dc.DrawImage(img2, new Rect(x, y, w, h));
//-- and other stuff
}

Friday, March 30, 2007

C#: using the prev LinkList

LinkList<Dog> list = new LinkList<Dog>();
list.addLast(new Dog("Rocky", 16));
list.addLast(new Dog("Waggie", 6));
list.remove(0);
list.get(0).bark();

C#: reading text files

using System;
using System.IO;

namespace FileIOText
{
class ReadTextFile
{
public ReadTextFile()
{
try
{
//-- "using" also closes the file
using (StreamReader sr = new StreamReader("data.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch(Exception e){
Console.WriteLine("File could not be read");
Console.WriteLine(e.Message);
}
}
}
}

C#: reading binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace FileIOText
{
class ReadBinaryFile
{
const String filename = "binfile.dat";
public ReadBinaryFile()
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
/*
//create reader
BinaryReader br = new BinaryReader(fs);

Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadDouble());
Console.WriteLine(br.ReadBoolean());
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadChar());

br.Close();
*/

BinaryFormatter formatter = new BinaryFormatter();
Dog d = (Dog)formatter.Deserialize(fs);
Console.WriteLine(d.Name);
Console.WriteLine(d.Age);
fs.Close();
}
}
}

C#: writing to text file

using System;
using System.IO;

namespace FileIOText
{
class WriteTextFile
{
public WriteTextFile()
{
try
{
//-- "using" also closes the file
using (StreamWriter sw = new StreamWriter("data.txt"))
{
sw.Write("Hello...");
sw.WriteLine("This is the start of the writing...");
sw.WriteLine("------------------");
//-- write arbitrary objects
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
catch (Exception e)
{
Console.WriteLine("File could not be written");
Console.WriteLine(e.Message);
}
}
}
}

C#: Writing binary file

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace FileIOText
{
class WriteBinaryFile
{
const String filename = "binfile.dat";
public WriteBinaryFile()
{
if(File.Exists(filename)){
Console.WriteLine("{0} already exists!", filename);
return;
}
FileStream fs = new FileStream(filename, FileMode.CreateNew);
/*
//create writer
BinaryWriter bw = new BinaryWriter(fs);

//create test data to write to file
int i = 299;
double d = 2.5999;
bool b = false;
String s = "Haha";
char c = 'A';

bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
bw.Write(c);
*/

Dog d = new Dog("Rocky", 16);
Dog e = new Dog("Waggie", 10);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, d);
formatter.Serialize(fs, e);
//bw.Close();
fs.Close();
}
}
}

C#: LinkList

using System;
using System.Collections.Generic;
using System.Text;

namespace List
{
[Serializable]
class LinkList<T>
{
int numOfElements = 0;
ListNode<T> headNode = null;
public LinkList() { }
public LinkList(T data) {
headNode = new ListNode<T>(data);
}
public bool isEmpty()
{
return (numOfElements == 0);
}
public int getNumOfElements()
{
return numOfElements;
}
public ListNode<T> find(int index)
{
ListNode<T> node = headNode;
if(index < 0 || index >= numOfElements){
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
for (int i = 0; i < index; i++ )
{
node = node.Next;
}
return node;
}
public T get(int index)
{
/*
if (index >= 0 && index < numOfElements)
{
return find(index).Data;
}*/
return find(index).Data;
}
public void add(int index, T data)
{
if (index == 0)
{
ListNode<T> newNode = new ListNode<T>(data, headNode);
headNode = newNode;
}
else if (index > 0 && index <= numOfElements)
{
ListNode<T> prevNode = find(index - 1);
ListNode<T> newNode = new ListNode<T>(data, prevNode.Next);
prevNode.Next = newNode;
}else{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements++;
}
public void addLast(T data)
{
add(getNumOfElements(), data);
}
public void remove(int index)
{
if (index == 0)
{
headNode = headNode.Next;
}
else if (index > 0 && index < numOfElements)
{
ListNode<T> prev = find(index - 1);
ListNode<T> current = prev.Next;
prev.Next = current.Next;
}
else
{
throw new IndexOutOfRangeException("Error! Out of List index bounds");
}
numOfElements--;
}
public void removeLast()
{
remove(numOfElements - 1);
}
}
}