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