Example #1
0
        private void GlControlOnRender(object?sender, GlControlEventArgs e)
        {
            var senderControl = sender as GlControl;

            int vpx = 0;
            int vpy = 0;
            int vpw = senderControl?.ClientSize.Width ?? 1;
            int vph = senderControl?.ClientSize.Height ?? 1;

            Gl.Viewport(vpx, vpy, vpw, vph);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            var projectionMatrix = _openGLCamera.ProjectionMatrix(vpw, vph);
            var viewMatrix       = _openGLCamera.ViewMatrix;

            Gl.UseProgram(_program);

            var u = Uniforms.Get(_program);

            foreach (var meshEntry in _meshes.Where(m => m.Enabled))
            {
                var modelMatrix = meshEntry.GLMesh.ModelMatrix;
                Gl.UniformMatrix4f(u.M, 1, true, modelMatrix);
                Gl.UniformMatrix4f(u.P, 1, true, projectionMatrix);
                Gl.UniformMatrix4f(u.V, 1, true, viewMatrix);
                Gl.Uniform3f(u.LightPosition, 1, CameraLight?.Position ?? new Vector3(-50, 200, 50));
                Gl.Uniform1f(u.LightPower, 1, _settingsViewModel.LightStrength);
                Gl.Uniform1f(u.LightPower, 1, _settingsViewModel.LightStrength);
                Gl.Uniform1f(u.Ambient, 1, _settingsViewModel.Ambient);
                Gl.Uniform1f(u.SpecularStrength, 1, _settingsViewModel.SpecularMapStrength);

                meshEntry.GLMesh.Render(u);
            }
        }
Example #2
0
        public void Render(Uniforms u)
        {
            Gl.EnableVertexAttribArray(0);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _buffers.VertexBuffer);
            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);

            Gl.EnableVertexAttribArray(1);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _buffers.UvBuffer);
            Gl.VertexAttribPointer(1, 2, VertexAttribType.Float, false, 0, IntPtr.Zero);

            Gl.EnableVertexAttribArray(2);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _buffers.NormalBuffer);
            Gl.VertexAttribPointer(2, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);

            Gl.EnableVertexAttribArray(3);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _buffers.TangentBuffer);
            Gl.VertexAttribPointer(3, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);

            Gl.EnableVertexAttribArray(4);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _buffers.BiTangentBuffer);
            Gl.VertexAttribPointer(4, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);

            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, _buffers.IndexBuffer);

            void BindTexture(int textureId, TextureUnit unit, Texture?texture, int slot)
            {
                if (texture != null)
                {
                    Gl.ActiveTexture(unit);
                    Gl.BindTexture(TextureTarget.Texture2d, texture.Buffer);
                    Gl.Uniform1i(textureId, 1, slot);
                }
            }

            BindTexture(u.DiffuseSampler, TextureUnit.Texture0, DiffuseTexture, 0);
            BindTexture(u.NormalSampler, TextureUnit.Texture1, NormalMap, 1);
            BindTexture(u.SpecularSampler, TextureUnit.Texture2, SpecularMap, 2);

            Gl.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, IntPtr.Zero);

            Gl.DisableVertexAttribArray(0);
        }