示例#1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            context3D = new Context3D();

            System.Drawing.Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            RenderTargetAdapter renderBufferAdapter = new RenderTargetAdapter(bitmap, pictureBox1);

            context3D.configureBackBuffer(pictureBox1.Width, pictureBox1.Height, renderBufferAdapter);

            pictureBox1.Image = bitmap;

            var triangles = new uint[] {
                2, 1, 0,                 //front face
                3, 2, 0,
                4, 7, 5,                 //bottom face
                7, 6, 5,
                8, 11, 9,                //back face
                9, 11, 10,
                12, 15, 13,              //top face
                13, 15, 14,
                16, 19, 17,              //left face
                17, 19, 18,
                20, 23, 21,              //right face
                21, 23, 22
            };

            indexList = context3D.createIndexBuffer(triangles.Length);
            indexList.uploadFromVector(triangles);

            var vertexData = new Vertex[] {
                // x,y,z r,g,b format
                //               0,0,0, 1,0,0, //front face
                //               0,1,0, 1,0,0,
                //1,1,0, 1,0,0,
                //1,0,0, 1,0,0,

                //0,0,0, 0,1,0, //bottom face
                //               1,0,0, 0,1,0,
                //1,0,1, 0,1,0,
                //0,0,1, 0,1,0,

                //0,0,1, 1,0,0, //back face
                //               1,0,1, 1,0,0,
                //1,1,1, 1,0,0,
                //0,1,1, 1,0,0,

                //0,1,1, 0,1,0, //top face
                //               1,1,1, 0,1,0,
                //1,1,0, 0,1,0,
                //0,1,0, 0,1,0,

                //0,1,1, 0,0,1, //left face
                //               0,1,0, 0,0,1,
                //0,0,0, 0,0,1,
                //0,0,1, 0,0,1,

                //1,1,0, 0,0,1, //right face
                //               1,1,1, 0,0,1,
                //1,0,1, 0,0,1,
                //1,0,0, 0,0,1



                new Vertex()
                {
                    vertex = new float3(0, 0, 0), color = new float3(1, 0, 0)
                },                                                                                 //front face
                new Vertex()
                {
                    vertex = new float3(0, 1, 0), color = new float3(1, 0, 0)
                },
                new Vertex()
                {
                    vertex = new float3(1, 1, 0), color = new float3(1, 0, 0)
                },
                new Vertex()
                {
                    vertex = new float3(1, 0, 0), color = new float3(1, 0, 0)
                },

                new Vertex()
                {
                    vertex = new float3(0, 0, 0), color = new float3(0, 1, 0)
                },                                                                                //bottom face
                new Vertex()
                {
                    vertex = new float3(1, 0, 0), color = new float3(0, 1, 0)
                },
                new Vertex()
                {
                    vertex = new float3(1, 0, 1), color = new float3(0, 1, 0)
                },
                new Vertex()
                {
                    vertex = new float3(0, 0, 1), color = new float3(0, 1, 0)
                },

                new Vertex()
                {
                    vertex = new float3(0, 0, 1), color = new float3(1, 0, 0)
                },                                                                                //back face
                new Vertex()
                {
                    vertex = new float3(1, 0, 1), color = new float3(1, 0, 0)
                },
                new Vertex()
                {
                    vertex = new float3(1, 1, 1), color = new float3(1, 0, 0)
                },
                new Vertex()
                {
                    vertex = new float3(0, 1, 1), color = new float3(1, 0, 0)
                },

                new Vertex()
                {
                    vertex = new float3(0, 1, 1), color = new float3(0, 1, 0)
                },                                                                                //top face
                new Vertex()
                {
                    vertex = new float3(1, 1, 1), color = new float3(0, 1, 0)
                },
                new Vertex()
                {
                    vertex = new float3(1, 1, 0), color = new float3(0, 1, 0)
                },
                new Vertex()
                {
                    vertex = new float3(0, 1, 0), color = new float3(0, 1, 0)
                },

                new Vertex()
                {
                    vertex = new float3(0, 1, 1), color = new float3(0, 0, 1)
                },                                                                                //left face
                new Vertex()
                {
                    vertex = new float3(0, 1, 0), color = new float3(0, 0, 1)
                },
                new Vertex()
                {
                    vertex = new float3(0, 0, 0), color = new float3(0, 0, 1)
                },
                new Vertex()
                {
                    vertex = new float3(0, 0, 1), color = new float3(0, 0, 1)
                },

                new Vertex()
                {
                    vertex = new float3(1, 1, 0), color = new float3(0, 0, 1)
                },                                                                                //right face
                new Vertex()
                {
                    vertex = new float3(1, 1, 1), color = new float3(0, 0, 1)
                },
                new Vertex()
                {
                    vertex = new float3(1, 0, 1), color = new float3(0, 0, 1)
                },
                new Vertex()
                {
                    vertex = new float3(1, 0, 0), color = new float3(0, 0, 1)
                },
            };

            vertexes = context3D.createVertexBuffer(24);
            vertexes.uploadFromVector(vertexData);


            var program3d = context3D.createProgram();

            program3d.upload(new programs.test1.VShader(), new programs.test1.FShader());
            context3D.setProgram(program3d);
        }
