public void RenderFrame(FastList <Note> notes, double midiTime, int finalCompositeBuff) { CheckScript(); if (currScript == null || currScript.error) { return; } GL.Enable(EnableCap.Blend); GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.ColorArray); GL.Enable(EnableCap.Texture2D); GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.EnableVertexAttribArray(2); GL.EnableVertexAttribArray(3); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); GL.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.Max); OpenTK.Graphics.OpenGL4.GL.BlendEquationSeparate(OpenTK.Graphics.OpenGL4.BlendEquationMode.FuncAdd, OpenTK.Graphics.OpenGL4.BlendEquationMode.FuncAdd); OpenTK.Graphics.OpenGL4.GL.BlendFuncSeparate(OpenTK.Graphics.OpenGL4.BlendingFactorSrc.SrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorDest.OneMinusSrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorSrc.One, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One); GL.BindFramebuffer(FramebufferTarget.Framebuffer, finalCompositeBuff); GL.Viewport(0, 0, renderSettings.width, renderSettings.height); GL.Clear(ClearBufferMask.ColorBufferBit); #region Vars for (int i = 0; i < 12; i++) { activeTexIds[i] = -1; } currentShader = TextureShaders.Normal; currentBlendFunc = BlendFunc.Mix; #endregion GL.UseProgram(quadShader); currentShader = TextureShaders.Normal; var options = GetRenderOptions(midiTime); CopyScriptValues(); currScript.instance.Render((IEnumerable <Note>)notes, options); CopyScriptValues(); FlushQuadBuffer(false); OpenTK.Graphics.OpenGL4.GL.BlendEquationSeparate(OpenTK.Graphics.OpenGL4.BlendEquationMode.FuncAdd, OpenTK.Graphics.OpenGL4.BlendEquationMode.FuncAdd); GL.ActiveTexture(TextureUnit.Texture0); GL.Disable(EnableCap.Blend); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.ColorArray); GL.Disable(EnableCap.Texture2D); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); GL.DisableVertexAttribArray(2); GL.DisableVertexAttribArray(3); }
public static void SelectTextureShader(TextureShaders shader) => selectTexShader(shader);
public void Init() { quadShader = MakeShader(quadShaderVert, quadShaderFrag); inverseQuadShader = MakeShader(quadShaderVert, invertQuadShaderFrag); evenquadShader = MakeShader(quadShaderVert, evenQuadShaderFrag); int loc; int[] samplers = new int[12]; for (int i = 0; i < 12; i++) { samplers[i] = i; } GL.UseProgram(quadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(quadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } GL.UseProgram(inverseQuadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(inverseQuadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } GL.UseProgram(evenquadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(evenquadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } quadVertexbuff = new double[quadBufferLength * 8]; quadColorbuff = new float[quadBufferLength * 16]; quadUVbuff = new double[quadBufferLength * 8]; quadTexIDbuff = new float[quadBufferLength * 4]; GL.GenBuffers(1, out vertexBufferID); GL.GenBuffers(1, out colorBufferID); GL.GenBuffers(1, out uvBufferID); GL.GenBuffers(1, out texIDBufferID); for (uint i = 0; i < indexes.Length / 6; i++) { indexes[i * 6 + 0] = i * 4 + 0; indexes[i * 6 + 1] = i * 4 + 1; indexes[i * 6 + 2] = i * 4 + 3; indexes[i * 6 + 3] = i * 4 + 1; indexes[i * 6 + 4] = i * 4 + 3; indexes[i * 6 + 5] = i * 4 + 2; } GL.GenBuffers(1, out indexBufferId); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId); GL.BufferData( BufferTarget.ElementArrayBuffer, (IntPtr)(indexes.Length * 4), indexes, BufferUsageHint.StaticDraw); if (currScript != null) { try { LoadScript(currScript); } catch { currScript = null; lastScriptChangeTime = 0; } } CheckScript(); IO.renderQuad = RenderQuad; IO.renderShape = RenderShape; IO.forceFlush = () => FlushQuadBuffer(false); IO.selectTexShader = (s) => { if (currentShader != s) { FlushQuadBuffer(false); } if (s == TextureShaders.Normal) { GL.UseProgram(quadShader); } if (s == TextureShaders.Inverted) { GL.UseProgram(inverseQuadShader); } if (s == TextureShaders.Hybrid) { GL.UseProgram(evenquadShader); } currentShader = s; }; IO.setBlendFunc += (f) => { if (currentBlendFunc != f) { FlushQuadBuffer(false); } if (f == BlendFunc.Mix) { OpenTK.Graphics.OpenGL4.GL.BlendFuncSeparate(OpenTK.Graphics.OpenGL4.BlendingFactorSrc.SrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorDest.OneMinusSrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorSrc.One, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One); } if (f == BlendFunc.Add) { OpenTK.Graphics.OpenGL4.GL.BlendFuncSeparate(OpenTK.Graphics.OpenGL4.BlendingFactorSrc.SrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One, OpenTK.Graphics.OpenGL4.BlendingFactorSrc.One, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One); } currentBlendFunc = f; }; Initialized = true; Console.WriteLine("Initialised ScriptedRender"); }
public void Init() { quadShader = MakeShader(quadShaderVert, quadShaderFrag); inverseQuadShader = MakeShader(quadShaderVert, invertQuadShaderFrag); evenquadShader = MakeShader(quadShaderVert, evenQuadShaderFrag); int loc; int[] samplers = new int[12]; for (int i = 0; i < 12; i++) { samplers[i] = i; } GL.UseProgram(quadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(quadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } GL.UseProgram(inverseQuadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(inverseQuadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } GL.UseProgram(evenquadShader); for (int i = 0; i < 12; i++) { loc = GL.GetUniformLocation(evenquadShader, "textureSampler" + (i + 1)); GL.Uniform1(loc, i); } quadVertexbuff = new double[quadBufferLength * 8]; quadColorbuff = new float[quadBufferLength * 16]; quadUVbuff = new double[quadBufferLength * 8]; quadTexIDbuff = new float[quadBufferLength * 4]; GL.GenBuffers(1, out vertexBufferID); GL.GenBuffers(1, out colorBufferID); GL.GenBuffers(1, out uvBufferID); GL.GenBuffers(1, out texIDBufferID); for (uint i = 0; i < indexes.Length / 6; i++) { indexes[i * 6 + 0] = i * 4 + 0; indexes[i * 6 + 1] = i * 4 + 1; indexes[i * 6 + 2] = i * 4 + 3; indexes[i * 6 + 3] = i * 4 + 1; indexes[i * 6 + 4] = i * 4 + 3; indexes[i * 6 + 5] = i * 4 + 2; } GL.GenBuffers(1, out indexBufferId); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId); GL.BufferData( BufferTarget.ElementArrayBuffer, (IntPtr)(indexes.Length * 4), indexes, BufferUsageHint.StaticDraw); if (currScript != null) { try { LoadScript(currScript); } catch { currScript = null; lastScriptChangeTime = 0; } } CheckScript(); IO.renderQuad = RenderQuad; IO.renderShape = RenderShape; IO.forceFlush = () => FlushQuadBuffer(false); IO.selectTexShader = (s) => { if (currentShader != s) { FlushQuadBuffer(false); } if (s == TextureShaders.Normal) { GL.UseProgram(quadShader); } if (s == TextureShaders.Inverted) { GL.UseProgram(inverseQuadShader); } if (s == TextureShaders.Hybrid) { GL.UseProgram(evenquadShader); } currentShader = s; }; IO.setBlendFunc = (f) => { if (currentBlendFunc != f) { FlushQuadBuffer(false); } if (f == BlendFunc.Mix) { OpenTK.Graphics.OpenGL4.GL.BlendFuncSeparate(OpenTK.Graphics.OpenGL4.BlendingFactorSrc.SrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorDest.OneMinusSrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorSrc.One, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One); } if (f == BlendFunc.Add) { OpenTK.Graphics.OpenGL4.GL.BlendFuncSeparate(OpenTK.Graphics.OpenGL4.BlendingFactorSrc.SrcAlpha, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One, OpenTK.Graphics.OpenGL4.BlendingFactorSrc.One, OpenTK.Graphics.OpenGL4.BlendingFactorDest.One); } currentBlendFunc = f; }; IO.getTextSize = (Font f, string t) => { var bb = f.engine.GetBoundBox(t); return(bb.Width / (double)bb.Height / renderSettings.width * renderSettings.height); }; IO.renderText = (double left, double bottom, double height, Color4 color, Font f, string text) => { if (text.Contains("\n")) { throw new Exception("New line characters not allowed when rendering text (yet)"); } FlushQuadBuffer(false); var size = f.engine.GetBoundBox(text); Matrix4 transform = Matrix4.Identity; transform = Matrix4.Mult(transform, Matrix4.CreateScale(1.0f / renderSettings.width * renderSettings.height / f.fontPixelSize * (float)height, -1.0f / f.fontPixelSize * (float)height, 1.0f)); transform = Matrix4.Mult(transform, Matrix4.CreateTranslation((float)left * 2 - 1, (float)(bottom + height * 0.7) * 2 - 1, 0)); f.engine.Render(text, transform, color); if (currentShader == TextureShaders.Normal) { GL.UseProgram(quadShader); } if (currentShader == TextureShaders.Inverted) { GL.UseProgram(inverseQuadShader); } if (currentShader == TextureShaders.Hybrid) { GL.UseProgram(evenquadShader); } }; Initialized = true; Console.WriteLine("Initialised ScriptedRender"); }