Пример #1
0
        /// <summary>
        /// Draw the scene
        /// </summary>
        /// <param name="camera">The camera where you want to draw from</param>
        /// <param name="window">The window you want to draw to</param>
        public void Draw(Camera camera, GameWindow window)
        {
            Matrix4 mat = camera.CalculateMatrix();

            _voxelData.Update();
            _modelData.Update();

            for (int i = 0; i < _models.Count; i++)
            {
                Matrix4 inverseModelMatrix = _models[i].Transform.CalculateInverseMatrix();
                Matrix4 modelMatrix        = _models[i].Transform.CalculateMatrix();
                Matrix4 normalMatrix       = _models[i].Transform.CalculateNormalMatrix();

                // Check if the matrices are the same to avoid sending data the gpu already has
                if (_modelTransformations[i * 3 + 0] != inverseModelMatrix)
                {
                    _modelTransformations[i * 3 + 0] = inverseModelMatrix;
                }

                if (_modelTransformations[i * 3 + 1] != modelMatrix)
                {
                    _modelTransformations[i * 3 + 1] = modelMatrix;
                }

                if (_modelTransformations[i * 3 + 2] != normalMatrix)
                {
                    _modelTransformations[i * 3 + 2] = normalMatrix;
                }
            }
            _modelTransformations.Update();

            Shader.Bind();
            GL.BindVertexArray(_canvasFB.Vao);

            // Send the buffers containing the models
            _voxelData.Bind(TextureUnit.Texture0);
            GL.Uniform1(Shader.GetUniformLocation("u_voxelBuffer"), 1, new[] { 0 });
            _modelData.Bind(TextureUnit.Texture1);
            GL.Uniform1(Shader.GetUniformLocation("u_modelData"), 1, new[] { 1 });
            _modelTransformations.Bind(TextureUnit.Texture2);
            GL.Uniform1(Shader.GetUniformLocation("u_modelTransformations"), 1, new[] { 2 });

            // Send the materials
            Materials.Bind("u_materials");

            // Send the windowsize
            GL.Uniform2(Shader.GetUniformLocation("u_windowSize"), 1, new float[] { window.Width, window.Height / 2 });

            // Send the camera
            GL.Uniform1(Shader.GetUniformLocation("u_camera.zoom"), 1, new[] { (window.Height / 1.5f) / (float)Math.Tan(camera.Fov * (Math.PI / 360.0f)) });
            GL.UniformMatrix4(Shader.GetUniformLocation("u_camera.matrix"), false, ref mat);

            // Send the lights
            GL.Uniform1(Shader.GetUniformLocation("u_dirLightCount"), DirectionalLights.Count());
            for (int i = 0; i < DirectionalLights.Count(); i++)
            {
                GL.Uniform3(Shader.GetUniformLocation($"u_dirLights[{i}].direction"), DirectionalLights[i].direction);
                GL.Uniform1(Shader.GetUniformLocation($"u_dirLights[{i}].intensity"), DirectionalLights[i].intensity);
                GL.Uniform3(Shader.GetUniformLocation($"u_dirLights[{i}].colour"), DirectionalLights[i].colour);
            }
            GL.Uniform1(Shader.GetUniformLocation("u_pointLightCount"), PointLights.Count());
            for (int i = 0; i < PointLights.Count(); i++)
            {
                GL.Uniform3(Shader.GetUniformLocation($"u_pointLights[{i}].position"), PointLights[i].position); //_pointLights[i].position
                GL.Uniform1(Shader.GetUniformLocation($"u_pointLights[{i}].intensity"), PointLights[i].intensity);
                GL.Uniform3(Shader.GetUniformLocation($"u_pointLights[{i}].colour"), PointLights[i].colour);
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
            //GL.Viewport(window.Width / 2, window.Height / 2, window.Width / 2, window.Height / 2);
            GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            Shader.Unbind();

            ScaleShader.Bind();
            GL.BindVertexArray(_canvas.Vao);
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, _textureColorBuffer);
            GL.Uniform1(ScaleShader.GetUniformLocation("u_framebuffer"), 1, new[] { 0 });
            GL.Uniform2(ScaleShader.GetUniformLocation("u_resolution"), 1, new float[] { window.Width, window.Height });

            //GL.Viewport(0,0, window.Width,window.Height);

            GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);

            ScaleShader.Unbind();
        }