예제 #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.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);
        }