Пример #1
0
        protected override void RunPhase1()
        {
            base.RunPhase1();

            utils.MeshManager.Initialize(mDevice);

            SamplerStateDescription desc = new SamplerStateDescription()
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                MipLodBias         = 0,
                MaximumAnisotropy  = 1,
                ComparisonFunction = Comparison.Always,
                BorderColor        = new Color4(0, 0, 0, 0),
                MinimumLod         = 0,
                MaximumLod         = float.MaxValue
            };

            mSampler = new SamplerState(mDevice, desc);

            string vsData = System.IO.File.ReadAllText("shaders\\example06.vs");
            string psData = System.IO.File.ReadAllText("shaders\\example06.ps");

            mShader.Load(mDevice, vsData, psData);
            mShader.Compile(mDevice);

            mLayout = new InputLayout(mDevice, mShader.VertexShaderSignature, new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 32, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0)
            });

            mDeviceContext.InputAssembler.InputLayout = mLayout;

            mMesh = utils.MeshManager.LoadMesh(@"meshes/teapot.fbx");

            mCamera = new CameraBase(ScreenWidth, ScreenHeight);
            mClock  = new Stopwatch();
            mClock.Start();
        }
Пример #2
0
        public static RenderMesh LoadMesh(string filename)
        {
            if (mMeshList.ContainsKey(filename))
            {
                return(mMeshList[filename]);
            }

            Scene scene = mAssetContent.ImportFile(GetPathToFile(filename));

            RenderMesh result         = new RenderMesh();
            Texture    diffuseTexture = null;

            if (scene.HasMaterials)
            {
                foreach (var material in scene.Materials)
                {
                    foreach (var diffueseMaterial in material.GetMaterialTextures(TextureType.Diffuse))
                    {
                        string textureFilePath = GetPathToFile(diffueseMaterial.FilePath);
                        if (!mTextureList.ContainsKey(textureFilePath))
                        {
                            diffuseTexture = new Texture();
                            diffuseTexture.Initialize(mDevice, GetPathToFile(diffueseMaterial.FilePath));

                            mTextureList.Add(GetPathToFile(diffueseMaterial.FilePath), diffuseTexture);
                        }
                    }
                }
            }

            if (scene.HasMeshes)
            {
                foreach (MeshData mesh in scene.Meshes)
                {
                    int primitiveCount = mesh.FaceCount * 3;
                    utils.RenderMesh.MeshFormat[] renderMeshData = new utils.RenderMesh.MeshFormat[primitiveCount];
                    int index = 0;
                    foreach (var face in mesh.Faces)
                    {
                        // ensure the data is triangulated.
                        if (face.IndexCount != 3)
                        {
                            System.Console.WriteLine("Unable to process data with faces having > 3 vertices");
                            continue;
                        }

                        // Grab verts, colors, normals and UVs
                        // For now, assume that the color channel isn't populated
                        int currentIndex = face.Indices[0];
                        renderMeshData[index].position.X = mesh.Vertices[currentIndex].X;
                        renderMeshData[index].position.Y = mesh.Vertices[currentIndex].Y;
                        renderMeshData[index].position.Z = mesh.Vertices[currentIndex].Z;
                        renderMeshData[index].position.W = 1.0f;
                        renderMeshData[index].normal.X   = mesh.Normals[currentIndex].X;
                        renderMeshData[index].normal.Y   = mesh.Normals[currentIndex].Y;
                        renderMeshData[index].normal.Z   = mesh.Normals[currentIndex].Z;
                        renderMeshData[index].normal.W   = 1.0f;
                        renderMeshData[index].color      = new SharpDX.Vector4(1.0f); // Not supporting Vertex Color channels
                        renderMeshData[index].UV.X       = mesh.TextureCoordinateChannels[0][currentIndex].X;
                        renderMeshData[index].UV.Y       = 1.0f - mesh.TextureCoordinateChannels[0][currentIndex].Y;
                        index++;

                        currentIndex = face.Indices[1];
                        renderMeshData[index].position.X = mesh.Vertices[currentIndex].X;
                        renderMeshData[index].position.Y = mesh.Vertices[currentIndex].Y;
                        renderMeshData[index].position.Z = mesh.Vertices[currentIndex].Z;
                        renderMeshData[index].position.W = 1.0f;
                        renderMeshData[index].normal.X   = mesh.Normals[currentIndex].X;
                        renderMeshData[index].normal.Y   = mesh.Normals[currentIndex].Y;
                        renderMeshData[index].normal.Z   = mesh.Normals[currentIndex].Z;
                        renderMeshData[index].normal.W   = 1.0f;
                        renderMeshData[index].color      = new SharpDX.Vector4(1.0f);
                        renderMeshData[index].UV.X       = mesh.TextureCoordinateChannels[0][currentIndex].X;
                        renderMeshData[index].UV.Y       = 1.0f - mesh.TextureCoordinateChannels[0][currentIndex].Y;
                        index++;

                        currentIndex = face.Indices[2];
                        renderMeshData[index].position.X = mesh.Vertices[currentIndex].X;
                        renderMeshData[index].position.Y = mesh.Vertices[currentIndex].Y;
                        renderMeshData[index].position.Z = mesh.Vertices[currentIndex].Z;
                        renderMeshData[index].position.W = 1.0f;
                        renderMeshData[index].normal.X   = mesh.Normals[currentIndex].X;
                        renderMeshData[index].normal.Y   = mesh.Normals[currentIndex].Y;
                        renderMeshData[index].normal.Z   = mesh.Normals[currentIndex].Z;
                        renderMeshData[index].normal.W   = 1.0f;
                        renderMeshData[index].color      = new SharpDX.Vector4(1.0f);
                        renderMeshData[index].UV.X       = mesh.TextureCoordinateChannels[0][currentIndex].X;
                        renderMeshData[index].UV.Y       = 1.0f - mesh.TextureCoordinateChannels[0][currentIndex].Y;
                        index++;
                    }
                    result.AddMesh(mDevice, renderMeshData, primitiveCount);
                }
            }

            if (scene.HasMaterials)
            {
                RenderMaterial material      = new RenderMaterial();
                Material       sceneMaterial = scene.Materials[0];
                material.Ambient    = new Vector4(sceneMaterial.ColorAmbient.R, sceneMaterial.ColorAmbient.G, sceneMaterial.ColorAmbient.B, sceneMaterial.ColorAmbient.A);
                material.Diffuse    = new Vector4(sceneMaterial.ColorDiffuse.R, sceneMaterial.ColorDiffuse.G, sceneMaterial.ColorDiffuse.B, sceneMaterial.ColorDiffuse.A);
                material.DiffuseMap = diffuseTexture;

                result.Material = material;
            }

            return(result);
        }