private void InitializeModelData()
        {
            // Check if this model is an npc, and if so jankload the animation for it.
            if (this.renderDatums.Length == 1)// &&
            //    ArcFileCollection.Instance.ArcFiles[this.renderDatums[0].ArcIndex].FileEntries[this.renderDatums[0].FileIndex].FileName.Contains("npc") == true)
            {
                Archive          arcFile;
                ArchiveFileEntry fileEntry;

                ArchiveCollection.Instance.GetArchiveFileEntryFromDatum(this.renderDatums[0], out arcFile, out fileEntry);

                string animationFileName = fileEntry.FileName.Replace("rModel", "rMotionList").Replace("model", "motion");
                ArchiveCollection.Instance.GetArchiveFileEntryFromFileName(animationFileName, out arcFile, out fileEntry);

                if (fileEntry != null)
                {
                    this.modelAnimation = arcFile.GetFileAsResource <rMotionList>(fileEntry.FileName);
                }
            }

            // Loop through all of the datums to render and setup each one for rendering.
            this.resourcesToRender = new GameResource[this.renderDatums.Length];
            for (int i = 0; i < this.renderDatums.Length; i++)
            {
                // Create the game resource from the datum.
                this.resourcesToRender[i] = ArchiveCollection.Instance.GetFileAsResource <GameResource>(this.renderDatums[i]);
                if (this.resourcesToRender[i] == null)
                {
                    // Failed to load the required resource.
                    // TODO: Bubble this up to the user.
                    throw new NotImplementedException();
                }

                // Let the object initialize and required directx resources.
                if (this.resourcesToRender[i].InitializeGraphics(this, this.device) == false)
                {
                    // Failed to initialize graphics for resource.
                    // TODO: Bubble this up to the user.
                    throw new NotImplementedException();
                }
            }

            // HACK: For now just cast the first resource to an rModel and use it for reference.
            rModel firstModel = (rModel)this.resourcesToRender[0];

            // Position the camera and make it look at the model.
            this.camera.Position = new Vector3(firstModel.primitives[0].BoundingBoxMin.X, firstModel.primitives[0].BoundingBoxMin.Y, firstModel.primitives[0].BoundingBoxMin.Z);
            this.camera.LookAt   = (firstModel.header.BoundingBoxMax - firstModel.header.BoundingBoxMin).ToVector3();
            //this.camera.Speed = Math.Abs(firstModel.primitives[0].BoundingBoxMin.X / 1000.0f);
            this.camera.SpeedModifier = Math.Abs(firstModel.primitives[0].BoundingBoxMin.X / 100000.0f);
        }
 private static void WritePrimitiveBlock(rModel model, XmlWriter writer, int index)
 {
     // Write the geometry element.
     writer.WriteStartElement("geometry");
     writer.WriteAttributeString("id", "primitive_" + index.ToString());
     writer.WriteAttributeString("name", "Primitive " + index.ToString());
     {
         // Write the mesh element.
         writer.WriteStartElement("mesh");
         {
         }
         writer.WriteFullEndElement();
     }
     writer.WriteFullEndElement();
 }