示例#2
0
 public Texture(Context3D context, int width, int height, string format,
                bool optimizeForRenderToTexture, int streamingLevels)
 {
     throw new NotImplementedException();
 }
示例#3
0
        //
        // Methods
        //

        public RectangleTexture(Context3D context)
            : base(context, TextureTarget.Texture2D)
        {
        }
        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...");
        }
示例#5
0
        private void Form1_Load(object sender, EventArgs e)
        {
            context3D = new Context3D();

            System.Drawing.Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            RenderTargetAdapter renderBufferAdapter = new RenderTargetAdapter(bitmap, pictureBox1);

            context3D.configureBackBuffer(pictureBox1.Width, pictureBox1.Height, renderBufferAdapter);
            pictureBox1.BackgroundImage = bitmap;


            System.Drawing.Bitmap debuglayer      = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            RenderTargetAdapter   debuggerAdapter = new RenderTargetAdapter(debuglayer, pictureBox1);

            pictureBox1.Image           = debuglayer;
            context3D.debugLayerAdapter = debuggerAdapter;


            Assimp.AssimpContext importer = new Assimp.AssimpContext();

            scene = importer.ImportFile("../../../models/cube.fbx",
                                        Assimp.PostProcessSteps.MakeLeftHanded
                                        | Assimp.PostProcessSteps.Triangulate
                                        //| Assimp.PostProcessSteps.GenerateSmoothNormals
                                        | Assimp.PostProcessSteps.CalculateTangentSpace
                                        //| Assimp.PostProcessSteps.PreTransformVertices
                                        );

            lst_indexList = new List <IndexBuffer3D>();
            lst_vertexes  = new List <VertexBuffer3D>();


            //var texture = context3D.createTexture(474, 474);
            //texture.uploadFromByteArray(SceneUtils.LoadBitmapData("../../../models/texs/th.jpg"), 0);
            var texture = MiniRender.textures.Texture.white;

            //SceneUtils.MakeAndUploadTexture(context3D, "../../../models/texs/duckCM.bmp");
            texture.AutoGenMipMap();
            context3D.setTextureAt(0, texture);
            context3D.setSamplerStateAt(0, Context3DWrapMode.REPEAT, Context3DTextureFilter.LINEAR, Context3DMipFilter.MIPLINEAR);

            //设置matcap
            var matcap = SceneUtils.MakeAndUploadTexture(context3D, "../../../models/texs/MaCrea_3.png");

            matcap.AutoGenMipMap();
            context3D.setTextureAt(1, matcap);
            context3D.setSamplerStateAt(1, Context3DWrapMode.CLAMP, Context3DTextureFilter.LINEAR, Context3DMipFilter.MIPLINEAR);

            //设置法线
            var normalmap = MiniRender.textures.Texture.planeNormal;

            //SceneUtils.MakeAndUploadTexture(context3D, "../../../models/texs/Robot_Normal.png");
            normalmap.AutoGenMipMap();
            context3D.setTextureAt(2, normalmap);
            context3D.setSamplerStateAt(2, Context3DWrapMode.REPEAT, Context3DTextureFilter.LINEAR, Context3DMipFilter.MIPNEAREST);

            //设置粗糙度与金属性贴图
            var metallic =
                SceneUtils.MakeAndUploadTexture(context3D, "../../../models/texs/jian_2_m.png");

            metallic.AutoGenMipMap();
            context3D.setTextureAt(3, metallic);
            context3D.setSamplerStateAt(3, Context3DWrapMode.REPEAT, Context3DTextureFilter.LINEAR, Context3DMipFilter.MIPNEAREST);

            for (int k = 0; k < scene.MeshCount; k++)
            {
                var mesh    = scene.Meshes[k];
                var vs      = mesh.Vertices;
                var indices = mesh.GetUnsignedIndices();

                var normals  = mesh.Normals;
                var tangents = mesh.Tangents;
                var coords   = mesh.TextureCoordinateChannels[0];

                var indexList = context3D.createIndexBuffer(indices.Length);
                indexList.uploadFromVector(indices);

                lst_indexList.Add(indexList);

                List <Vertex> vertices = new List <Vertex>();
                for (int i = 0; i < vs.Count; i++)
                {
                    vertices.Add(
                        new Vertex()
                    {
                        vertex = new float3(vs[i].X, vs[i].Y, vs[i].Z) * 3
                    }
                        );
                }

                if (mesh.HasNormals)
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].normal = (new float3(normals[i].X, normals[i].Y, normals[i].Z));
                    }
                }

                if (mesh.HasTangentBasis)
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].tangent = new float3(tangents[i].X, tangents[i].Y, tangents[i].Z);
                    }
                }

                if (mesh.HasTextureCoords(0))
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].uv = new float3(coords[i].X, coords[i].Y, coords[i].Z);
                    }
                }

                if (mesh.HasVertexColors(0))
                {
                    var color = mesh.VertexColorChannels[0];
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].color = new float4(color[i].R, color[i].G, color[i].B, color[i].A);
                    }
                }



                var vertexes = context3D.createVertexBuffer(vertices.Count);
                vertexes.uploadFromVector(vertices.ToArray());

                lst_vertexes.Add(vertexes);
            }



            var program3d = context3D.createProgram();

            fShader = new programs.test3.FShader_Metallic();
            program3d.upload(new programs.test4.VShader_Bump(),
                             new programs.test4.FShader_Bump()
                             //fShader
                             );

            context3D.setProgram(program3d);
        }
        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...");
        }
