예제 #1
0
        /// <summary>
        /// Initalises the Vertex Array Object without seting any vertex or index data
        /// </summary>
        public unsafe void InitElementsArrayBuffer <T>(int stride, int[] attributeLengths, int[] attributeOffsets, BufferUsageHint hint = BufferUsageHint.GL_STATIC_DRAW) where T : IInterleavedVertex
        {
            GlBindings.GenVertexArrays(1, out m_VaoId);

            GlBindings.GenBuffers(1, out m_VertexArrayBufferId);
            GlBindings.GenBuffers(1, out m_IndexBufferId);

            GlBindings.BindVertexArray(m_VaoId);

            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, m_VertexArrayBufferId);

            GlBindings.BindBuffer(BufferTarget.GL_ELEMENT_ARRAY_BUFFER, m_IndexBufferId);


            // Setup Attributes
            for (int i = 0; i < attributeLengths.Length; i++)
            {
                GlBindings.VertexAttribPointer(i, attributeLengths[i], VertexAttribPointerType.Float, 0, stride, attributeOffsets[i]);
                GlBindings.EnableVertexAttribArray(i);
            }

            //m_VertexCount = indicies.Length;
            m_Stride      = stride;
            m_BufferUsage = hint;

            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, 0);
            GlBindings.BindVertexArray(0);
        }
예제 #2
0
        public unsafe void BindArrayBuffer <T>(T[] verts, int stride, int[] attributeLengths, int[] attributeOffsets, BufferUsageHint hint = BufferUsageHint.GL_STATIC_DRAW) where T : IInterleavedVertex
        {
            GlBindings.GenVertexArrays(1, out m_VaoId);

            GlBindings.GenBuffers(1, out m_VertexArrayBufferId);
            GlBindings.BindVertexArray(m_VaoId);

            GCHandle handle       = GCHandle.Alloc(verts, GCHandleType.Pinned);
            IntPtr   ptrVerticies = handle.AddrOfPinnedObject();

            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, m_VertexArrayBufferId);
            GlBindings.BufferData(BufferTarget.GL_ARRAY_BUFFER, stride * verts.Length, ptrVerticies, hint);
            handle.Free();

            // Setup Attributes
            for (int i = 0; i < attributeLengths.Length; i++)
            {
                GlBindings.VertexAttribPointer(i, attributeLengths[i], VertexAttribPointerType.Float, 0, stride, attributeOffsets[i]);
                GlBindings.EnableVertexAttribArray(i);
            }

            m_VertexCount = verts.Length;

            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, 0);
            GlBindings.BindVertexArray(0);
        }
예제 #3
0
        internal static Texture2D Create(int width, int height, int bytesPerPixel)
        {
            byte[] bytearray = new byte[width * height * bytesPerPixel];

            // is there a faster way to create white?
            for (int i = 0; i < bytearray.Length; i++)
            {
                bytearray[i] = 255;
            }

            GCHandle pinnedArray = GCHandle.Alloc(bytearray, GCHandleType.Pinned);
            IntPtr   pixels      = pinnedArray.AddrOfPinnedObject();

            var rv = new Texture2D();

            {
                GlBindings.GenTextures(1, out rv.m_TextureId);
                GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR);

                GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
            }
            pinnedArray.Free();

            return(rv);
        }
        public GraphicsCapabilities()
        {
            m_OpenGlVersion  = GlBindings.GetString(GLStrings.GL_VERSION);
            m_OpenGlRenderer = GlBindings.GetString(GLStrings.GL_RENDERER);
            m_OpenGlVendor   = GlBindings.GetString(GLStrings.GL_VENDOR);

            Console.WriteLine("OpenGL Renderer:" + OpenGlRenderer);
            Console.WriteLine("OpenGL Version:" + m_OpenGlVersion);
            Console.WriteLine("OpenGL Vendor:" + m_OpenGlVendor);

            GlBindings.GetIntegerv(Capabilities.GL_MAX_TEXTURE_IMAGE_UNITS, out m_MaxTextureImageUnits);
            GlBindings.GetIntegerv(Capabilities.GL_MAX_SAMPLES, out m_MaxMultiSampleCount);
            GlBindings.GetIntegerv(Capabilities.GL_NUM_EXTENSIONS, out m_NumExtensions);

            Console.WriteLine("GL_MAX_TEXTURE_IMAGE_UNITS:" + m_MaxTextureImageUnits);
            Console.WriteLine("GL_MAX_SAMPLES:" + m_MaxMultiSampleCount);
            Console.WriteLine("GL_NUM_EXTENSIONS:" + m_NumExtensions);

            for (uint i = 0; i < m_NumExtensions; i++)
            {
                m_Extensions.Add(GlBindings.glGetStringi(GLStrings.GL_EXTENSIONS, i));
            }
            Console.WriteLine("");
            foreach (var e in m_Extensions)
            {
                Console.WriteLine(e);
            }
        }
