コード例 #1
0
        public ApplicationSprite()
        {
            // Set the default stage behavior
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            // Request a 3D context instance
            stage3D = stage.stage3Ds[0];


            //stage3D.addEventListener(Event.CONTEXT3D_CREATE, contextReady, false, 0, true);
            stage3D.context3DCreate +=
                e =>
                {
                    //stage3D.removeEventListener(Event.CONTEXT3D_CREATE, contextReady);
                    //trace("Got context!");

                    // Get the new context
                    context = stage3D.context3D;

                    // Configure back buffer
                    context.configureBackBuffer(CONTEXT_WIDTH, CONTEXT_HEIGHT, 2, true);
                    stage3D.x = stage3D.y = 0;

                    // Prepare vertex data
                    Vector<double> vertexData = new[]{
				            -0.5, -0.5,	0,		1.0, 0.0, 0.0,	//<- 1st vertex x,y,z, r,g,b
				            -0.5, 0.5,	0,		0.0, 1.0, 0.0,	//<- 2nd vertex x,y,z, r,g,b
				            0.5,  0.0,	0,		0.0, 0.0, 1.0	//<- 3rd vertex x,y,z, r,g,b
                        };

                    // Connect the vertices into a triangle (in counter-clockwise order)
                    Vector<uint> indexData = new uint[] { 0, 1, 2 };

                    // Prepare a shader for rendering
                    shader = new BasicRender();
                    shader.upload(context);
                    shader.setGeometry(vertexData, indexData);

                    // ...and start rendering frames!
                    //addEventListener(Event.ENTER_FRAME, renderFrame, false, 0, true);

                    var sw = new Stopwatch();
                    sw.Start();

                    Func<Matrix3D> makeViewMatrix = delegate
                    {
                        var aspect = CONTEXT_WIDTH / CONTEXT_HEIGHT;
                        var zNear = 0.01;
                        var zFar = 1000;
                        var fov = 45 * DEGS_TO_RADIANS;

                        var view = new PerspectiveMatrix3D();
                        view.perspectiveFieldOfViewLH(fov, aspect, zNear, zFar);

                        var m = new Matrix3D();

                        m.appendRotation(sw.ElapsedMilliseconds / 30, Vector3D.Z_AXIS);
                        m.appendTranslation(0, 0, 2);
                        m.append(view);

                        return m;
                    };



                    this.enterFrame +=
                        delegate
                        {
                            // Clear away the old frame render
                            context.clear();

                            // Calculate the view matrix, and run the shader program!
                            shader.render(makeViewMatrix());

                            // Show the newly rendered frame on screen
                            context.present();
                        };
                };

            stage3D.requestContext3D(Context3DRenderMode.AUTO);

            //trace("Awaiting context...");
        }
