PageRenderTime 62ms CodeModel.GetById 33ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  1<?xml version="1.0" encoding="utf-8"?>
  2<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  3			   xmlns:s="library://ns.adobe.com/flex/spark" 
  4			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" viewSourceURL="srcview/index.html" applicationComplete="init()" backgroundColor="#000000">
  5	<fx:Declarations>
  6		<!-- Place non-visual elements (e.g., services, value objects) here -->
  7	</fx:Declarations>
  8	<fx:Script>
  9		<![CDATA[
 10			//
 11			//   Display an image using Paul Bourke's Warp-Mesh  http://local.wasp.uwa.edu.au/~pbourke/dataformats/meshwarp/
 12			//
 13			//
 14			// keys:
 15			//  	move:			up, down, left, right
 16			//		rotate:			a, s
 17			//		change image:   c
 18			//
 19			//
 20			//
 21			//
 22			
 23			import ImageStuff.loadImagesXML;
 24			
 25			import mx.events.ResizeEvent;
 26			
 27			import org.osmf.layout.AbsoluteLayoutFacet;
 28			
 29			import structuredlight.BourkeMesh;
 30			
 31			var verbose:Boolean = false;
 32			
 33			
 34			var pbm:BourkeMesh;
 35			public function importMesh()
 36			{
 37				pbm = new BourkeMesh();
 38				pbm.addEventListener( pbm._completeLoadingEvent, importComplete);
 39				pbm.importMeshFile();
 40			}
 41			
 42			var uvTimer:Number = 0
 43			public function importComplete( e:Event)
 44			{
 45				
 46				_img.source = new Bitmap( e.target.displayMesh() );
 47				_log.text = " map loaded    width" + e.target.width + "  height " + e.target.height; 
 48				
 49				initTriangles();
 50				
 51			}
 52			
 53			//
 54			//
 55			///      testing uv mesh triangles
 56			///
 57			/////
 58			/////////
 59			/////////
 60			/////
 61			///
 62			///
 63			//
 64			//
 65			
 66			var bmIn:Bitmap;
 67			var imageList:loadImagesXML = new loadImagesXML("images/images.xml");
 68			var imageIndex:int = -1 ;
 69			var bmBig:BitmapData;//displayed image
 70			
 71			var xyVertices:Vector.<Number> = new Vector.<Number>;
 72			var uvVertices:Vector.<Number> = new Vector.<Number>;
 73			var indices:Vector.<int> = new Vector.<int>;
 74			
 75			var xyVerticesOriginal:Vector.<Number> = new Vector.<Number>;
 76			var uvVerticesOriginal:Vector.<Number> = new Vector.<Number>;
 77			
 78			private function splitAtWhiteSpace( s:String):Array
 79			{
 80				return s.replace(/^\s+/,"").replace(/\s+$/,"").split(/\s+/);//split at white spaces
 81			}
 82			function init()
 83			{
 84				stage.addEventListener( KeyboardEvent.KEY_DOWN, watchkeys);
 85				stage.addEventListener(Event.RESIZE,stageResize);
 86				
 87			}
 88			function changeImage():Bitmap
 89			{
 90				_log.text += " image cahnged to :" + imageList.image_names[ imageIndex];
 91
 92				imageIndex = (imageIndex + 1)%imageList.images.length;
 93				bmIn  = imageList.images[ imageIndex ] ; 
 94				redrawImage( bmIn.bitmapData);
 95				return bmIn;
 96			}
 97			
 98			function initTriangles()
 99			{	
100				uvTimer = getTimer();
101				var meshRa:Array = new Array( pbm.width );
102				//var bmd:BitmapData = new BitmapData( stage.width, stage.height,false,0xd00d00);
103				
104				var indexs:Array = pbm.meshString.split( "\n");
105				var type:int = int(indexs[0]);
106				var mesh:Array = indexs.slice(2);//TAKE EVERYTHING AFTER THE FIRST TWO LINES
107				var dimensionsString:String = indexs[1]; //PARSE DIMENSIONS FROM SECOND LINE
108				var dimensions:Array = splitAtWhiteSpace( dimensionsString); 
109			    width = int( dimensions[0]) ;
110				height = int(dimensions[1]) ;
111				
112				var index:int = 0 ;
113				indices = new Vector.<int>;
114				xyVertices = new Vector.<Number>;
115				uvVertices = new Vector.<Number>;
116				
117				xyVerticesOriginal = new Vector.<Number>;
118				uvVerticesOriginal = new Vector.<Number>;
119				
120				//
121				//
122				////
123				////  parse the String into usable numbers
124				//  
125				//
126				if( !pbm)
127				{
128					_log.text += "\n load mesh file first ";
129					return;
130				}
131
132				for( var x1:int = 0; x1 < pbm.width; x1++)
133				{
134					meshRa[x1] = new Array( pbm.height);
135				}
136				
137
138				
139				//var sh1:Shape = new Shape();
140				_log.text += " indexs length:"+ indexs.length + " \n" ;//+ "  " +meshArray.toString();
141				_log.text += " mesh array length : " + mesh.length + "\n";
142				_log.text += " Loaded mesh :  width" + width + "  height "+ height + " \n"; 
143				_log.text += " stage width : " + stage.width + " height " + stage.height + "\n";
144				
145				//
146				//  Do the uv mapping with the triangles
147				//
148				//
149		
150				for ( var n:int = 0 ; n < mesh.length; n++)
151				{
152					var tmp:String = mesh[n];
153					var m:Array = splitAtWhiteSpace(tmp);//split at white spaces
154					
155					if( m.length != 5){
156						// THIS LINE DOES NOT LOOK RIGHT
157						// SKIP IT. it's usually like an extra carrier return at the endof the page. 
158						_log.text += "ERROR m.lengt != 5 " + m + "   at  " + n;
159					}
160					else
161					{
162						//
163						// get corrected x,y
164						var xc:int = n % width;
165						var yc:int = Math.floor( n/width);
166						
167						var cent:Array =  splitAtWhiteSpace( mesh[n]);
168						
169						if(verbose)
170							_log.text += " ( " + xc +" ,  " + yc+")";
171						
172						
173						var x2:Number = (Number(cent[0]) + 1 ) * stage.width / 2;
174						var y2:Number = (Number(cent[1]) + 1 ) * stage.height / 2;
175						var u2:Number = Number(cent[2])  ;
176						var v2:Number = Number(cent[3])   ;
177						
178						if( _invertBox.selected ){
179							y2 = ( - Number(cent[1]) + 1 ) * stage.height / 2;	
180							v2 = 1 - Number(cent[3]) ;
181						}
182						
183						var vert:Object = {  x: x2, y: y2, u: u2 , v : v2 , i: cent[4] , index: int(index) };
184						
185						if(verbose)
186							_log.text += "\n x: " + vert.x + " y:"+ vert.y + " u:"+ vert.u +" v:"+ vert.v +" i:"+ vert.i +   " indx:"+ vert.index ; 
187						meshRa[xc][yc] = vert ;
188						
189						xyVertices.push( vert.x);
190						xyVertices.push( vert.y);
191						uvVertices.push( vert.u);
192						uvVertices.push( vert.v);
193						
194						xyVerticesOriginal.push( Number(vert.x) );
195						xyVerticesOriginal.push( Number(vert.y) );
196						uvVerticesOriginal.push( Number(vert.u) );
197						uvVerticesOriginal.push( Number(vert.v) );
198						
199						
200						index++;
201					}			
202				}
203			
204				for( var x:int = 0; x < meshRa.length - 1 ; x++)
205				{
206					for( var y:int = 0 ; y < meshRa[0].length - 1; y++)
207					{
208						var center:Object = meshRa[ x ][ y ];
209						var right:Object = meshRa[x + 1][ y ];
210						var down:Object = meshRa[ x ][ y + 1 ];
211						var diag:Object = meshRa[x + 1][ y + 1];
212						if( center.i > 0 && right.i > 0 && down.i > 0 && diag.i > 0)
213						{
214							//push triangles
215							//  ____
216							// |\ T|    T = top triangle
217							// |B\ |	B = bottom triangle
218							// |__\|
219							//
220							//first: top triange
221							indices.push( center.index );
222							indices.push( right.index );
223							indices.push( diag.index );
224							//
225							//  second: bottom triangle 
226							indices.push( center.index );
227							indices.push( down.index );
228							indices.push( diag.index );
229							
230						}
231					}
232				}
233				
234				bmIn = changeImage();
235
236				//
237				// count time elapsed
238				_log.text += "\n uv time elapsed : " +  (getTimer() - uvTimer);  
239				
240			}//...drawTriangles
241			
242			var bmChild:DisplayObject;
243		
244			function redrawImage( bm2:BitmapData )
245			{
246				_log.text += "\n redrawing image "
247				
248
249//add the image to the stage
250				if( !bmChild )
251				{
252					bmBig = new BitmapData( stage.width, stage.height,false,0);
253					bmChild = stage.addChild( new Bitmap( bmBig) );
254					//bmChild.name = ("displayed image");
255				}
256				else if( bmChild.width != stage.width || bmChild.height != stage.height )
257				{
258					bmBig = new BitmapData( stage.width, stage.height,false, 0xff0f0f);
259
260					stage.removeChild(bmChild);
261					bmChild = stage.addChild( new Bitmap( bmBig));
262				}
263				
264				//
265				// do the Heavy lifting
266				var outShape:Shape = new Shape();
267				outShape.graphics.beginBitmapFill( bm2);
268				outShape.graphics.drawTriangles( xyVertices, indices, uvVertices);
269				outShape.graphics.endFill();
270				
271				bmBig.draw( outShape );
272				
273				
274				if( _showTriangles.selected )
275				{
276					for( var i:int = 2 ; i < indices.length; i = i + 3)
277					{
278					}	
279				}
280				
281				
282				//_img.source =  new Bitmap( bmBig) ;
283			
284					//bmChild.name = "bitmap child";
285				
286				
287			}
288			
289			
290			
291			function matrixTransform( matrix:Matrix )
292			{	
293				var ucoord:Number ;
294				var vcoord:Number ;
295				for( var n:int = 0 ; n < uvVertices.length; n = n + 2 )
296				{	
297					ucoord = Number(uvVertices[n]);	
298					vcoord = Number(uvVertices[ n+1]);
299					
300					uvVertices[n] = matrix.a * ucoord + matrix.c * ucoord + matrix.tx;//x
301					uvVertices[n+1] = matrix.b * vcoord + matrix.d * vcoord + matrix.ty;//y
302				}
303			}
304			function matrixTransformOfOriginal( matrix:Matrix )
305			{	
306				_log.text += "\n matrixtransform of original " + matrix.toString() + matrix.tx + "," +matrix.ty;
307				var ycoord:Number ;
308				var xcoord:Number ;
309				for( var n:int = 0 ; n < uvVerticesOriginal.length; n = n + 2 )
310				{	
311					xcoord = uvVerticesOriginal[n];	
312					ycoord = uvVerticesOriginal[ n+1];
313					
314					uvVertices[n] = (matrix.a * xcoord) + (matrix.c * ycoord) + matrix.tx;//x
315					uvVertices[n+1] = (matrix.b * xcoord) + (matrix.d * ycoord) + matrix.ty;//y
316				}
317			}
318			
319			var currMatrix:Matrix = new Matrix(1,0,0,1,0,0);
320			
321			function watchkeys( e:KeyboardEvent )
322			{
323				
324				_log.text += "\n key pressed " + e.type + " , " + e.keyCode ;
325				if(e.keyCode == Keyboard.LEFT){
326					currMatrix.tx -= 0.1;
327				}
328				else if(e.keyCode == Keyboard.RIGHT )
329				{
330					currMatrix.tx += 0.1;
331				}
332				else if(e.keyCode == Keyboard.UP )
333				{
334					currMatrix.ty -= 0.04;
335
336				}
337				else if(e.keyCode == Keyboard.DOWN )
338				{
339					currMatrix.ty += 0.04;
340				}
341				else if(e.keyCode == 187)
342				{
343					//zoom in
344					currMatrix.scale( 0.9 , 0.9);
345				}
346				else if(e.keyCode == 189)
347				{
348					//zoom out
349					currMatrix.scale( 1.1, 1.1);
350				}
351				else if( e.keyCode == 65)//a rotate
352				{
353					currMatrix.rotate( 0.157);
354				}
355				else if( e.keyCode == 83)//s rotate
356				{
357					currMatrix.rotate( -0.157);
358				}
359				else if( e.keyCode == 67 )//changeImage
360				{
361					changeImage();
362				}
363				
364				if(pbm)
365				{
366					matrixTransformOfOriginal( currMatrix );
367					//_log.text += "cents " + centx + "," + centy;
368					redrawImage( bmIn.bitmapData );
369				}
370			}
371			
372			
373			public function goFullScreen( )
374			{
375				stage.addEventListener(FullScreenEvent.FULL_SCREEN, goBIGhandler)
376				stage.displayState = "fullScreen"	;
377				_log.text += " \n going Full Screen ";
378			}
379			public function goBIGhandler( e:FullScreenEvent)
380			{
381				stageResize();
382			}
383			public function stageResize(e:Event = null)
384			{
385				_log.text += "stage resized" + stage.width + "," +stage.height;
386				if(stage && bmIn)
387				{
388					//bmBig = new BitmapData( stage.width, stage.height,false,0);
389					initTriangles();
390				}
391			}
392			
393		]]>
394	</fx:Script>
395	
396	<mx:Image x="0" y="0" id="_img" />
397	<s:Button x="69" y="67" label="load mesh .data" click="importMesh()"/>
398	<s:TextArea x="18" y="242" width="421" height="450" id="_log" contentBackgroundAlpha="0.5" color="#FFFFFF" contentBackgroundColor="#000000"/>
399	<s:Button x="68" y="183" label="change Image" click="changeImage()"/>
400	<s:Button x="69" y="106" label="FULL screen" click="goFullScreen()"/>
401	<s:CheckBox x="199" y="186" label="invert" id="_invertBox" width="55" selected="true" color="#AAAAAA"/>
402	<s:CheckBox x="199" y="212" label="show Triangles" id="_showTriangles" color="#AAAAAA"/>
403	
404</s:Application>
405