예제 #5
0
        public void SetUniform(string uniformParameterName, Matrix4 transform)
        {
            // TODO Cache location
            var location = GlBindings.GetUniformLocation(ShaderProgramId, uniformParameterName);

            GlBindings.UniformMatrix4fv(location, 1, 0, transform);
        }
예제 #6
0
        public void SetUniform(string uniformParameterName, int value)
        {
            // TODO Cache location
            var location = GlBindings.GetUniformLocation(ShaderProgramId, uniformParameterName);

            GlBindings.Uniform1i(location, value);
        }
예제 #7
0
        public void SetUniform(string uniformParameterName, Vector3 value)
        {
            // TODO Cache location
            var location = GlBindings.GetUniformLocation(ShaderProgramId, uniformParameterName);

            GlBindings.Uniform3f(location, value.X, value.Y, value.Z);
        }
예제 #8
0
        public unsafe void SetVertexData <T>(T[] verts)
        {
            GCHandle handleVerticies = GCHandle.Alloc(verts, GCHandleType.Pinned);

            IntPtr ptrVerticies = handleVerticies.AddrOfPinnedObject();

            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, m_VertexArrayBufferId);
            GlBindings.BufferData(BufferTarget.GL_ARRAY_BUFFER, m_Stride * verts.Length, ptrVerticies, m_BufferUsage);

            handleVerticies.Free();
        }
예제 #9
0
 public void SetRenderTarget(RenderTarget renderTarget)
 {
     if (renderTarget != null)
     {
         GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, renderTarget.RenderTargetId);
     }
     else
     {
         GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, 0);
     }
 }
예제 #10
0
        public unsafe void SetIndexData32(uint[] indicies)
        {
            GCHandle handleIndicies = GCHandle.Alloc(indicies, GCHandleType.Pinned);

            IntPtr ptrIndicies = handleIndicies.AddrOfPinnedObject();

            GlBindings.BindBuffer(BufferTarget.GL_ELEMENT_ARRAY_BUFFER, m_IndexBufferId);
            GlBindings.BufferData(BufferTarget.GL_ELEMENT_ARRAY_BUFFER, indicies.Length * 4, ptrIndicies, m_BufferUsage);   // TODO Support UnsignedInt and UnsignedShort
            handleIndicies.Free();

            m_VertexCount = indicies.Length;
        }
예제 #11
0
        public unsafe void UpdateVertexData <T>(T[] verts, int offset, int vertexCount)
        {
            GCHandle handleVerticies = GCHandle.Alloc(verts, GCHandleType.Pinned);

            IntPtr ptrVerticies = handleVerticies.AddrOfPinnedObject();


            GlBindings.BindBuffer(BufferTarget.GL_ARRAY_BUFFER, m_VertexArrayBufferId);
            GlBindings.BufferSubData(BufferTarget.GL_ARRAY_BUFFER, (IntPtr)0, (IntPtr)(m_Stride * vertexCount), ptrVerticies);

            handleVerticies.Free();
        }
