Пример #1
0
        public RModel GetRenderModel(RSkeleton skeleton = null)
        {
            var model = new RModel
            {
                BoundingSphere = new Vector4(mesh.BoundingSphereX, mesh.BoundingSphereY, mesh.BoundingSphereZ, mesh.BoundingSphereRadius)
            };

            foreach (MeshObject meshObject in mesh.Objects)
            {
                //PrintAttributeInformation(meshObject);

                var rMesh = new RMesh
                {
                    Name           = meshObject.Name,
                    SingleBindName = meshObject.ParentBoneName,
                    BoundingSphere = new Vector4(meshObject.BoundingSphereX, meshObject.BoundingSphereY,
                                                 meshObject.BoundingSphereZ, meshObject.BoundingSphereRadius),
                    RenderMesh = CreateRenderMesh(skeleton, meshObject),
                };

                model.SubMeshes.Add(rMesh);
            }

            return(model);
        }
Пример #2
0
        public RModel GetRenderModel(RSkeleton Skeleton = null)
        {
            RModel model = new RModel();

            // The bounding sphere containing all meshes.
            var modelSphere = mesh.GetBoundingSphere();

            model.BoundingSphere = new Vector4(modelSphere.Item1, modelSphere.Item2, modelSphere.Item3, modelSphere.Item4);

            foreach (MeshObject meshObject in mesh.Objects)
            {
                PrintAttributeInformation(meshObject);

                RMesh rMesh = new RMesh
                {
                    Name           = meshObject.Name,
                    SingleBindName = meshObject.ParentBoneName,
                };

                // Get bounding sphere.
                var sphere = meshObject.GetBoundingSphere();
                rMesh.BoundingSphere = new Vector4(sphere.Item1, sphere.Item2, sphere.Item3, sphere.Item4);

                // Get vertex data.
                var vertexAccessor           = new SSBHVertexAccessor(mesh);
                var vertexIndices            = vertexAccessor.ReadIndices(0, meshObject.IndexCount, meshObject);
                List <CustomVertex> vertices = CreateVertices(Skeleton, meshObject, vertexAccessor, vertexIndices);

                rMesh.RenderMesh = new RenderMesh(vertices, new List <uint>(vertexIndices));

                model.subMeshes.Add(rMesh);
            }

            return(model);
        }
Пример #3
0
        public static RModel GetRenderModel(Mesh mesh, RSkeleton skeleton)
        {
            var model = new RModel
            {
                BoundingSphere = new Vector4(mesh.BoundingSphereCenter.ToOpenTK(), mesh.BoundingSphereRadius)
            };

            // Use a shared buffer to improve performance.
            // The render meshes will keep references to these objects.
            var vertexBuffer0 = new BufferObject(BufferTarget.ArrayBuffer);
            vertexBuffer0.SetData(mesh.VertexBuffers[0].Buffer, BufferUsageHint.StaticDraw);

            var vertexBuffer1 = new BufferObject(BufferTarget.ArrayBuffer);
            vertexBuffer1.SetData(mesh.VertexBuffers[1].Buffer, BufferUsageHint.StaticDraw);

            foreach (MeshObject meshObject in mesh.Objects)
            {
                var singleBindIndex = skeleton.GetBoneIndex(meshObject.ParentBoneName);

                var rMesh = new RMesh(meshObject.Name,
                    meshObject.SubIndex,
                    meshObject.ParentBoneName,
                    singleBindIndex,
                    new Vector4(meshObject.BoundingSphereCenter.ToOpenTK(),
                    meshObject.BoundingSphereRadius),
                    CreateRenderMesh(mesh, skeleton, meshObject, vertexBuffer0, vertexBuffer1),
                    // TODO: The actual in game check is more complicated, and involves checking names, subindex, and usage.
                    meshObject.Attributes.Select(m => m.AttributeStrings[0].Text).ToList()
                );

                model.SubMeshes.Add(rMesh);
            }

            return model;
        }
