コード例 #1
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Create program
            _programId = GL.CreateProgram();
            Instance.AssignPID(_programId);

            // Adjust render settings
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            GL.PatchParameter(PatchParameterInt.PatchVertices, 3);

            // Create and compile shaders
            int vertexShader   = ShaderHandler.CreateShader(@"Shaders\vertexshader.glsl", ShaderType.VertexShader);
            int fragmentShader = ShaderHandler.CreateShader(@"Shaders\fragmentshader.glsl", ShaderType.FragmentShader);

            _shadersList.Add(vertexShader);
            _shadersList.Add(fragmentShader);

            GL.UseProgram(_programId);
            ShaderHandler.CompileShaders(_programId, _shadersList); // we want both shaders active at once

            // Debug
            string DebugInfo = GL.GetProgramInfoLog(_programId);

            Logging.LogEvent(string.IsNullOrEmpty(DebugInfo) ? "Graphics success" : DebugInfo);

            // Add closed event
            Closed += OnExit;
        }
コード例 #2
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            _time += e.Time;
            Title  = "InstantGameworks (OpenGL " + GL.GetString(StringName.Version) + ") " + System.Math.Round(1f / e.Time) + "fps";

            // Adjust viewport
            GL.Viewport(0, 0, Width, Height);

            // Clear frame
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.ClearColor(Color.CornflowerBlue);

            // Reset caps
            foreach (EnableCap cap in Enum.GetValues(typeof(EnableCap)))
            {
                GL.Disable(cap);
            }
            foreach (EnableCap cap in EnableCaps)
            {
                GL.Enable(cap);
            }
            // Render frame
            GL.UseProgram(_programId);
            ShaderHandler.CompileShaders(_programId, _shadersList);

            // Update camera
            Camera.AspectRatio = (float)Width / Height;
            Camera.Update();
            _cameraMatrix = Camera.PerspectiveMatrix;
            GL.UniformMatrix4(3, false, ref _cameraMatrix);
            Matrix4 cameraPosition;

            Matrix4.CreateTranslation(Camera.Position.X, Camera.Position.Y, Camera.Position.Z, out cameraPosition);
            GL.UniformMatrix4(9, false, ref cameraPosition);



            int dLightCount = 0;

            foreach (var dLight in _directionalLights)
            {
                var baseLoc = GL.GetUniformLocation(_programId, "dLights[" + dLightCount++ + "].diffuseColor");
                GL.Uniform4(baseLoc + 0, dLight.DiffuseColor);
                GL.Uniform4(baseLoc + 1, dLight.SpecularColor);
                GL.Uniform4(baseLoc + 2, dLight.AmbientColor);
                GL.Uniform4(baseLoc + 3, dLight.EmitColor);
                GL.Uniform1(baseLoc + 4, dLight.Intensity);
                GL.Uniform3(baseLoc + 5, dLight.RelativeDirection);
                GL.Uniform1(baseLoc + 6, dLight.Enabled ? 1 : 0);
            }
            foreach (var pLight in _pointLights)
            {
                var baseLoc = GL.GetUniformLocation(_programId, "pLights[" + dLightCount++ + "].diffuseColor");
                GL.Uniform4(baseLoc + 0, pLight.DiffuseColor);
                GL.Uniform4(baseLoc + 1, pLight.SpecularColor);
                GL.Uniform4(baseLoc + 2, pLight.AmbientColor);
                GL.Uniform4(baseLoc + 3, pLight.EmitColor);
                GL.Uniform1(baseLoc + 4, pLight.Intensity);
                GL.Uniform1(baseLoc + 5, pLight.Radius);
                GL.Uniform3(baseLoc + 6, pLight.Position);
                GL.Uniform1(baseLoc + 7, pLight.Enabled ? 1 : 0);
            }
            foreach (var renderObject in RenderObjects)
            {
                GL.Uniform4(5, renderObject.DiffuseColor);
                GL.Uniform4(6, renderObject.SpecularColor);
                GL.Uniform4(7, renderObject.AmbientColor);
                GL.Uniform4(8, renderObject.EmitColor);
                renderObject.Render();
            }


            // Render frame
            GL.UseProgram(GuiHolder._pid);                                    // use new program
            ShaderHandler.CompileShaders(GuiHolder._pid, GuiHolder._shaders); // apply separate shaders to said program
            GL.Disable(EnableCap.CullFace);
            foreach (var guiHolder in GuiObjects)
            {
                guiHolder.Render(); // render
            }



            SwapBuffers();
            base.OnRenderFrame(e);
        }