예제 #12
0
        public unsafe void UpdateIndexData32(uint[] indicies)
        {
            GCHandle handleIndicies = GCHandle.Alloc(indicies, GCHandleType.Pinned);

            IntPtr ptrIndicies = handleIndicies.AddrOfPinnedObject();

            GlBindings.BindBuffer(BufferTarget.GL_ELEMENT_ARRAY_BUFFER, m_IndexBufferId);
            GlBindings.BufferSubData(BufferTarget.GL_ELEMENT_ARRAY_BUFFER, (IntPtr)0, (IntPtr)(4 * indicies.Length), ptrIndicies);
            handleIndicies.Free();

            m_VertexCount = indicies.Length;
        }
        public void Draw(uint textureId)
        {
            GameWindow.GraphicsDevice.BindTexture2D(textureId, OpenGL.TextureUnits.GL_TEXTURE0);
            GameWindow.GraphicsDevice.BindTexture2D(0, OpenGL.TextureUnits.GL_TEXTURE1);

            GameWindow.GraphicsDevice.BindShaderProgram(GameWindow.FullScreenShader.ShaderProgramId);

            GameWindow.QuadBatchShader.SetUniform("texture1", 0);

            GlBindings.BindVertexArray(m_VertexArrayObject.VaoId);
            GlBindings.DrawElements(PrimitiveType.TriangleList, 1 * 6, DrawElementsType.UnsignedInt, 0);

            GlBindings.BindVertexArray(0);
        }
예제 #14
0
        internal static Texture2D Create(IntPtr pixels, int width, int height, int bytesPerPixel)
        {
            var rv = new Texture2D();

            GlBindings.GenTextures(1, out rv.m_TextureId);
            GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId);
            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT);
            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT);
            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR);
            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR);

            GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);

            return(rv);
        }
예제 #15
0
        internal static Texture2D CreateFromFile(string filename, bool generateMipmaps = false)
        {
            using (TracedStopwatch.Start("Loading Texture: " + filename))
            {
                IntPtr intPtr      = SDL2.SDL_image.IMG_Load(filename);
                var    surface     = Marshal.PtrToStructure <SDL.SDL_Surface>(intPtr);
                var    pixelFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format);

                var mode = OpenGL.PixelFormat.Rgba;
                if (pixelFormat.BytesPerPixel == 3)
                {
                    mode = OpenGL.PixelFormat.Rgb;
                }
                if (pixelFormat.BytesPerPixel == 1)
                {
                    mode = OpenGL.PixelFormat.Red;
                }

                var rv = new Texture2D();

                // TODO Pass in wraping styles
                GlBindings.GenTextures(1, out rv.m_TextureId);
                GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureId);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_S, TextureAttributeValue.GL_REPEAT);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_WRAP_T, TextureAttributeValue.GL_REPEAT);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR);
                GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR);

                var internalFormat = OpenGL.PixelInternalFormat.Rgba;
                if (mode == PixelFormat.Luminance)
                {
                    internalFormat = PixelInternalFormat.Red;
                }

                GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, internalFormat, surface.w, surface.h, 0, mode, PixelType.UnsignedByte, surface.pixels);

                if (generateMipmaps)
                {
                    GlBindings.GenerateMipmap(TextureType.GL_TEXTURE_2D);
                }

                SDL2.SDL.SDL_free(intPtr);

                return(rv);
            }
        }
