/src/uvTest3_3.mxml
http://github.com/m0ose/ambient-pixel-action-script · Macromedia eXtensible Markup Language · 405 lines · 329 code · 76 blank · 0 comment · 0 complexity · 43ef82bbdae03f3f1e624ab52b2808e6 MD5 · raw file
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" viewSourceURL="srcview/index.html" applicationComplete="init()" backgroundColor="#000000">
- <fx:Declarations>
- <!-- Place non-visual elements (e.g., services, value objects) here -->
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- //
- // Display an image using Paul Bourke's Warp-Mesh http://local.wasp.uwa.edu.au/~pbourke/dataformats/meshwarp/
- //
- //
- // keys:
- // move: up, down, left, right
- // rotate: a, s
- // change image: c
- //
- //
- //
- //
-
- import ImageStuff.loadImagesXML;
-
- import mx.events.ResizeEvent;
-
- import org.osmf.layout.AbsoluteLayoutFacet;
-
- import structuredlight.BourkeMesh;
-
- var verbose:Boolean = false;
-
-
- var pbm:BourkeMesh;
- public function importMesh()
- {
- pbm = new BourkeMesh();
- pbm.addEventListener( pbm._completeLoadingEvent, importComplete);
- pbm.importMeshFile();
- }
-
- var uvTimer:Number = 0
- public function importComplete( e:Event)
- {
-
- _img.source = new Bitmap( e.target.displayMesh() );
- _log.text = " map loaded width" + e.target.width + " height " + e.target.height;
-
- initTriangles();
-
- }
-
- //
- //
- /// testing uv mesh triangles
- ///
- /////
- /////////
- /////////
- /////
- ///
- ///
- //
- //
-
- var bmIn:Bitmap;
- var imageList:loadImagesXML = new loadImagesXML("images/images.xml");
- var imageIndex:int = -1 ;
- var bmBig:BitmapData;//displayed image
-
- var xyVertices:Vector.<Number> = new Vector.<Number>;
- var uvVertices:Vector.<Number> = new Vector.<Number>;
- var indices:Vector.<int> = new Vector.<int>;
-
- var xyVerticesOriginal:Vector.<Number> = new Vector.<Number>;
- var uvVerticesOriginal:Vector.<Number> = new Vector.<Number>;
-
- private function splitAtWhiteSpace( s:String):Array
- {
- return s.replace(/^\s+/,"").replace(/\s+$/,"").split(/\s+/);//split at white spaces
- }
- function init()
- {
- stage.addEventListener( KeyboardEvent.KEY_DOWN, watchkeys);
- stage.addEventListener(Event.RESIZE,stageResize);
-
- }
- function changeImage():Bitmap
- {
- _log.text += " image cahnged to :" + imageList.image_names[ imageIndex];
- imageIndex = (imageIndex + 1)%imageList.images.length;
- bmIn = imageList.images[ imageIndex ] ;
- redrawImage( bmIn.bitmapData);
- return bmIn;
- }
-
- function initTriangles()
- {
- uvTimer = getTimer();
- var meshRa:Array = new Array( pbm.width );
- //var bmd:BitmapData = new BitmapData( stage.width, stage.height,false,0xd00d00);
-
- var indexs:Array = pbm.meshString.split( "\n");
- var type:int = int(indexs[0]);
- var mesh:Array = indexs.slice(2);//TAKE EVERYTHING AFTER THE FIRST TWO LINES
- var dimensionsString:String = indexs[1]; //PARSE DIMENSIONS FROM SECOND LINE
- var dimensions:Array = splitAtWhiteSpace( dimensionsString);
- width = int( dimensions[0]) ;
- height = int(dimensions[1]) ;
-
- var index:int = 0 ;
- indices = new Vector.<int>;
- xyVertices = new Vector.<Number>;
- uvVertices = new Vector.<Number>;
-
- xyVerticesOriginal = new Vector.<Number>;
- uvVerticesOriginal = new Vector.<Number>;
-
- //
- //
- ////
- //// parse the String into usable numbers
- //
- //
- if( !pbm)
- {
- _log.text += "\n load mesh file first ";
- return;
- }
- for( var x1:int = 0; x1 < pbm.width; x1++)
- {
- meshRa[x1] = new Array( pbm.height);
- }
-
-
- //var sh1:Shape = new Shape();
- _log.text += " indexs length:"+ indexs.length + " \n" ;//+ " " +meshArray.toString();
- _log.text += " mesh array length : " + mesh.length + "\n";
- _log.text += " Loaded mesh : width" + width + " height "+ height + " \n";
- _log.text += " stage width : " + stage.width + " height " + stage.height + "\n";
-
- //
- // Do the uv mapping with the triangles
- //
- //
-
- for ( var n:int = 0 ; n < mesh.length; n++)
- {
- var tmp:String = mesh[n];
- var m:Array = splitAtWhiteSpace(tmp);//split at white spaces
-
- if( m.length != 5){
- // THIS LINE DOES NOT LOOK RIGHT
- // SKIP IT. it's usually like an extra carrier return at the endof the page.
- _log.text += "ERROR m.lengt != 5 " + m + " at " + n;
- }
- else
- {
- //
- // get corrected x,y
- var xc:int = n % width;
- var yc:int = Math.floor( n/width);
-
- var cent:Array = splitAtWhiteSpace( mesh[n]);
-
- if(verbose)
- _log.text += " ( " + xc +" , " + yc+")";
-
-
- var x2:Number = (Number(cent[0]) + 1 ) * stage.width / 2;
- var y2:Number = (Number(cent[1]) + 1 ) * stage.height / 2;
- var u2:Number = Number(cent[2]) ;
- var v2:Number = Number(cent[3]) ;
-
- if( _invertBox.selected ){
- y2 = ( - Number(cent[1]) + 1 ) * stage.height / 2;
- v2 = 1 - Number(cent[3]) ;
- }
-
- var vert:Object = { x: x2, y: y2, u: u2 , v : v2 , i: cent[4] , index: int(index) };
-
- if(verbose)
- _log.text += "\n x: " + vert.x + " y:"+ vert.y + " u:"+ vert.u +" v:"+ vert.v +" i:"+ vert.i + " indx:"+ vert.index ;
- meshRa[xc][yc] = vert ;
-
- xyVertices.push( vert.x);
- xyVertices.push( vert.y);
- uvVertices.push( vert.u);
- uvVertices.push( vert.v);
-
- xyVerticesOriginal.push( Number(vert.x) );
- xyVerticesOriginal.push( Number(vert.y) );
- uvVerticesOriginal.push( Number(vert.u) );
- uvVerticesOriginal.push( Number(vert.v) );
-
-
- index++;
- }
- }
-
- for( var x:int = 0; x < meshRa.length - 1 ; x++)
- {
- for( var y:int = 0 ; y < meshRa[0].length - 1; y++)
- {
- var center:Object = meshRa[ x ][ y ];
- var right:Object = meshRa[x + 1][ y ];
- var down:Object = meshRa[ x ][ y + 1 ];
- var diag:Object = meshRa[x + 1][ y + 1];
- if( center.i > 0 && right.i > 0 && down.i > 0 && diag.i > 0)
- {
- //push triangles
- // ____
- // |\ T| T = top triangle
- // |B\ | B = bottom triangle
- // |__\|
- //
- //first: top triange
- indices.push( center.index );
- indices.push( right.index );
- indices.push( diag.index );
- //
- // second: bottom triangle
- indices.push( center.index );
- indices.push( down.index );
- indices.push( diag.index );
-
- }
- }
- }
-
- bmIn = changeImage();
- //
- // count time elapsed
- _log.text += "\n uv time elapsed : " + (getTimer() - uvTimer);
-
- }//...drawTriangles
-
- var bmChild:DisplayObject;
-
- function redrawImage( bm2:BitmapData )
- {
- _log.text += "\n redrawing image "
-
- //add the image to the stage
- if( !bmChild )
- {
- bmBig = new BitmapData( stage.width, stage.height,false,0);
- bmChild = stage.addChild( new Bitmap( bmBig) );
- //bmChild.name = ("displayed image");
- }
- else if( bmChild.width != stage.width || bmChild.height != stage.height )
- {
- bmBig = new BitmapData( stage.width, stage.height,false, 0xff0f0f);
- stage.removeChild(bmChild);
- bmChild = stage.addChild( new Bitmap( bmBig));
- }
-
- //
- // do the Heavy lifting
- var outShape:Shape = new Shape();
- outShape.graphics.beginBitmapFill( bm2);
- outShape.graphics.drawTriangles( xyVertices, indices, uvVertices);
- outShape.graphics.endFill();
-
- bmBig.draw( outShape );
-
-
- if( _showTriangles.selected )
- {
- for( var i:int = 2 ; i < indices.length; i = i + 3)
- {
- }
- }
-
-
- //_img.source = new Bitmap( bmBig) ;
-
- //bmChild.name = "bitmap child";
-
-
- }
-
-
-
- function matrixTransform( matrix:Matrix )
- {
- var ucoord:Number ;
- var vcoord:Number ;
- for( var n:int = 0 ; n < uvVertices.length; n = n + 2 )
- {
- ucoord = Number(uvVertices[n]);
- vcoord = Number(uvVertices[ n+1]);
-
- uvVertices[n] = matrix.a * ucoord + matrix.c * ucoord + matrix.tx;//x
- uvVertices[n+1] = matrix.b * vcoord + matrix.d * vcoord + matrix.ty;//y
- }
- }
- function matrixTransformOfOriginal( matrix:Matrix )
- {
- _log.text += "\n matrixtransform of original " + matrix.toString() + matrix.tx + "," +matrix.ty;
- var ycoord:Number ;
- var xcoord:Number ;
- for( var n:int = 0 ; n < uvVerticesOriginal.length; n = n + 2 )
- {
- xcoord = uvVerticesOriginal[n];
- ycoord = uvVerticesOriginal[ n+1];
-
- uvVertices[n] = (matrix.a * xcoord) + (matrix.c * ycoord) + matrix.tx;//x
- uvVertices[n+1] = (matrix.b * xcoord) + (matrix.d * ycoord) + matrix.ty;//y
- }
- }
-
- var currMatrix:Matrix = new Matrix(1,0,0,1,0,0);
-
- function watchkeys( e:KeyboardEvent )
- {
-
- _log.text += "\n key pressed " + e.type + " , " + e.keyCode ;
- if(e.keyCode == Keyboard.LEFT){
- currMatrix.tx -= 0.1;
- }
- else if(e.keyCode == Keyboard.RIGHT )
- {
- currMatrix.tx += 0.1;
- }
- else if(e.keyCode == Keyboard.UP )
- {
- currMatrix.ty -= 0.04;
- }
- else if(e.keyCode == Keyboard.DOWN )
- {
- currMatrix.ty += 0.04;
- }
- else if(e.keyCode == 187)
- {
- //zoom in
- currMatrix.scale( 0.9 , 0.9);
- }
- else if(e.keyCode == 189)
- {
- //zoom out
- currMatrix.scale( 1.1, 1.1);
- }
- else if( e.keyCode == 65)//a rotate
- {
- currMatrix.rotate( 0.157);
- }
- else if( e.keyCode == 83)//s rotate
- {
- currMatrix.rotate( -0.157);
- }
- else if( e.keyCode == 67 )//changeImage
- {
- changeImage();
- }
-
- if(pbm)
- {
- matrixTransformOfOriginal( currMatrix );
- //_log.text += "cents " + centx + "," + centy;
- redrawImage( bmIn.bitmapData );
- }
- }
-
-
- public function goFullScreen( )
- {
- stage.addEventListener(FullScreenEvent.FULL_SCREEN, goBIGhandler)
- stage.displayState = "fullScreen" ;
- _log.text += " \n going Full Screen ";
- }
- public function goBIGhandler( e:FullScreenEvent)
- {
- stageResize();
- }
- public function stageResize(e:Event = null)
- {
- _log.text += "stage resized" + stage.width + "," +stage.height;
- if(stage && bmIn)
- {
- //bmBig = new BitmapData( stage.width, stage.height,false,0);
- initTriangles();
- }
- }
-
- ]]>
- </fx:Script>
-
- <mx:Image x="0" y="0" id="_img" />
- <s:Button x="69" y="67" label="load mesh .data" click="importMesh()"/>
- <s:TextArea x="18" y="242" width="421" height="450" id="_log" contentBackgroundAlpha="0.5" color="#FFFFFF" contentBackgroundColor="#000000"/>
- <s:Button x="68" y="183" label="change Image" click="changeImage()"/>
- <s:Button x="69" y="106" label="FULL screen" click="goFullScreen()"/>
- <s:CheckBox x="199" y="186" label="invert" id="_invertBox" width="55" selected="true" color="#AAAAAA"/>
- <s:CheckBox x="199" y="212" label="show Triangles" id="_showTriangles" color="#AAAAAA"/>
-
- </s:Application>