コード例 #2
0
        public ApplicationSprite()
        {
            // Set the default stage behavior
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            // Request a 3D context instance
            stage3D = stage.stage3Ds[0];
            //stage3D.addEventListener(Event.CONTEXT3D_CREATE, contextReady, false, 0, true);

            stage3D.context3DCreate +=
                delegate
                {
                    //stage3D.removeEventListener(Event.CONTEXT3D_CREATE, contextReady);

                    // Get the new context
                    context = stage3D.context3D;
                    //trace("Got context!  " + context);

                    // Configure back buffer
                    context.configureBackBuffer(DefaultWidth, DefaultHeight, 2, true);
                    context.setCulling(Context3DTriangleFace.BACK);
                    stage3D.x = stage3D.y = 0;

                    // Prepare vertex data:  x,y,z, nx,ny,nz, u,v  (position, normal, texture)
                    Vector<double> vertexData = new double[] {
				         0.5,  0.5, -0.5,	0,0,-1,		1,0,	// 	Front
				        -0.5,  0.5, -0.5,	0,0,-1,		0,0,	// 
				        -0.5, -0.5, -0.5,	0,0,-1,		0,1,	// 
				         0.5, -0.5, -0.5,	0,0,-1,		1,1,	// 
				                           
				         0.5, -0.5, -0.5,	0,-1,0,		1,0,	//  Bottom
				        -0.5, -0.5, -0.5,	0,-1,0,		0,0,	// 
				        -0.5, -0.5,  0.5,	0,-1,0,		0,1,	// 
				         0.5, -0.5,  0.5,	0,-1,0,		1,1,	// 
				                           
				        -0.5,  0.5,  0.5,	0,0,1, 		1,0,	// 	Back
				         0.5,  0.5,  0.5,	0,0,1,		0,0,	// 
				         0.5, -0.5,  0.5,	0,0,1,		0,1,	// 
				        -0.5, -0.5,  0.5,	0,0,1,		1,1,	// 
				                           
				        -0.5,  0.5,  0.5,	0,1,0, 		1,0,	// 	Top
				         0.5,  0.5,  0.5,	0,1,0,		0,0,	// 
				         0.5,  0.5, -0.5,	0,1,0,		0,1,	// 
				        -0.5,  0.5, -0.5,	0,1,0,		1,1,	// 
				                           
				        -0.5,  0.5, -0.5,	-1,0,0,		1,0,	// 	Left
				        -0.5,  0.5,  0.5,	-1,0,0,		0,0,	// 
				        -0.5, -0.5,  0.5,	-1,0,0,		0,1,	// 
				        -0.5, -0.5, -0.5,	-1,0,0,		1,1,	// 
				                           
				         0.5,  0.5,  0.5,	1,0,0, 		1,0,	// 	Right
				         0.5,  0.5, -0.5,	1,0,0,		0,0,	// 
				         0.5, -0.5, -0.5,	1,0,0,		0,1,	// 
				         0.5, -0.5,  0.5,	1,0,0,		1,1		// 	  	
			        };


                    Vector<uint> indexData = new uint[] {
				        0, 1, 2,		0, 2, 3,		// Front face
				        4, 5, 6,		4, 6, 7,        // Bottom face
				        8, 9, 10,		8, 10, 11,      // Back face
				        14, 13, 12,		15, 14, 12,     // Top face
				        16, 17, 18,		16, 18, 19,     // Left face
				        20, 21, 22,		20, 22, 23      // Right face
			        };

                    // Prep the bitmap data to be used as a texture
                    var texture = new ActionScript.Images.box().bitmapData;

                    // Prepare a shader for rendering
                    shader = new LightedRender();
                    shader.upload(context);
                    shader.setGeometry(vertexData, indexData, texture);

                    // The projection defines a 3D perspective to be rendered
                    projection = new PerspectiveMatrix3D();
                    projection.perspectiveFieldOfViewRH(45, (double)DefaultWidth / (double)DefaultHeight, 1, 500);

                    // The pivot will keep track of the model's current rotation
                    pivot = new Vector3D();

                    // Prepare a matrix which we'll use to apply transformations to the model
                    modelMatrix = new Matrix3D();
                    modelMatrix.identity();
                    modelMatrix.appendRotation(45, Vector3D.X_AXIS, pivot);
                    modelMatrix.appendRotation(45, Vector3D.Y_AXIS, pivot);
                    modelMatrix.appendRotation(45, Vector3D.Z_AXIS, pivot);

                    // The view matrix will contain the concatenation of all transformations
                    viewMatrix = new Matrix3D();

                    // Prepare lighting
                    lightColor = new Vector3D(0.95, 0.80, 0.55, 0.8);  // R,G,B,strength
                    ambient = new Vector3D(0.00, 0.05, 0.1);
                    lightPos = new Vector3D(1.0, 1.0, -4.0, 0.2);

                    // Start rendering frames
                    //addEventListener(Event.ENTER_FRAME, renderFrame, false, 0, true);

                    this.enterFrame +=
                        delegate
                        {
                            // Clear away the old frame render
                            context.clear(0.05, 0.12, 0.18);  // Dark grey background

                            // Rotate the model matrix
                            modelMatrix.appendRotation(0.4, Vector3D.X_AXIS, pivot);
                            modelMatrix.appendRotation(0.3, Vector3D.Y_AXIS, pivot);

                            // Calculate the view matrix, and run the shader program!
                            viewMatrix.identity();
                            viewMatrix.append(modelMatrix);
                            viewMatrix.appendTranslation(0, 0, -2);
                            viewMatrix.append(projection);
                            viewMatrix.transpose();

                            shader.render(viewMatrix, lightPos, lightColor, ambient);

                            // Show the newly rendered frame on screen
                            context.present();
                        };

                    // Created with EasyAGAL!
                    //var bitmap:Bitmap = new createdWith();
                    //bitmap.y = CONTEXT_HEIGHT - bitmap.height;
                    //addChild(bitmap);

                };

            stage3D.requestContext3D(Context3DRenderMode.AUTO);

            //trace("Awaiting context...");
        }
コード例 #3
0
		/// <summary>
		/// Appends the matrix by multiplying another Matrix3D object by the current Matrix3D object.
		/// </summary>
		public void append(Matrix3D lhs)
		{
		}
コード例 #4
0
		/// <summary>
		/// Prepends a matrix by multiplying the current Matrix3D object by another Matrix3D object.
		/// </summary>
		public void prepend(Matrix3D rhs)
		{
		}
コード例 #5
0
		/// <summary>
		/// Interpolates the display object's matrix a percent closer to a target's matrix.
		/// </summary>
		public void interpolateTo(Matrix3D toMat, double percent)
		{
		}
コード例 #6
0
		/// <summary>
		/// [static] Simplifies the interpolation from one frame of reference to another by interpolating a display object a percent point closer to a target display object.
		/// </summary>
		public static Matrix3D interpolate(Matrix3D thisMat, Matrix3D toMat, double percent)
		{
			return default(Matrix3D);
		}
コード例 #7
0
        public void render(Matrix3D viewMatrix, Vector3D lightPos, Vector3D lightColor, Vector3D ambient)
        {
            // Set ATTRIBUTE Registers to point at vertex data
            context.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3); // x,y,z
            context.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3); // nx,ny,nz
            context.setVertexBufferAt(2, vertexBuffer, 6, Context3DVertexBufferFormat.FLOAT_2); // u,v

            // Set SAMPLER Register
            context.setTextureAt(0, texture3D);

            // Pass viewMatrix into constant registers
            context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, viewMatrix);

            // Pass a vector for the (world space) location of the light
            context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 4, vec3DtoVec(lightPos), 1);

            // Pass light parameters
            context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, vec3DtoVec(lightColor), 1);
            context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 1, vec3DtoVec(ambient), 1);
            context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 2, new double[] { 0, 1 / 3, 1, 0.5 }, 1);

            // Tell the 3D context that this is the current shader program to be rendered
            context.setProgram(program);

            // Render the shader!
            context.drawTriangles(indexBuffer);
        }