private void ApplyStateCore(GraphicsContext ctx, ShaderProgram program, BlendState currentState) { if (Enabled) { // Enable blending if (currentState.Enabled == false) { Gl.Enable(EnableCap.Blend); } // Set blending equation if (ctx.Extensions.BlendMinmax_EXT) { if (EquationSeparated) { if (currentState.RgbEquation != RgbEquation || currentState.AlphaEquation != AlphaEquation) { Gl.BlendEquationSeparate(RgbEquation, AlphaEquation); } } else { if (currentState.RgbEquation != RgbEquation) { Gl.BlendEquation(RgbEquation); } } } // Set blending function if (FunctionSeparated) { if (currentState._RgbSrcFactor != _RgbSrcFactor || currentState._RgbDstFactor != _RgbDstFactor || currentState._AlphaSrcFactor != _AlphaSrcFactor || currentState._AlphaDstFactor != _AlphaDstFactor) { Gl.BlendFuncSeparate(_RgbSrcFactor, _RgbDstFactor, _AlphaSrcFactor, _AlphaDstFactor); } } else { if (currentState._RgbSrcFactor != _RgbSrcFactor || currentState._RgbDstFactor != _RgbDstFactor) { Gl.BlendFunc(_RgbSrcFactor, _RgbDstFactor); } } // Set blend color, if required if (RequiresConstColor(_RgbSrcFactor) || RequiresConstColor(_AlphaSrcFactor) || RequiresConstColor(_RgbDstFactor) || RequiresConstColor(_AlphaDstFactor)) { Gl.BlendColor(_BlendColor.r, _BlendColor.g, _BlendColor.b, _BlendColor.a); } } else { // Disable blending if (currentState.Enabled == true) { Gl.Disable(EnableCap.Blend); } } }
private void renderGlToBuffer(int frameIdx = -1) { ParticleEditModel md = MainModel.ins.particleEditModel; if (md == null || md.width == 0 || md.height == 0) { return; } float x = 0; float y = 0; int w = md.width; int h = md.height; int time = renderTime; if (frameIdx >= 0) { time = (int)((float)frameIdx / md.fps * 1000); } //int vpx = 0; //int vpy = 0; //int vpw = w; //int vph = h; //Gl.Viewport(vpx, vpy, vpw, vph); //Gl.Clear(ClearBufferMask.ColorBufferBit); //Gl.MatrixMode(MatrixMode.Projection); //Gl.LoadIdentity(); //Gl.Ortho(0.0, w, 0.0, h, 0.0, 1.0); //Gl.MatrixMode(MatrixMode.Modelview); //Gl.LoadIdentity(); Gl.BlendFuncSeparate(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha, BlendingFactor.One, BlendingFactor.OneMinusSrcAlpha); Gl.BlendEquation(BlendEquationMode.FuncAdd); renderInitMatrix(w, h); //mask if (md.isMaskBox) { Gl.Enable(EnableCap.ScissorTest); Gl.Scissor((int)x, (int)y, md.width, md.height); } //emitter for (int i = 0; i < lstEmitter.Count; ++i) { lstEmitter[i].setStartPos((int)x, (int)y); lstEmitter[i].render(bufferMVP, time); } //mask if (md.isMaskBox) { Gl.Disable(EnableCap.ScissorTest); } }
/// <summary> /// Set ShaderProgram state. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> which has defined the shader program <paramref name="sProgram"/>. /// </param> /// <param name="sProgram"> /// The <see cref="ShaderProgram"/> which has the state set. /// </param> public override void ApplyState(GraphicsContext ctx, ShaderProgram sProgram) { if (ctx == null) { throw new ArgumentNullException("ctx"); } BlendState currentState = null; // (BlendState)ctx.CurrentState[StateId]; if (Enabled) { // Enable blending if (currentState == null || !currentState.Enabled) { Gl.Enable(EnableCap.Blend); } // Set blending equation if (ctx.Caps.GlExtensions.BlendMinmax_EXT) { if (EquationSeparated) { Gl.BlendEquationSeparate(RgbEquation, AlphaEquation); } else { Gl.BlendEquation((int)RgbEquation); } } // Set blending function if (FunctionSeparated) { Gl.BlendFuncSeparate((int)mRgbSrcFactor, (int)mRgbDstFactor, (int)mAlphaSrcFactor, (int)mAlphaDstFactor); } else { Gl.BlendFunc(mRgbSrcFactor, mRgbDstFactor); } // Set blend color, if required if (RequiresConstColor(mRgbSrcFactor) || RequiresConstColor(mAlphaSrcFactor) || RequiresConstColor(mRgbDstFactor) || RequiresConstColor(mAlphaDstFactor)) { Gl.BlendColor(mBlendColor.Red, mBlendColor.Green, mBlendColor.Blue, mBlendColor.Alpha); } } else { // Disable blending if (currentState == null || currentState.Enabled) { Gl.Disable(EnableCap.Blend); } } }
private void ApplyStateCore(GraphicsContext ctx, ShaderProgram program) { if (Enabled) { // Enable blending Gl.Enable(EnableCap.Blend); // Set blending equation if (ctx.Extensions.BlendMinmax_EXT) { if (EquationSeparated) { Gl.BlendEquationSeparate(RgbEquation, AlphaEquation); } else { Gl.BlendEquation((int)RgbEquation); } } // Set blending function if (FunctionSeparated) { Gl.BlendFuncSeparate((int)_RgbSrcFactor, (int)_RgbDstFactor, (int)_AlphaSrcFactor, (int)_AlphaDstFactor); } else { Gl.BlendFunc(_RgbSrcFactor, _RgbDstFactor); } // Set blend color, if required if (RequiresConstColor(_RgbSrcFactor) || RequiresConstColor(_AlphaSrcFactor) || RequiresConstColor(_RgbDstFactor) || RequiresConstColor(_AlphaDstFactor)) { Gl.BlendColor(_BlendColor.Red, _BlendColor.Green, _BlendColor.Blue, _BlendColor.Alpha); } } else { // Disable blending Gl.Disable(EnableCap.Blend); } }
private void SetupRenderState(ImDrawDataPtr drawData) { // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill Gl.Enable(EnableCap.Blend); Gl.BlendEquation(BlendEquationMode.FuncAdd); Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); Gl.Disable(EnableCap.CullFace); Gl.Disable(EnableCap.DepthTest); Gl.Enable(EnableCap.ScissorTest); Gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); // Setup viewport, orthographic projection matrix // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. Gl.Viewport(0, 0, (int)(drawData.DisplaySize.X * drawData.FramebufferScale.X), (int)(drawData.DisplaySize.Y * drawData.FramebufferScale.Y)); var L = drawData.DisplayPos.X; var R = drawData.DisplayPos.X + drawData.DisplaySize.X; var T = drawData.DisplayPos.Y; var B = drawData.DisplayPos.Y + drawData.DisplaySize.Y; var ortho_projection = new float[] { 2f / (R - L), 0, 0, 0, 0, 2f / (T - B), 0, 0, 0, 0, -1f, 0, (R + L) / (L - R), (T + B) / (B - T), 0, 1f }; Gl.UseProgram(_shaderHandle); Gl.Uniform1(_attribLocationTex, 0); Gl.UniformMatrix4(_attribLocationProjMtx, false, ortho_projection); Gl.BindVertexArray(_vertexArrayObject); // Bind vertex/index buffers and setup attributes for ImDrawVert Gl.BindBuffer(BufferTarget.ArrayBuffer, _vboHandle); Gl.BindBuffer(BufferTarget.ElementArrayBuffer, _elementsHandle); Gl.EnableVertexAttribArray((uint)_attribLocationVtxPos); Gl.EnableVertexAttribArray((uint)_attribLocationVtxUV); Gl.EnableVertexAttribArray((uint)_attribLocationVtxColor); Gl.VertexAttribPointer((uint)_attribLocationVtxPos, 2, VertexAttribType.Float, false, Unsafe.SizeOf <ImDrawVert>(), Marshal.OffsetOf(typeof(ImDrawVert), "pos")); Gl.VertexAttribPointer((uint)_attribLocationVtxUV, 2, VertexAttribType.Float, false, Unsafe.SizeOf <ImDrawVert>(), Marshal.OffsetOf(typeof(ImDrawVert), "uv")); Gl.VertexAttribPointer((uint)_attribLocationVtxColor, 4, VertexAttribType.UnsignedByte, true, Unsafe.SizeOf <ImDrawVert>(), Marshal.OffsetOf(typeof(ImDrawVert), "col")); }
private void RenderImGui(ImDrawDataPtr drawData) { // TODO: use abstract graphics impl Gl.BlendEquation(BlendEquationMode.FuncAdd); Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); Gl.Disable(EnableCap.CullFace); Gl.Disable(EnableCap.DepthTest); Gl.Enable(EnableCap.ScissorTest); io.DisplaySize = new Vector2(Constants.windowWidth, Constants.windowHeight); var projectionMatrix = Matrix4x4f.Ortho2D(0f, io.DisplaySize.X, io.DisplaySize.Y, 0.0f); imguiShader.Use(); imguiShader.SetInt("albedoTexture", 0); imguiShader.SetMatrix("projection", projectionMatrix); Gl.BindVertexArray(vao); Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo); Gl.BindBuffer(BufferTarget.ElementArrayBuffer, ebo); Gl.EnableVertexAttribArray(0); // Position Gl.EnableVertexAttribArray(1); // UV Coord Gl.EnableVertexAttribArray(2); // Colors Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 20, (IntPtr)0); Gl.VertexAttribPointer(1, 2, VertexAttribType.Float, false, 20, (IntPtr)(2 * sizeof(float))); Gl.VertexAttribPointer(2, 4, VertexAttribType.UnsignedByte, true, 20, (IntPtr)(4 * sizeof(float))); var clipOffset = drawData.DisplayPos; var clipScale = drawData.FramebufferScale; for (var commandListIndex = 0; commandListIndex < drawData.CmdListsCount; commandListIndex++) { int indexOffset = 0; var commandList = drawData.CmdListsRange[commandListIndex]; Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(commandList.VtxBuffer.Size * 20), commandList.VtxBuffer.Data, BufferUsage.DynamicDraw); Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)(commandList.IdxBuffer.Size * 2), commandList.IdxBuffer.Data, BufferUsage.DynamicDraw); for (var commandIndex = 0; commandIndex < commandList.CmdBuffer.Size; commandIndex++) { var currentCommand = commandList.CmdBuffer[commandIndex]; var clipBounds = new Vector4f( (currentCommand.ClipRect.X - clipOffset.X) * clipScale.X, (currentCommand.ClipRect.Y - clipOffset.Y) * clipScale.Y, (currentCommand.ClipRect.Z - clipOffset.X) * clipScale.X, (currentCommand.ClipRect.W - clipOffset.Y) * clipScale.Y ); Gl.Scissor((int)clipBounds.x, (int)(windowSize.Y - clipBounds.w), (int)(clipBounds.z - clipBounds.x), (int)(clipBounds.w - clipBounds.y)); Gl.ActiveTexture(TextureUnit.Texture0); Gl.BindTexture(TextureTarget.Texture2d, defaultFontTexture.Id); Gl.DrawElementsBaseVertex(PrimitiveType.Triangles, (int)currentCommand.ElemCount, DrawElementsType.UnsignedShort, (IntPtr)(indexOffset * 2), 0); indexOffset += (int)currentCommand.ElemCount; } } // TODO: Move these elsewhere to prevent later confusion Gl.Disable(EnableCap.ScissorTest); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.CullFace); }