示例#7
0
        private void FrmGenNormal_Load(object sender, EventArgs e)
        {
            context3D = new Context3D();

            System.Drawing.Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            RenderTargetAdapter renderBufferAdapter = new RenderTargetAdapter(bitmap, pictureBox1);

            context3D.configureBackBuffer(pictureBox1.Width, pictureBox1.Height, renderBufferAdapter);
            pictureBox1.BackgroundImage = bitmap;


            System.Drawing.Bitmap debuglayer      = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            RenderTargetAdapter   debuggerAdapter = new RenderTargetAdapter(debuglayer, pictureBox1);

            pictureBox1.Image           = debuglayer;
            context3D.debugLayerAdapter = debuggerAdapter;


            Assimp.AssimpContext importer = new Assimp.AssimpContext();
            Assimp.Scene         scene
                = importer.ImportFile("../../../models/Quad.fbx",
                                      Assimp.PostProcessSteps.MakeLeftHanded
                                      |
                                      Assimp.PostProcessSteps.CalculateTangentSpace
                                      );

            lst_indexList = new List <IndexBuffer3D>();
            lst_vertexes  = new List <VertexBuffer3D>();

            texsize = new float2(1.0f / 2, 1.0f / 2);

            var texture = MiniRender.textures.Texture.white;

            texture.AutoGenMipMap();
            context3D.setTextureAt(0, texture);
            context3D.setSamplerStateAt(0, Context3DWrapMode.REPEAT, Context3DTextureFilter.LINEAR, Context3DMipFilter.MIPNONE);

            for (int k = 0; k < scene.MeshCount; k++)
            {
                var mesh    = scene.Meshes[k];
                var vs      = mesh.Vertices;
                var indices = mesh.GetUnsignedIndices();



                var normals  = mesh.Normals;
                var tangents = mesh.Tangents;
                var coords   = mesh.TextureCoordinateChannels[0];

                var indexList = context3D.createIndexBuffer(indices.Length);
                indexList.uploadFromVector(indices);

                lst_indexList.Add(indexList);

                List <Vertex> vertices = new List <Vertex>();
                for (int i = 0; i < vs.Count; i++)
                {
                    vertices.Add(
                        new Vertex()
                    {
                        vertex = new float3(vs[i].X, vs[i].Y, vs[i].Z)
                    }
                        );
                }

                if (mesh.HasNormals)
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].normal = (new float3(normals[i].X, normals[i].Y, normals[i].Z));
                    }
                }

                if (mesh.HasTangentBasis)
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].tangent = new float3(tangents[i].X, tangents[i].Y, tangents[i].Z);
                    }
                }

                if (mesh.HasTextureCoords(0))
                {
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].uv = new float3(coords[i].X, coords[i].Y, coords[i].Z);
                    }
                }

                if (mesh.HasVertexColors(0))
                {
                    var color = mesh.VertexColorChannels[0];
                    for (int i = 0; i < vs.Count; i++)
                    {
                        vertices[i].color = new float4(color[i].R, color[i].G, color[i].B, color[i].A);
                    }
                }
                var vertexes = context3D.createVertexBuffer(vertices.Count);
                vertexes.uploadFromVector(vertices.ToArray());

                lst_vertexes.Add(vertexes);
            }



            var program3d = context3D.createProgram();

            program3d.upload(new VShader(),
                             new FShader()
                             );

            context3D.setProgram(program3d);

            //refreshCtl();

            //render();
        }