예제 #16
0
        private void Flush()
        {
            if (m_quadRecordCount > 0)
            {
                var device = GameWindow.GraphicsDevice;

                var sX = 2 / (float)GameWindow.ScreenWidth;
                var sY = 2 / (float)GameWindow.ScreenHeight;

                var oX = -1;
                var oY = 1;

                var quadCount = m_quadRecordCount;

                // Edit Vertex Data
                for (int i = 0; i < quadCount; i++)
                {
                    var record = quadRecords[i];
                    m_Verticies[i * 4 + 0].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y + record.H) * sY, 0.0f);       // Top Right
                    m_Verticies[i * 4 + 1].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y * sY), 0.0f);                  // Bottom Right
                    m_Verticies[i * 4 + 2].Position = new Vector3(oX + record.X * sX, oY - (record.Y * sY), 0.0f);                               // Bottom Left
                    m_Verticies[i * 4 + 3].Position = new Vector3(oX + record.X * sX, oY - (record.Y + record.H) * sY, 0.0f);                    // Top Left
                }

                m_VertexArrayObject.UpdateVertexData <DefaultQuadBatchVertex>(m_Verticies, 0, quadCount * 6);

                // TODO Access the IPlatformGraphicsDevice
                GameWindow.GraphicsDevice.BindShaderProgram(GameWindow.QuadBatchShader.ShaderProgramId);
                GameWindow.QuadBatchShader.SetUniform("texture1", 0);
                GameWindow.QuadBatchShader.SetUniform("texture2", 1);

                GlBindings.BindVertexArray(m_VertexArrayObject.VaoId);
                GlBindings.DrawElements(PrimitiveType.TriangleList, quadCount * 6, DrawElementsType.UnsignedInt, 0);

                m_quadRecordCount = 0;
            }
        }
        // TODO Will probably need to handle resizing the framebuffer

        // TODO Make internal and store
        public static RenderTarget Create(int width, int height)
        {
            var rv = new RenderTarget();

            // Create the frame buffer
            GlBindings.GenFramebuffers(1, out rv.m_RanderTargetId);
            GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, rv.m_RanderTargetId);

            // Attach the texture component
            GlBindings.GenTextures(1, out rv.m_TextureColorBuffer);
            GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, rv.m_TextureColorBuffer);
            GlBindings.TexImage2D(TextureType.GL_TEXTURE_2D, 0, OpenGL.PixelInternalFormat.Rgb, width, height, 0, OpenGL.PixelFormat.Rgb, OpenGL.PixelType.UnsignedByte, IntPtr.Zero);

            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, TextureAttributeValue.GL_LINEAR);
            GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, TextureAttributeValue.GL_LINEAR);

            GlBindings.glFramebufferTexture2D(FrameBuffer.GL_FRAMEBUFFER, Attachment.GL_COLOR_ATTACHMENT0, TextureType.GL_TEXTURE_2D, rv.m_TextureColorBuffer, 0);


            // TODO Depth buffer!

            GlBindings.glGenRenderbuffers(1, out rv.m_RenderBufferObjectId);
            GlBindings.glBindRenderbuffer(RenderBuffer.GL_RENDERBUFFER, rv.m_RenderBufferObjectId);
            GlBindings.glRenderbufferStorage(RenderBuffer.GL_RENDERBUFFER, InternalFormat.GL_DEPTH24_STENCIL8, width, height);
            GlBindings.glFramebufferRenderbuffer(RenderBuffer.GL_FRAMEBUFFER, Attachment.GL_DEPTH_STENCIL_ATTACHMENT, RenderBuffer.GL_RENDERBUFFER, rv.RenderBufferObjectId);

            if (GlBindings.glCheckFramebufferStatus(FrameBuffer.GL_FRAMEBUFFER) != (int)ReturnValue.GL_FRAMEBUFFER_COMPLETE)
            {
                throw new Exception("Error with framebuffer");
            }


            // Store previous
            GlBindings.BindFrameBuffer(OpenGL.FrameBuffer.GL_FRAMEBUFFER, 0);
            return(rv);
        }
예제 #18
0
 public void BindShaderProgram(int shaderId)
 {
     GlBindings.UseProgram(shaderId);
 }
