示例#1
0
        private void Render(object sender, PaintEventArgs e)
        {
            if (!ReadyToRender)
            {
                return;
            }

            glViewport.MakeCurrent();

            GL.LoadIdentity();
            GL.Viewport(glViewport.ClientRectangle);

            // Push all attributes so we don't have to clean up later
            GL.PushAttrib(AttribMask.AllAttribBits);
            // clear the gf buffer
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            // use fixed function pipeline for drawing background and floor grid
            GL.UseProgram(0);

            if (MeshList.treeView1.SelectedNode != null)
            {
                if (MeshList.treeView1.SelectedNode is BCH_Texture)
                {
                    GL.PopAttrib();
                    BCH_Texture tex = ((BCH_Texture)MeshList.treeView1.SelectedNode);
                    RenderTools.DrawTexturedQuad(tex.display, tex.Width, tex.Height, true, true, true, true, false, true);
                    glViewport.SwapBuffers();
                    return;
                }
                if (MeshList.treeView1.SelectedNode is NUT_Texture)
                {
                    GL.PopAttrib();
                    NUT_Texture tex = ((NUT_Texture)MeshList.treeView1.SelectedNode);
                    RenderTools.DrawTexturedQuad(((NUT)tex.Parent).draw[tex.HASHID], tex.Width, tex.Height, true, true, true, true, false, true);
                    glViewport.SwapBuffers();
                    return;
                }
            }

            if (Runtime.renderBackGround)
            {
                // background uses different matrices
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();

                RenderTools.RenderBackground();
            }

            // Camera Update
            // -------------------------------------------------------------
            GL.MatrixMode(MatrixMode.Projection);
            if (glViewport.ClientRectangle.Contains(glViewport.PointToClient(Cursor.Position)) &&
                glViewport.Focused &&
                CurrentMode == Mode.Normal &&
                !TransformTool.hit)
            {
                Camera.Update();
                //if (cameraPosForm != null && !cameraPosForm.IsDisposed)
                //    cameraPosForm.updatePosition();
            }
            try
            {
                if (OpenTK.Input.Mouse.GetState() != null)
                {
                    Camera.mouseSLast = OpenTK.Input.Mouse.GetState().WheelPrecise;
                }
            } catch
            {
            }

            Matrix4 matrix = Camera.getMVPMatrix();

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref matrix);

            // Floor
            // -------------------------------------------------------------
            if (Runtime.renderFloor)
            {
                RenderTools.drawFloor();
            }

            // Shadows
            // -------------------------------------------------------------
            if (Runtime.drawModelShadow)
            {
                CalculateLightSource();
                // update light matrix and setup shadowmap rendering
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadMatrix(ref lightMatrix);
                GL.Enable(EnableCap.DepthTest);
                GL.Viewport(0, 0, sw, sh);
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, sfb);

                foreach (ModelContainer m in draw)
                {
                    m.RenderShadow(Camera, 0, Matrix4.Zero, Camera.getMVPMatrix());
                }

                // reset matrices and viewport for model rendering again
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
                GL.LoadMatrix(ref matrix);
                GL.Viewport(glViewport.ClientRectangle);
            }

            // render models into hdr buffer
            // -------------------------------------------------------------
            if (Runtime.useDepthTest)
            {
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(DepthFunction.Lequal);
            }

            else
            {
                GL.Disable(EnableCap.DepthTest);
            }

            GL.Enable(EnableCap.DepthTest);

            // Models
            // -------------------------------------------------------------
            //frameTime.Start();
            if (Runtime.renderModel || Runtime.renderModelWireframe)
            {
                foreach (TreeNode m in draw)
                {
                    if (m is ModelContainer)
                    {
                        ((ModelContainer)m).Render(Camera, 0, Matrix4.Zero, Camera.getMVPMatrix());
                    }
                }
            }

            if (ViewComboBox.SelectedIndex == 1)
            {
                foreach (TreeNode m in draw)
                {
                    if (m is ModelContainer)
                    {
                        ((ModelContainer)m).RenderPoints(Camera);
                    }
                }
            }


            //GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            /*// render gaussian blur stuff
             * if (Runtime.drawQuadBlur)
             *  DrawQuadBlur();
             *
             * // render full screen quad for post processing
             * if (Runtime.drawQuadFinalOutput)
             *  DrawQuadFinalOutput();*/

            // use fixed function pipeline again for area lights, lvd, bones, hitboxes, etc
            SetupFixedFunctionRendering();

            /*// draw path.bin
             * if (Runtime.renderPath)
             *  DrawPathDisplay();
             *
             * // area light bounding boxes should intersect stage geometry and not render on top
             * if (Runtime.drawAreaLightBoundingBoxes)
             *  DrawAreaLightBoundingBoxes();*/

            // clear depth buffer so stuff will render on top of the models
            GL.Clear(ClearBufferMask.DepthBufferBit);

            if (Runtime.renderLVD)
            {
                _lvd.Render();
            }

            if (Runtime.renderBones)
            {
                foreach (ModelContainer m in draw)
                {
                    m.RenderBones();
                }
            }


            // ACMD
            if (ParamManager != null && Runtime.renderHurtboxes && draw.Count > 0 && (draw[0] is ModelContainer))
            {
                ParamManager.RenderHurtboxes(Frame, scriptId, ACMDScript, ((ModelContainer)draw[0]).GetVBN());
            }

            if (ACMDScript != null && draw.Count > 0 && (draw[0] is ModelContainer))
            {
                ACMDScript.Render(((ModelContainer)draw[0]).GetVBN());
            }
            //Debug.WriteLine(frameTime.getAverageRenderTime());



            // Bone Transform Tool
            if (ViewComboBox.SelectedIndex == 2)
            {
                if (modeBone.Checked)
                {
                    TransformTool.Render(Camera, new Ray(Camera, glViewport));
                    if (TransformTool.state == 1)
                    {
                        CurrentMode = Mode.Selection;
                    }
                    else
                    {
                        CurrentMode = Mode.Normal;
                    }
                }

                if (TransformTool.HasChanged())
                {
                    if (Animation != null && TransformTool.b != null)
                    {
                        // get the node group for the current bone in animation
                        Animation.KeyNode ThisNode = null;
                        foreach (Animation.KeyNode node in Animation.Bones)
                        {
                            if (node.Text.Equals(TransformTool.b.Text))
                            {
                                // found
                                ThisNode = node;
                                break;
                            }
                        }
                        if (ThisNode == null)
                        {
                            ThisNode = new Animation.KeyNode(TransformTool.b.Text);
                            Animation.Bones.Add(ThisNode);
                        }

                        // update or add the key frame
                        ThisNode.SetKeyFromBone((float)currentFrame.Value, TransformTool.b);
                    }
                }
            }


            // Mouse selection
            // -------------------------------------------------------------
            if (ViewComboBox.SelectedIndex == 1)
            {
                try
                {
                    if (CurrentMode == Mode.Normal && OpenTK.Input.Mouse.GetState().IsButtonDown(OpenTK.Input.MouseButton.Right))
                    {
                        CurrentMode = Mode.Selection;
                        Vector2 m = GetMouseOnViewport();
                        sx1 = m.X;
                        sy1 = m.Y;
                    }
                }
                catch
                {
                }
                if (CurrentMode == Mode.Selection)
                {
                    if (!OpenTK.Input.Mouse.GetState().IsButtonDown(OpenTK.Input.MouseButton.Right))
                    {
                        checkSelect();
                        CurrentMode = Mode.Normal;
                    }

                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.PushMatrix();
                    GL.LoadIdentity();

                    Vector2 m = GetMouseOnViewport();
                    GL.Color3(Color.Black);
                    GL.LineWidth(2f);
                    GL.Begin(PrimitiveType.LineLoop);
                    GL.Vertex2(sx1, sy1);
                    GL.Vertex2(m.X, sy1);
                    GL.Vertex2(m.X, m.Y);
                    GL.Vertex2(sx1, m.Y);
                    GL.End();

                    GL.Color3(Color.White);
                    GL.LineWidth(1f);
                    GL.Begin(PrimitiveType.LineLoop);
                    GL.Vertex2(sx1, sy1);
                    GL.Vertex2(m.X, sy1);
                    GL.Vertex2(m.X, m.Y);
                    GL.Vertex2(sx1, m.Y);
                    GL.End();
                    GL.PopMatrix();
                }
            }

            /*if (CurrentMode == Mode.Photoshoot)
             * {
             *  freezeCamera = false;
             *  if (Keyboard.GetState().IsKeyDown(Key.W) && Mouse.GetState().IsButtonDown(MouseButton.Left))
             *  {
             *      shootX = this.PointToClient(Cursor.Position).X;
             *      shootY = this.PointToClient(Cursor.Position).Y;
             *      freezeCamera = true;
             *  }
             *  // Hold on to your pants, boys
             *  RenderTools.DrawPhotoshoot(glViewport, shootX, shootY, shootWidth, shootHeight);
             * }*/

            GL.PopAttrib();
            glViewport.SwapBuffers();
        }