예제 #1
0
        private void renderControl_Render(object sender, EventArgs e)
        {
            this.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} - {1} FPS", Application.ProductName, Core.CurrentFramesPerSecond);

            RenderControl renderControl = (sender as RenderControl);

            if (takeScreenshot)
            {
                GL.ClearColor(1.0f, 1.0f, 1.0f, 0.0f);
            }

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            // OpenTK GLControl focus checking is GARBAGE, might as well leave the check here off -.-
            if (renderControl.Focused)
            {
                OpenTK.Input.KeyboardState kbdState = OpenTK.Input.Keyboard.GetState();

                if (kbdState[OpenTK.Input.Key.Q])
                {
                    sunAngle += Core.DeltaTime / 15.0f;
                }
                if (kbdState[OpenTK.Input.Key.E])
                {
                    sunAngle -= Core.DeltaTime / 15.0f;
                }

                lastKbd = kbdState;

                camera.Update(Core.DeltaTime);
            }

            if (wireframe)
            {
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
            }
            else
            {
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            }

            if (culling)
            {
                GL.Enable(EnableCap.CullFace);
            }
            else
            {
                GL.Disable(EnableCap.CullFace);
            }

            if (shader != null)
            {
                Matrix4 tempMatrix = modelviewMatrix * camera.GetViewMatrix();
                shader.SetUniformMatrix(modelviewMatrixName, false, tempMatrix);

                shader.SetUniform(sunPositionName, new Vector3((float)(Math.Cos(sunAngle * Math.PI / 180.0) * 70.0f), (float)(Math.Sin(sunAngle * Math.PI / 180.0) * 70.0f), 0.0f));
                shader.SetUniform(enableLightName, Convert.ToInt32(lighting));
            }

            if (meshes != null)
            {
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

                shader.SetUniform(discardOpaqueName, 0);
                foreach (Mesh mesh in meshes)
                {
                    mesh.Render();
                }
                shader.SetUniform(discardOpaqueName, 1);
                foreach (Mesh mesh in meshes)
                {
                    mesh.Render();
                }
            }

            if (!takeScreenshot && font != null)
            {
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

                StringBuilder builder = new StringBuilder();
                builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0:0} FPS\n", Core.CurrentFramesPerSecond);

                if (meshes != null)
                {
                    builder.AppendLine();
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Camera: WASD & Mouse+Left Button\n");
                    builder.AppendLine();
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Vsync (F1): {0}\n", renderControl.VSync);
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Wireframe (F2): {0}\n", wireframe);
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Culling (F3): {0}\n", culling);
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Lighting (F4): {0}\n", lighting);
                    builder.AppendLine();
                    builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "Rotate Light (Q/E)\n");
                }

                font.DrawString(8.0f, 8.0f, builder.ToString());
            }

            if (takeScreenshot)
            {
                renderControl.GrabScreenshot(true).Save(screenshotPath);
                takeScreenshot = false;

                GL.ClearColor(renderControl.BackColor);
            }
        }