Пример #4
0
        public RModel GetRenderModel(RSkeleton skeleton = null)
        {
            var model = new RModel
            {
                BoundingSphere = new Vector4(mesh.BoundingSphereX, mesh.BoundingSphereY, mesh.BoundingSphereZ, mesh.BoundingSphereRadius)
            };

            foreach (MeshObject meshObject in mesh.Objects)
            {
                PrintAttributeInformation(meshObject);

                var rMesh = new RMesh
                {
                    Name           = meshObject.Name,
                    SingleBindName = meshObject.ParentBoneName,
                };

                // Get bounding sphere.
                rMesh.BoundingSphere = new Vector4(meshObject.BoundingSphereX, meshObject.BoundingSphereY, meshObject.BoundingSphereZ, meshObject.BoundingSphereRadius);

                // Get vertex data.
                rMesh.RenderMesh = GetRenderMesh(skeleton, meshObject, rMesh);

                model.subMeshes.Add(rMesh);
            }

            return(model);
        }
Пример #5
0
        public RModel GetRenderModel(RSkeleton skeleton = null)
        {
            var model = new RModel
            {
                BoundingSphere = new Vector4(mesh.BoundingSphereCenter.ToOpenTK(), mesh.BoundingSphereRadius)
            };

            // Use a shared buffer to improve performance.
            // The render meshes will keep references to these objects.
            var vertexBuffer0 = new BufferObject(BufferTarget.ArrayBuffer);

            vertexBuffer0.SetData(mesh.VertexBuffers[0].Buffer, BufferUsageHint.StaticDraw);

            var vertexBuffer1 = new BufferObject(BufferTarget.ArrayBuffer);

            vertexBuffer1.SetData(mesh.VertexBuffers[1].Buffer, BufferUsageHint.StaticDraw);

            foreach (MeshObject meshObject in mesh.Objects)
            {
                var rMesh = new RMesh
                {
                    Name           = meshObject.Name,
                    SubIndex       = meshObject.SubIndex,
                    SingleBindName = meshObject.ParentBoneName,
                    BoundingSphere = new Vector4(meshObject.BoundingSphereCenter.X, meshObject.BoundingSphereCenter.Y,
                                                 meshObject.BoundingSphereCenter.Z, meshObject.BoundingSphereRadius),
                    RenderMesh = CreateRenderMesh(skeleton, meshObject, vertexBuffer0, vertexBuffer1),
                };

                model.SubMeshes.Add(rMesh);
            }

            return(model);
        }
Пример #6
0
        public RMesh CreateMesh(string name, RSceneNode parent)
        {
            RMesh mesh = RMesh.Create <RMesh>();

            mesh.Name     = name;
            mesh.Children = new List <RSceneNode>();
            mesh.Parent   = parent;
            parent.Children.Add(mesh);
            return(mesh);
        }
Пример #7
0
        private RenderMesh GetRenderMesh(RSkeleton skeleton, MeshObject meshObject, RMesh rMesh)
        {
            var vertexAccessor = new SsbhVertexAccessor(mesh);
            {
                var vertexIndices = vertexAccessor.ReadIndices(0, meshObject.IndexCount, meshObject);

                System.Diagnostics.Debug.WriteLine($"Vertex Count: {vertexIndices.Length}");

                List <CustomVertex> vertices = CreateVertices(skeleton, meshObject, vertexAccessor, vertexIndices);

                /*if(obs.IndexOf(meshObject) != 0x2B && ExtendedMesh != null && ExtendedMesh.MeshToIndexBuffer.ContainsKey(obs.IndexOf(meshObject)))
                 * {
                 *  rMesh.RenderMesh = new RenderMesh(vertices, new List<uint>(ExtendedMesh.MeshToIndexBuffer[obs.IndexOf(meshObject)]), PrimitiveType.TriangleFan);
                 * }
                 * else*/
                return(new RenderMesh(vertices, new List <uint>(vertexIndices)));
            }
        }