예제 #19
0
        public void DrawText(string text, Vector2 position, TextureFont font, Vector4 color, TextureUnits textureUnit = TextureUnits.GL_TEXTURE0, int layer = 0)
        {
            // TODO? Make a seperate FontBatcher?
            // See http://www.angelcode.com/products/bmfont/doc/render_text.html
            // To improve the rendering...

            var fontSheetWidth  = (float)font.Font.Common.ScaleW;
            var fontSheetHeight = (float)font.Font.Common.ScaleH;

            for (int i = 0; i < text.Length; i++)
            {
                var quad = new QuadRecord();
                quad.X = 32 + i * 32 + 16 * i + position.X;
                quad.Y = 32 + (2 * i) + position.Y;
                quad.W = 32;
                quad.H = 32;

                // Compute the U, V and UW, UH values
                var charId   = (byte)text[i];
                var fontChar = font.CharLookup[charId];

                quad.U = fontChar.X / fontSheetWidth;
                quad.V = fontChar.Y / fontSheetHeight;

                quad.UW = fontChar.Width / fontSheetWidth;
                quad.VH = fontChar.Height / fontSheetHeight;



                PushQuad(quad);
            }

            var device = GameWindow.GraphicsDevice;

            var sX = 2 / (float)GameWindow.ScreenWidth;
            var sY = 2 / (float)GameWindow.ScreenHeight;

            var oX = -1;
            var oY = 1;

            var quadCount = m_quadRecordCount;

            // Edit Vertex Data
            for (int i = 0; i < quadCount; i++)
            {
                var record = quadRecords[i];
                m_Verticies[i * 4 + 0].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y + record.H) * sY, 0.0f);       // Top Right
                m_Verticies[i * 4 + 1].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y * sY), 0.0f);                  // Bottom Right
                m_Verticies[i * 4 + 2].Position = new Vector3(oX + record.X * sX, oY - (record.Y * sY), 0.0f);                               // Bottom Left
                m_Verticies[i * 4 + 3].Position = new Vector3(oX + record.X * sX, oY - (record.Y + record.H) * sY, 0.0f);                    // Top Left


                m_Verticies[i * 4 + 0].Texture = new Vector2(record.U + record.UW, record.V + record.VH);                      // Note: Inverted Y
                m_Verticies[i * 4 + 1].Texture = new Vector2(record.U + record.UW, record.V);
                m_Verticies[i * 4 + 2].Texture = new Vector2(record.U, record.V);
                m_Verticies[i * 4 + 3].Texture = new Vector2(record.U, record.V + record.VH);
            }

            m_VertexArrayObject.UpdateVertexData <DefaultQuadBatchVertex>(m_Verticies, 0, quadCount * 6);


            // TODO Make a font.glsl shader
            //    GlBindings.PolygonMode(Face.GL_FRONT_AND_BACK, Mode.GL_LINE);

            GameWindow.GraphicsDevice.BindTexture2D(font.FontTexture.TextureId, OpenGL.TextureUnits.GL_TEXTURE0);
            GameWindow.GraphicsDevice.BindTexture2D(0, OpenGL.TextureUnits.GL_TEXTURE1);

            GameWindow.GraphicsDevice.BindShaderProgram(GameWindow.QuadBatchShader.ShaderProgramId);

            GameWindow.QuadBatchShader.SetUniform("texture1", 0);

            GlBindings.BindVertexArray(m_VertexArrayObject.VaoId);
            GlBindings.DrawElements(PrimitiveType.TriangleList, quadCount * 6, DrawElementsType.UnsignedInt, 0);

            GlBindings.BindVertexArray(0);

            m_quadRecordCount = 0;
        }
예제 #20
0
 public void FillMode(Mode mode)
 {
     GlBindings.PolygonMode(Face.GL_FRONT_AND_BACK, mode);
 }
예제 #21
0
 public void SetViewport(int x, int y, uint width, uint height)
 {
     GlBindings.glViewport(x, y, width, height);
 }
예제 #22
0
 public void DepthFunc(DepthFunc depthFunc)
 {
     GlBindings.DepthFunc(depthFunc);
 }
예제 #23
0
 public void BlendFunc(BlendFunc srcBlend, BlendFunc dstBlend)
 {
     GlBindings.BlendFunc(srcBlend, dstBlend);
 }
예제 #24
0
 public void Disable(Enable enableFlags)
 {
     GlBindings.Disable(enableFlags);
 }
예제 #25
0
 public void SetTextureSamplingAttribute(TextureAttributeValue attribute)
 {
     GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MIN_FILTER, attribute);
     GlBindings.TexParameteri(TextureType.GL_TEXTURE_2D, TextureAttribute.GL_TEXTURE_MAG_FILTER, attribute);
 }
예제 #26
0
 public void Enable(Enable enableFlags)
 {
     GlBindings.Enable(enableFlags);
 }
예제 #27
0
 public void BindTexture2D(uint textureId, TextureUnits textureUnit = TextureUnits.GL_TEXTURE0)
 {
     GlBindings.ActiveTexture(textureUnit);
     GlBindings.BindTexture(TextureType.GL_TEXTURE_2D, textureId);
 }
예제 #28
0
 public void DrawElements(PrimitiveType primitiveType, int count, DrawElementsType elementType, int offset)
 {
     GlBindings.DrawElements(primitiveType, count, elementType, offset);
 }
예제 #29
0
 public void DrawArrays(PrimitiveType primitiveType, int offset, int vertexCount)
 {
     GlBindings.DrawArrays(primitiveType, offset, vertexCount);
 }
예제 #30
0
 public void BindVertexArrayObject(int vertexArrayObjectId)
 {
     GlBindings.BindVertexArray(vertexArrayObjectId);
 }