Example #3
0
        protected override void OnGameResourceUpdated()
        {
            // Make sure the arc file and game resource are valid.
            if (this.ArcFile == null || this.GameResource == null)
            {
                // Clear the textbox contents and return.
                this.textBox1.Text = "";
                return;
            }

            // Cast the game resource to a rModel object.
            rModel model = (rModel)this.GameResource;

            // Print all the model information out to the textbox.
            string headerInfo = "*** Header\n";

            headerInfo += StructureToString(model.header) + "\n";

            string textures = "*** Textures\n";

            if (model.textureFileNames != null)
            {
                for (int i = 0; i < model.textureFileNames.Length; i++)
                {
                    textures += string.Format("\t[{0}]: {1}\n", i + 1, model.textureFileNames[i]);
                }
            }
            textures += "\n";

            string joints = "*** Joints\n";

            if (model.joints != null)
            {
                for (int i = 0; i < model.joints.Length; i++)
                {
                    string joint = "   Joint #" + i.ToString() + "\n";
                    joint  += StructureToString(model.joints[i]);
                    joints += joint + "\n";
                }
            }

            string materials = "*** Materials\n";

            for (int i = 0; i < model.materials.Length; i++)
            {
                string mat = "   Material #" + i.ToString() + "\n";
                mat       += StructureToString(model.materials[i]);
                materials += mat + "\n";
            }

            string primitives = "*** Primitives\n";

            for (int i = 0; i < model.primitives.Length; i++)
            {
                string prim = "   Primitive #" + i.ToString() + "\n";
                prim       += StructureToString(model.primitives[i]);
                primitives += prim + "\n";
            }

            // Set textbox text.
            this.textBox1.Text = headerInfo.Replace("\n", "\r\n") + textures.Replace("\n", "\r\n") + joints.Replace("\n", "\r\n") + materials.Replace("\n", "\r\n") + primitives.Replace("\n", "\r\n");
        }
        private void Render()
        {
            // If the form is not visible do not render anything.
            if (this.Visible == false)
            {
                return;
            }

            // Update the time from the previous frame to the current frame.
            this.renderTime.LastTickCount    = this.renderTime.CurrentTickCount;
            this.renderTime.CurrentTickCount = DateTime.Now.Ticks;
            this.renderTime.TimeDelta        = (float)(this.renderTime.CurrentTickCount - this.renderTime.LastTickCount) / (float)TimeSpan.TicksPerSecond;

            // Check if we need to reset the fps counter.
            if (this.renderTime.CurrentTickCount >= this.startTime + TimeSpan.TicksPerSecond)
            {
                // Update the fps counter.
                this.Text = string.Format("RenderView: {0} fps", this.framesPerSecond);

                // Reset the fps counter.
                this.framesPerSecond = 0;
                this.startTime       = this.renderTime.CurrentTickCount;
            }

            // Increment the frame counter.
            this.framesPerSecond++;

            // Check if the form has resized and if so reset our render state to accomidate the size change.
            if (this.hasResized == true)
            {
                ResizeRenderTarget();
            }

            // Cap input polling to 30 times per second.
            if ((this.renderTime.CurrentTickCount - this.lastInputPollTime) > (TimeSpan.TicksPerSecond / 30))
            {
                // Only move the camera if the window is visible.
                if (this.Visible == true && this.Focused == true)
                {
                    // Update input.
                    this.lastInputPollTime = DateTime.Now.Ticks;
                    this.inputManager.DrawFrame(this, this.device);

                    // Check if we need to update debug draw options.
                    if (this.inputManager.ButtonPressed(InputAction.MiscAction1) == true)
                    {
                        // Toggle joint bounding spheres.
                        if (this.debugDrawOptions.HasFlag(DebugDrawOptions.DrawJointBoundingSpheres) == true)
                        {
                            this.debugDrawOptions &= ~DebugDrawOptions.DrawJointBoundingSpheres;
                        }
                        else
                        {
                            this.debugDrawOptions |= DebugDrawOptions.DrawJointBoundingSpheres;
                        }
                    }
                    if (this.inputManager.ButtonPressed(InputAction.MiscAction2) == true)
                    {
                        // Toggle primitive bounding boxes.
                        if (this.debugDrawOptions.HasFlag(DebugDrawOptions.DrawPrimitiveBoundingBox) == true)
                        {
                            this.debugDrawOptions &= ~DebugDrawOptions.DrawPrimitiveBoundingBox;
                        }
                        else
                        {
                            this.debugDrawOptions |= DebugDrawOptions.DrawPrimitiveBoundingBox;
                        }
                    }

                    // Update the camera.
                    this.camera.DrawFrame(this, this.device);
                }
            }

            // Set our render target to our swapchain buffer.
            this.device.ImmediateContext.OutputMerger.SetRenderTargets(this.depthStencilView, this.renderView);

            // Clear the backbuffer.
            this.device.ImmediateContext.ClearRenderTargetView(this.renderView, SharpDX.Color.CornflowerBlue);
            this.device.ImmediateContext.ClearDepthStencilView(this.depthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

            // Set depth stencil and rasterizer states.
            this.device.ImmediateContext.OutputMerger.SetDepthStencilState(this.depthStencilState, 0);
            this.device.ImmediateContext.Rasterizer.State = this.rasterState;

            // Set the viewport.
            this.device.ImmediateContext.Rasterizer.SetViewport(0, 0, this.ClientSize.Width, this.ClientSize.Height, 0.0f, 1.0f);

            // Set output target.
            this.device.ImmediateContext.OutputMerger.SetRenderTargets(this.depthStencilView, this.renderView);

            // The pixel shader constants do not change between primtive draw calls, update them now.
            this.shaderConsts.gXfViewProj = Matrix.Transpose(this.worldGround * this.camera.ViewMatrix * this.projectionMatrix);

            // Loop through all of the resources to render and draw each one.
            for (int i = 0; i < this.resourcesToRender.Length; i++)
            {
                // HACK: We can only render rModels for now.
                rModel model = (rModel)this.resourcesToRender[i];

                // Set the bounding box parameters for this model.
                this.shaderConsts.gXfQuantPosScale  = model.header.BoundingBoxMax - model.header.BoundingBoxMin;
                this.shaderConsts.gXfQuantPosOffset = model.header.BoundingBoxMin;

                // Update the shader constants buffer with the new data.
                this.device.ImmediateContext.UpdateSubresource(ref this.shaderConsts, this.shaderConstantBuffer);

                // Set the shader constants.
                this.device.ImmediateContext.VertexShader.SetConstantBuffer(0, this.shaderConstantBuffer);
                this.device.ImmediateContext.PixelShader.SetConstantBuffer(0, this.shaderConstantBuffer);

                // Draw the model.
                this.resourcesToRender[i].DrawFrame(this, this.device);
            }

            // Present the final frame.
            this.swapChain.Present(0, PresentFlags.None);
        }
        public static bool ExportModel(rModel model, string outputFolder)
        {
            // Setup xml writer formatting settings.
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent       = true;
            settings.IndentChars  = "  ";
            settings.NewLineChars = "\r\n";
            settings.Encoding     = Encoding.UTF8;

            // Create a new xml writer for the output collada file.
            string modelName = Path.GetFileName(model.FileName).Replace(".rModel", "");

            using (XmlWriter writer = XmlWriter.Create(string.Format("{0}\\{1}.dae", outputFolder, modelName), settings))
            {
                // Write the xml header.
                writer.WriteStartDocument();

                // Write the collada element start.
                writer.WriteStartElement("COLLADA", "http://www.collada.org/2004/COLLADASchema");
                writer.WriteAttributeString("version", "1.4.1");

                // Write the asset element.
                writer.WriteStartElement("asset");
                writer.WriteStartElement("contributor");
                writer.WriteElementString("authoring_tool", "Dead Rising Arc Tool");
                writer.WriteFullEndElement();
                writer.WriteElementString("created", DateTime.Now.ToString());
                writer.WriteElementString("modified", DateTime.Now.ToString());
                writer.WriteFullEndElement();

                // Write the images library element.
                writer.WriteStartElement("library_images");
                {
                    // Loop and write and image element for every texture the model has.
                    for (int i = 0; i < model.textureFileNames.Length; i++)
                    {
                        // Get name of the texture.
                        string textureName = Path.GetFileName(model.textureFileNames[i]);

                        // Find the arc file the resource is in.
                        string textureFileName = GameResource.GetFullResourceName(model.textureFileNames[i], ResourceType.rTexture);
                        ArchiveCollection.Instance.GetArchiveFileEntryFromFileName(textureFileName, out Archive.Archive arcFile, out ArchiveFileEntry fileEntry);
                        if (arcFile == null || fileEntry == null)
                        {
                            // Failed to find a resource with the specified name.
                            return(false);
                        }

                        // Parse the game resource and cast it to rtexture.
                        rTexture texture = arcFile.GetFileAsResource <rTexture>(textureFileName);

                        // Save the texture to a dds image that can be loaded with the model.
                        DDSImage ddsImage = DDSImage.FromGameTexture(texture);
                        if (ddsImage.WriteToFile(string.Format("{0}\\{1}.dds", outputFolder, textureName)) == false)
                        {
                            // Failed to extract texture to file.
                            return(false);
                        }

                        // Write the image element.
                        writer.WriteStartElement("image");
                        writer.WriteAttributeString("name", textureName);
                        writer.WriteElementString("init_from", "./" + textureName + ".dds");
                        writer.WriteFullEndElement();
                    }
                }
                writer.WriteFullEndElement();

                // Write the materials library element.
                //writer.WriteStartElement("library_materials");
                //{
                //    // Loop through all of the materials and write each one.
                //    for (int i = 0; i < model.materials.Length; i++)
                //    {
                //        // Write the material element.
                //        writer.WriteStartElement("material");
                //        writer.WriteAttributeString("id", "Material " + i.ToString());
                //    }
                //}
                //writer.WriteFullEndElement();

                // TODO: Joints

                // Write the geometries library element.
                writer.WriteStartElement("library_geometries");
                {
                    // Loop through all the primitives and write each one.
                    for (int i = 0; i < model.primitives.Length; i++)
                    {
                        // Write the primitive to file.
                        WritePrimitiveBlock(model, writer, i);
                    }
                }
                writer.WriteFullEndElement();

                // Write the collada and document end.
                writer.WriteEndElement();
                writer.WriteEndDocument();

                // Flush to file.
                writer.Close();
                return(true);
            }
        }