Пример #8
0
        public override void Init()
        {
            Engine.InitGameWindow(1920, 1080, RWindowStyle.Normal);
            GameWindow.VSync = OpenTK.VSyncMode.On;
            Engine.SetShowFPS(true);
            Engine.SetViewport(new RViewport(0, 0, 1920, 1080));
            //Engine.InitHDR();

            GameWindow.CursorVisible = true;
            GameWindow.Location      = new System.Drawing.Point(0, 0);
            sponza = Engine.Scene.Create <RMesh>("sponza");
            sponza.LoadSourceModel("/models/sponza.fbx");
            sponza.CullEnable  = false;
            sponza.CullMode    = RCullMode.None;
            sponza.Scale       = Vector3.One;
            sponza.Position    = Vector3.Zero;
            sponza.BlendEnable = false;
            var cam = Engine.GetCamera();

            //cam.LookAt(cam.Position - Vector3.UnitZ);
            cam.Position = new Vector3(0, 1.5f, 0);
            cam.Near     = 0.01f;
            cam.Far      = 10000f;
            //cam.ViewDirection = Vector3.Normalize(cam.Position - Vector3.UnitZ);
            cam.Update();

            font = RScreen.Instance.LoadFont("/vcr_osd_mono.ttf", 16);

            RTexture2D posX = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posX", "/textures/sky-posX.png");
            RTexture2D posY = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posY", "/textures/sky-posY.png");
            RTexture2D posZ = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posZ", "/textures/sky-posZ.png");
            RTexture2D negX = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negX", "/textures/sky-negX.png");
            RTexture2D negY = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negY", "/textures/sky-negY.png");
            RTexture2D negZ = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negZ", "/textures/sky-negZ.png");

            var skyboxTexture = new RTexture3D();

            skyboxTexture.Create(RPixelFormat.Rgb, ref posX, ref posY, ref posZ, ref negX, ref negY, ref negZ);

            Engine.Atmosphere.CreateSkyBox(skyboxTexture);
            GameWindow.Visible = true;
        }
Пример #9
0
        public override void Init()
        {
            Engine.InitGameWindow(1280, 720, RWindowStyle.Normal);
            GameWindow.VSync = OpenTK.VSyncMode.On;
            Engine.SetShowFPS(true);
            Engine.SetViewport(new RViewport(0, 0, 1280, 720));
            Engine.InitHDR();
            GameWindow.CursorVisible = true;
            sponza = Engine.Scene.Create <RMesh>("sponza");
            sponza.LoadSourceModel("/models/sponza.fbx");
            sponza.CullEnable = false;
            sponza.CullMode   = RCullMode.None;
            sponza.SetScale(1f);
            sponza.SetPosition(0, 0, 0);
            sponza.BlendEnable = false;
            var cam = Engine.GetCamera();

            cam.SetPosition(0, 20, 1f);
            cam.LookAt(Vector3.Zero);
            cam.SetClipPlanes(0.01f, 10000f);

            font = RScreen.Instance.LoadFont("/vcr_osd_mono.ttf", 16);

            RTexture2D posX = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posX", "/textures/sky-posX.png");
            RTexture2D posY = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posY", "/textures/sky-posY.png");
            RTexture2D posZ = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("posZ", "/textures/sky-posZ.png");
            RTexture2D negX = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negX", "/textures/sky-negX.png");
            RTexture2D negY = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negY", "/textures/sky-negY.png");
            RTexture2D negZ = (RTexture2D)Engine.Textures.CreateTexture <RTexture2D>("negZ", "/textures/sky-negZ.png");

            var skyboxTexture = new RTexture3D();

            skyboxTexture.Create(RPixelFormat.Rgb, ref posX, ref posY, ref posZ, ref negX, ref negY, ref negZ);

            Engine.Atmosphere.CreateSkyBox(skyboxTexture);
        }
