first steps : vertex/index buffers in flash

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

first steps : vertex/index buffers in flash

Post by k1net1k »

so i have finally made the time to start learning this stuff. I got a project to compile and it works :)

trouble is i dont really like assembly and writing my own shaders. its a bit too hardcore for me. So i will be making some new stuff using some 3rd party libraries that abstract this low level stuff away for scripters like me.

you will need the flash player 11 incubator player (and that means uninstalling your normal flash player). I would suggest installing this one on another browser than your everyday browser as it still has some bugs, and an annoying watermark. get it from here.
http://labs.adobe.com/downloads/flashpl ... bator.html

and then to see what i have made so far in all its glory, see it here
http://www.mjt.net.au/labs/molehill/

for those interested, this is the code that is used. and i just copied this from someones blog so i could try it out. I am already finding this hard to grasp, but will learn it over time.

Code: Select all

/// ////////////////////////
package
{
	import com.adobe.utils.*;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display3D.*;
	import flash.display3D.textures.Texture;
	import flash.events.*;
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.events.MouseEvent;
	
	[SWF(width="800", height="600", frameRate="60", backgroundColor="#999966")]
	public class Main extends Sprite
	{
		[Embed( source = "harley.jpg" )]
		protected const TextureBitmap:Class;
		
		protected var context3D:Context3D;
		protected var vertexbuffer:VertexBuffer3D;
		protected var indexBuffer:IndexBuffer3D; 
		protected var program:Program3D;
		protected var texture:Texture;
		
		public function Main()
		{
			stage.stage3Ds[0].addEventListener( Event.CONTEXT3D_CREATE, initMolehill );
			stage.stage3Ds[0].requestContext3D();
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;			

			addEventListener(Event.ENTER_FRAME, onRender);
			
			
		}
		
		protected function initMolehill(e:Event):void
		{
			context3D = stage.stage3Ds[0].context3D;			
			stage.stage3Ds[0].viewPort = new Rectangle(0, 0, 800, 600);		
			
			context3D.configureBackBuffer(stage.stage3Ds[0].viewPort.width, stage.stage3Ds[0].viewPort.height, 4, true);
			
			
			var vertices:Vector.<Number> = Vector.<Number>([
				-0.5,-0.5,0, 0, 0, // x, y, z, u, v
				-0.5, 0.5, 0, 0, 1,
				0.5, 0.5, 0, 1, 1,
				0.5, -0.5, 0, 1, 0]);
			
				
			// 4 vertices, of 5 Numbers each
			vertexbuffer = context3D.createVertexBuffer(4, 5);
			// offset 0, 4 vertices
			vertexbuffer.uploadFromVector(vertices, 0, 4);
			
			// total of 6 indices. 2 triangles by 3 vertices each
			indexBuffer = context3D.createIndexBuffer(6);			
			
			// offset 0, count 6
			indexBuffer.uploadFromVector (Vector.<uint>([0, 1, 2, 2, 3, 0]), 0, 6);
			
			var bitmap:Bitmap = new TextureBitmap();
			texture = context3D.createTexture(bitmap.bitmapData.width, bitmap.bitmapData.height, Context3DTextureFormat.BGRA, false);
			texture.uploadFromBitmapData(bitmap.bitmapData);
			
			var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
			vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
				"m44 op, va0, vc0\n" + // pos to clipspace
				"mov v0, va1" // copy uv
			);
			var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
			fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
				"tex ft1, v0, fs0 <2d,linear,nomip>\n" +
				"mov oc, ft1"
			);
			
			program = context3D.createProgram();
			program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
			
		}
		
		protected function onRender(e:Event):void
		{
			if ( !context3D ) 
				return;
			
			context3D.clear ( 1, 1, 1, 1 );
			
			// vertex position to attribute register 0
			context3D.setVertexBufferAt (0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			// uv coordinates to attribute register 1
			context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
			// assign texture to texture sampler 0
			context3D.setTextureAt(0, texture);			
			// assign shader program
			context3D.setProgram(program);
			
			var m:Matrix3D = new Matrix3D();
			//m.appendRotation( getTimer()/50, Vector3D.Z_AXIS);
			m.appendRotation( mouseX, Vector3D.Z_AXIS);
			
			context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
			
			context3D.drawTriangles(indexBuffer);
			
			trace(context3D.driverInfo);
			
			context3D.present();
		}
	}
}
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: first steps : vertex/index buffers in flash

Post by k1net1k »

a spinning cube, code crudely hacked to bits from some tutorials i read
http://www.mjt.net.au/labs/molehill/index2.html

and again from a tutorial i found, car driving game (using away3d, so a lot higher level and more able for me to grasp)
stuff like this

Code: Select all

// CAR
			car = new Object3D();
			// car body
			carBody = new Box(200,300,100);
			var material:FillMaterial = new FillMaterial(0xFFFF00, 0.8);
			carBody.setMaterialToAllSurfaces(material);
			carBody.y = -200;
			car.addChild(carBody);
http://www.mjt.net.au/labs/molehill/index3.html
Image
Post Reply