public TextRenderer(TextShader _shader) { UIQuad = Loader.LoadToVAO(Geometry.GetVertex(Geometries.Quad), Geometry.GetVertexName(Geometries.Quad)); shader = _shader; shader.Start(); shader.Stop(); }
private static void CreateResources() { ResourceFactory factory = _graphicsDevice.ResourceFactory; textShader = new TextShader(factory); // Create pipeline GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription(); pipelineDescription.BlendState = BlendStateDescription.SingleOverrideBlend; pipelineDescription.DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual); pipelineDescription.RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false); pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleStrip; pipelineDescription.ResourceLayouts = System.Array.Empty <ResourceLayout>(); pipelineDescription.ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { textShader.Layout }, shaders: new Shader[] { textShader.VertexShader, textShader.FragmentShader }); pipelineDescription.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription; _pipeline = factory.CreateGraphicsPipeline(pipelineDescription); _commandList = factory.CreateCommandList(); }
public void Initiliaze(Camera camera) { entityShader = new EntityShader(); terrainShader = new TerrainShader(); shaderUI = new SpriteShader(); textShader = new TextShader(); skyboxShader = new SkyboxShader(); entityRenderer = new EntityRenderer(entityShader); terrainRenderer = new TerrainRenderer(terrainShader); spriteRenderer = new SpriteRenderer(shaderUI); textRenderer = new TextRenderer(textShader); skyboxRenderer = new SkyboxRenderer(skyboxShader, camera.GetProjectionMatrix()); }
// inicializar os shaders de texto e as variáveis correspondentes do OpenGL public TextRenderer(int width, int height) { TextShader = ResourceManager.LoadShader("shaders/text.vert", "shaders/text.frag", "text"); TextShader.Use(); TextShader.SetMatrix4("projection", Matrix4.CreateOrthographicOffCenter(0, width, height, 0, -1, 1)); TextShader.SetInteger("text", 0); VAO = GL.GenVertexArray(); VBO = GL.GenBuffer(); GL.BindVertexArray(VAO); GL.BindBuffer(BufferTarget.ArrayBuffer, VBO); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * 24, IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0); GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
// desenhar cada caracter do texto utilizando a fonte carregada anteriormente // x e y denotam a posição inicial do texto, e o tamanho da fonte é utilizado // para determinar a posição do próximo caractér public void RenderText(string text, float x, float y, float scale, Vector3 color) { TextShader.Use(); TextShader.SetVector3f("textColor", color); GL.ActiveTexture(TextureUnit.Texture0); GL.BindVertexArray(VAO); foreach (char c in text) { Character ch = Characters[c]; float xpos = x + (ch.Bearing.X * scale); float ypos = y + ((Characters['X'].Bearing.Y - ch.Bearing.Y) * scale); float w = ch.Size.X * scale; float h = ch.Size.Y * scale; float[,] vertices = { { xpos, ypos + h, 0.0f, 1.0f }, { xpos + w, ypos, 1.0f, 0.0f }, { xpos, ypos, 0.0f, 0.0f }, { xpos, ypos + h, 0.0f, 1.0f }, { xpos + w, ypos + h, 1.0f, 1.0f }, { xpos + w, ypos, 1.0f, 0.0f } }; GL.BindTexture(TextureTarget.Texture2D, ch.TextureID); GL.BindBuffer(BufferTarget.ArrayBuffer, VBO); GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, sizeof(float) * vertices.Length, vertices); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); x += ch.Advance * scale; } GL.BindVertexArray(0); GL.BindTexture(TextureTarget.Texture2D, 0); }
/// <summary> /// Initializes a new instance of the <see cref="Shaders"/> class. /// Setting up all Shaders and storing them. /// </summary> /// <param name="lineShaderVertPath">The line shader vert path.</param> /// <param name="lineShaderFragPath">The line shader frag path.</param> /// <param name="TextShaderVertPath">The text shader vert path.</param> /// <param name="TextShaderFragPath">The text shader frag path.</param> public Shaders(byte[] lineShaderVert, byte[] lineShaderFrag, byte[] TextShaderVert, byte[] TextShaderFrag) { _lineShader = new LinesShader(lineShaderVert, lineShaderFrag); _textShader = new TextShader(TextShaderVert, TextShaderFrag); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Closed += ShaderTest_Closed; gl3dcontroller = new Controller3D(); gl3dcontroller.PaintObjects = ControllerDraw; gl3dcontroller.MatrixCalc.PerspectiveNearZDistance = 1f; gl3dcontroller.MatrixCalc.PerspectiveFarZDistance = 1000f; gl3dcontroller.ZoomDistance = 20F; gl3dcontroller.Start(glwfc, new Vector3(0, 0, 0), new Vector3(110f, 0, 0f), 1F); glwfc.MouseClick += GLMouseClick; gl3dcontroller.KeyboardTravelSpeed = (ms, eyedist) => { return((float)ms / 40.0f); }; items.Add(new GLColorShaderWorld(), "COSW"); items.Add(new GLColorShaderObjectTranslation(), "COSOT"); #region coloured lines if (true) { GLRenderState lines = GLRenderState.Lines(1); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(-100, -0, 100), new Vector3(10, 0, 0), 21), new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed }) ); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(100, -0, -100), new Vector3(0, 0, 10), 21), new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed })); } #endregion #region Coloured triangles if (true) { GLRenderState rc = GLRenderState.Tri(); rc.CullFace = false; rObjects.Add(items.Shader("COSOT"), "scopen", GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Triangles, rc, GLCubeObjectFactory.CreateSolidCubeFromTriangles(5f), new Color4[] { Color4.Red, Color4.Green, Color4.Blue, Color4.White, Color4.Cyan, Color4.Orange }, new GLRenderDataTranslationRotation(new Vector3(10, 0, 20)) )); } #endregion var sunvertex = new GLPLVertexShaderModelCoordWorldAutoscale(new Color[] { Color.FromArgb(255, 220, 220, 10), Color.FromArgb(255, 0, 0, 0) }); items.Add(sunvertex); var sunshader = new GLShaderPipeline(sunvertex, new GLPLStarSurfaceFragmentShader()); items.Add(sunshader); var shapebuf = new GLBuffer(); items.Add(shapebuf); var shape = GLSphereObjectFactory.CreateSphereFromTriangles(1, 0.5f); shapebuf.AllocateFill(shape); GLStorageBlock block = new GLStorageBlock(20); findshader = items.NewShaderPipeline(null, sunvertex, null, null, new GLPLGeoShaderFindTriangles(block, 16), null, null, null); int texunitspergroup = 16; // opengl minimum texture units per frag shader //var textshader = new GLShaderPipeline(new GLPLVertexShaderQuadTextureWithMatrixTranslation(), new GLPLFragmentShaderTexture2DIndexedMulti(0,0,true, texunitspergroup)); var textshader = new TextShader(texunitspergroup); items.Add(textshader); Font fnt = new Font("MS sans serif", 16f); if (true) { int maxstars = 1000; // this is an aspriation, depends on fragmentation of the system dataindirectbuffer = new GLVertexBufferIndirect(items, maxstars * (GLBuffer.Vec4size + GLBuffer.Mat4size), GLBuffer.WriteIndirectArrayStride * 100, true); var textarray = new GLTexture2DArray(128, 32, maxstars, SizedInternalFormat.Rgba8); int SectorSize = 10; { Vector3 pos = new Vector3(-20, 0, -15); Vector4[] array = new Vector4[10]; Random rnd = new Random(23); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); } dataindirectbuffer.Fill(array, 0, array.Length, 0, shape.Length, 0, array.Length, -1); Matrix4[] matrix = new Matrix4[array.Length]; for (int i = 0; i < array.Length; i++) { int imgpos = textarray.DepthIndex; textarray.DrawText("A" + i, fnt, Color.White, Color.Blue, -1); var mat = GLPLVertexShaderMatrixQuadTexture.CreateMatrix(new Vector3(array[i].X, array[i].Y + 0.6f, array[i].Z), new Vector3(1, 0, 0.2f), new Vector3(-90F.Radians(), 0, 0), imagepos: imgpos); matrix[i] = mat; } dataindirectbuffer.Vertex.AlignMat4(); // instancing counts in mat4 sizes (mat4 0 @0, mat4 1 @ 64 etc) so align to it dataindirectbuffer.Fill(matrix, 0, matrix.Length, 1, 4, 0, array.Length, -1); } if (true) { Vector3 pos = new Vector3(-20, 0, 0); Vector4[] array = new Vector4[5]; Random rnd = new Random(23); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); } dataindirectbuffer.Fill(array, 0, array.Length, 0, shape.Length, 0, array.Length, -1); } if (true) { Vector3 pos = new Vector3(-20, 0, 15); Vector4[] array = new Vector4[10]; Random rnd = new Random(23); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); } dataindirectbuffer.Fill(array, 0, array.Length, 0, shape.Length, 0, array.Length, -1); Matrix4[] matrix = new Matrix4[array.Length]; for (int i = 0; i < array.Length; i++) { int imgpos = textarray.DepthIndex; textarray.DrawText("C" + i, fnt, Color.White, Color.Red, -1); var mat = GLPLVertexShaderMatrixQuadTexture.CreateMatrix(new Vector3(array[i].X, array[i].Y + 0.6f, array[i].Z), new Vector3(1, 0, 0.2f), new Vector3(-90F.Radians(), 0, 0), imagepos: imgpos); matrix[i] = mat; } dataindirectbuffer.Vertex.AlignMat4(); // instancing countis in mat4 sizes (mat4 0 @0, mat4 1 @ 64 etc) so align to it dataindirectbuffer.Fill(matrix, 0, matrix.Length, 1, 4, 0, array.Length, -1); } int[] indirectints0 = dataindirectbuffer.Indirects[0].ReadInts(0, 12); int[] indirectints1 = dataindirectbuffer.Indirects[1].ReadInts(0, 4); float[] worldpos = dataindirectbuffer.Vertex.ReadFloats(0, 3 * 2 * 4); if (true) { GLRenderState rt = GLRenderState.Tri(); // render is triangles, with no depth test so we always appear rt.DepthTest = true; rt.DepthClamp = true; var renderer = GLRenderableItem.CreateVector4Vector4(items, PrimitiveType.Triangles, rt, shapebuf, 0, 0, // binding 0 is shapebuf, offset 0, no draw count dataindirectbuffer.Vertex, 0, // binding 1 is vertex's world positions, offset 0 null, 0, 1); // no ic, second divisor 1 renderer.IndirectBuffer = dataindirectbuffer.Indirects[0]; renderer.BaseIndexOffset = 0; // offset in bytes where commands are stored renderer.DrawCount = 3; renderer.MultiDrawCountStride = GLBuffer.WriteIndirectArrayStride; rObjects.Add(sunshader, "sunshader", renderer); } if (true) { var rc = GLRenderState.Quads(); rc.CullFace = true; rc.DepthTest = true; rc.ClipDistanceEnable = 1; // we are going to cull primitives which are deleted var renderer = GLRenderableItem.CreateMatrix4(items, PrimitiveType.Quads, rc, dataindirectbuffer.Vertex, 0, 0, //attach buffer with matrices, no draw count new GLRenderDataTexture(textarray, 0), 0, 1); //no ic, and matrix divide so 1 matrix per vertex set renderer.IndirectBuffer = dataindirectbuffer.Indirects[1]; renderer.BaseIndexOffset = 0; // offset in bytes where commands are stored renderer.DrawCount = 2; renderer.MultiDrawCountStride = GLBuffer.WriteIndirectArrayStride; rObjects.Add(textshader, "textshader", renderer); } } if (true) { GLRenderState starrc = GLRenderState.Tri(); // render is triangles, with no depth test so we always appear starrc.DepthTest = true; starrc.DepthClamp = true; var textrc = GLRenderState.Quads(); textrc.DepthTest = true; textrc.ClipDistanceEnable = 1; // we are going to cull primitives which are deleted sl = new GLObjectsWithLabels(); var ris = sl.Create(texunitspergroup, 50, 50, shapebuf, shape.Length, starrc, PrimitiveType.Triangles, new Size(128, 32), textrc, SizedInternalFormat.Rgba8, 3); rObjects.Add(sunshader, "SLsunshade", ris.Item1); rObjects.Add(textshader, "SLtextshade", ris.Item2); items.Add(sl); int SectorSize = 10; { Vector3 pos = new Vector3(0, 0, -15); Vector4[] array = new Vector4[10]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "A.r" + i; } var mats = GLPLVertexShaderMatrixQuadTexture.CreateMatrices(array, new Vector3(0, 0.6f, 0), new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false); var bmps = GLOFC.Utils.BitMapHelpers.DrawTextIntoFixedSizeBitmaps(sl.LabelSize, text, fnt, System.Drawing.Text.TextRenderingHint.ClearTypeGridFit, Color.White, Color.DarkBlue, 0.5f); List <GLObjectsWithLabels.BlockRef> bref = new List <GLObjectsWithLabels.BlockRef>(); sl.Add(array, mats, bmps, bref); GLOFC.Utils.BitMapHelpers.Dispose(bmps); } { Vector3 pos = new Vector3(0, 0, 0); Vector4[] array = new Vector4[20]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "B." + i; } List <GLObjectsWithLabels.BlockRef> bref = new List <GLObjectsWithLabels.BlockRef>(); sl.Add(array, text, fnt, Color.White, Color.DarkBlue, new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false, null, 0.5f, new Vector3(0, 0.6f, 0), bref); } { Vector3 pos = new Vector3(0, 0, 15); Vector4[] array = new Vector4[10]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "C." + i; } List <GLObjectsWithLabels.BlockRef> bref = new List <GLObjectsWithLabels.BlockRef>(); sl.Add(array, text, fnt, Color.White, Color.DarkBlue, new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false, null, 0.5f, new Vector3(0, 0.6f, 0), bref); } System.Diagnostics.Debug.WriteLine($"Sets {sl.Blocks} Removed {sl.BlocksRemoved}"); } // Sets of.. if (true) { GLRenderState starrc = GLRenderState.Tri(); // render is triangles, with no depth test so we always appear starrc.DepthTest = true; starrc.DepthClamp = true; var textrc = GLRenderState.Quads(); textrc.DepthTest = true; textrc.ClipDistanceEnable = 1; // we are going to cull primitives which are deleted slset = new GLSetOfObjectsWithLabels("SLSet", rObjects, true ? 4 : texunitspergroup, 50, 10, sunshader, shapebuf, shape.Length, starrc, PrimitiveType.Triangles, textshader, new Size(128, 32), textrc, SizedInternalFormat.Rgba8, 3); items.Add(slset); int SectorSize = 10; { Vector3 pos = new Vector3(20, 0, -15); Vector4[] array = new Vector4[10]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "S.A.r" + i; } var mats = GLPLVertexShaderMatrixQuadTexture.CreateMatrices(array, new Vector3(0, 0.6f, 0), new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false); var bmps = GLOFC.Utils.BitMapHelpers.DrawTextIntoFixedSizeBitmaps(slset.LabelSize, text, fnt, System.Drawing.Text.TextRenderingHint.ClearTypeGridFit, Color.White, Color.DarkBlue, 0.5f); slset.Add("GA", text, array, mats, bmps); GLOFC.Utils.BitMapHelpers.Dispose(bmps); } { Vector3 pos = new Vector3(20, 0, 0); Vector4[] array = new Vector4[10]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "S.B." + i; } var mats = GLPLVertexShaderMatrixQuadTexture.CreateMatrices(array, new Vector3(0, 0.6f, 0), new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false); var bmps = GLOFC.Utils.BitMapHelpers.DrawTextIntoFixedSizeBitmaps(slset.LabelSize, text, fnt, System.Drawing.Text.TextRenderingHint.ClearTypeGridFit, Color.White, Color.DarkBlue, 0.5f); slset.Add("GB", text, array, mats, bmps); GLOFC.Utils.BitMapHelpers.Dispose(bmps); } { Vector3 pos = new Vector3(20, 0, 15); Vector4[] array = new Vector4[10]; string[] text = new string[array.Length]; Random rnd = new Random(31); for (int i = 0; i < array.Length; i++) { array[i] = new Vector4(pos.X + rnd.Next(SectorSize), pos.Y + rnd.Next(SectorSize), pos.Z + rnd.Next(SectorSize), 0); text[i] = "S.C." + i; } var mats = GLPLVertexShaderMatrixQuadTexture.CreateMatrices(array, new Vector3(0, 0.6f, 0), new Vector3(2f, 0, 0.4f), new Vector3(-90F.Radians(), 0, 0), true, false); var bmps = GLOFC.Utils.BitMapHelpers.DrawTextIntoFixedSizeBitmaps(slset.LabelSize, text, fnt, System.Drawing.Text.TextRenderingHint.ClearTypeGridFit, Color.White, Color.DarkBlue, 0.5f); slset.Add("GC", text, array, mats, bmps); GLOFC.Utils.BitMapHelpers.Dispose(bmps); } } #region Matrix Calc Uniform items.Add(new GLMatrixCalcUniformBlock(), "MCUB"); // def binding of 0 #endregion }