Пример #10
0
        public static void LoadSource(this RMesh rmesh, string filename)
        {
            AssimpContext context = new AssimpContext();

            context.SetConfig(new Assimp.Configs.FBXImportAllMaterialsConfig(true));
            context.SetConfig(new Assimp.Configs.FBXImportAllGeometryLayersConfig(true));
            context.SetConfig(new Assimp.Configs.MultithreadingConfig(2));
            context.SetConfig(new Assimp.Configs.FBXStrictModeConfig(false));
            if (!context.IsImportFormatSupported(Path.GetExtension(filename)))
            {
                throw new ReactorException("Attempted to load a model that Assimp doesn't know how to load");
            }
            LogStream log = new LogStream((msg, user) => {
                RLog.Info(msg.Remove(msg.Length - 2));
            });

            log.Attach();
            int   platform = (int)Environment.OSVersion.Platform;
            Scene scene    = context.ImportFile(filename,
                                                PostProcessSteps.FindInvalidData |
                                                PostProcessSteps.GenerateSmoothNormals |
                                                PostProcessSteps.Triangulate |
                                                PostProcessSteps.GenerateUVCoords |
                                                PostProcessSteps.CalculateTangentSpace |
                                                PostProcessSteps.PreTransformVertices);

            if (scene.HasMeshes)
            {
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (!mesh.HasVertices)
                    {
                        continue;
                    }
                    RMeshPart rmeshpart = RMeshPart.Create <RMeshPart> ();


                    RVertexData [] data = new RVertexData [mesh.VertexCount];

                    List <int> indicesList = new List <int> ();

                    if (mesh.HasFaces)
                    {
                        foreach (Face face in mesh.Faces)
                        {
                            indicesList.AddRange(face.Indices.ToArray());
                            foreach (int index in face.Indices)
                            {
                                Vector3D p = mesh.Vertices [index];
                                data [index].Position = new Vector3(p.X, p.Y, p.Z);
                                if (mesh.HasTextureCoords(0))
                                {
                                    Vector3D t = mesh.TextureCoordinateChannels [0] [index];
                                    data [index].TexCoord = new Vector2(t.X, -t.Y);
                                }

                                if (mesh.HasNormals)
                                {
                                    Vector3D n = mesh.Normals [index];
                                    data [index].Normal = new Vector3(n.X, n.Y, n.Z);
                                }

                                if (mesh.HasTangentBasis)
                                {
                                    Vector3D b = mesh.BiTangents [index];
                                    Vector3D t = mesh.Tangents [index];
                                    data [index].Bitangent = new Vector3(b.X, b.Y, b.Z);
                                    data [index].Tangent   = new Vector3(t.X, t.Y, t.Z);
                                }
                            }
                        }
                    }



                    RVertexBuffer vbuffer = new RVertexBuffer(typeof(RVertexData), mesh.VertexCount, RBufferUsage.WriteOnly, true);


                    RIndexBuffer ibuffer = new RIndexBuffer(typeof(int), indicesList.Count, RBufferUsage.WriteOnly);
                    ibuffer.SetData(indicesList.ToArray());

                    vbuffer.SetData <RVertexData> (data);

#if WINDOWS
                    var separator = "\\";
#else
                    var separator = "/";
#endif

                    rmeshpart.VertexBuffer = vbuffer;
                    rmeshpart.IndexBuffer  = ibuffer;

                    RMaterial material = new RMaterial(rmesh.Name + ":Material");

                    if (scene.HasMaterials)
                    {
                        Material mat = scene.Materials[mesh.MaterialIndex];
                        material.Shininess = mat.Shininess;
                        material.SetColor(RMaterialColor.DIFFUSE, new RColor(mat.ColorDiffuse.R, mat.ColorDiffuse.G, mat.ColorDiffuse.B, mat.ColorDiffuse.A));
                        if (mat.HasTextureDiffuse)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureDiffuse.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.Bind();
                            tex.GenerateMipmaps();
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            tex.Unbind();
                            material.SetTexture(RTextureLayer.DIFFUSE, tex);
                        }
                        if (mat.HasTextureNormal)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureNormal.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.Bind();
                            tex.GenerateMipmaps();
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            tex.Unbind();
                            material.SetTexture(RTextureLayer.NORMAL, tex);
                        }
                        if (mat.HasTextureAmbient)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureAmbient.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.AMBIENT, tex);
                        }
                        if (mat.HasTextureHeight)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureHeight.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.HEIGHT, tex);
                        }
                        if (mat.HasTextureEmissive)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureEmissive.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.GLOW, tex);
                        }
                        if (mat.HasTextureSpecular)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureSpecular.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.SPECULAR, tex);
                        }
                    }
                    rmeshpart.Material = material;
                    rmesh.Parts.Add(rmeshpart);
                }
                //return rmesh;
            }
            //return null;
            if (rmesh.Parts.Count == 0)
            {
                throw new ReactorException("Attempted to load a model when Assimp couldn't find any verticies!");
            }

            context.Dispose();
        }