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); }
/// <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); }
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); }
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; } }
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; }
public void BindVertexArrayObject(int vertexArrayObjectId) { GlBindings.BindVertexArray(vertexArrayObjectId); }