public void SetUniform(String name, params int[] values) { if (ShaderID <= 0) { throw new RenderingException("Can't set uniforms in an invalid program!"); } if (!ShaderCompiled) { throw new RenderingException("Can't set uniforms in a not-yet-compiled program!"); } if (values.Length <= 0) { return; } if (values.Length > 4) { throw new RenderingException("Uniform size larger than 4 is not supported."); } int prog = GL.GetInteger(GetPName.CurrentProgram); GL.UseProgram(ShaderID); try { int uloc = GL.GetUniformLocation(ShaderID, name); if (uloc < 0) { throw new RenderingException("Nonexistent uniform name."); } switch (values.Length) { case 1: GL.Uniform1(uloc, values[0]); break; case 2: GL.Uniform2(uloc, values[0], values[1]); break; case 3: GL.Uniform3(uloc, values[0], values[1], values[2]); break; case 4: GL.Uniform4(uloc, values[0], values[1], values[2], values[3]); break; } RenderingException.FromGLError(); } finally { GL.UseProgram(prog); } }
public void Update() { if ((VBOSize == Vertices.Count) && (Vertices.Count <= 0)) { return; } GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); try { GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOID); if (VBOSize != Vertices.Count) // need to create new buffer { unsafe { Vertex[] Mem = Vertices.ToArray(); fixed(Vertex *MemPtr = Mem) { GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(Vertices.Count * Vertex.StructSize), (IntPtr)MemPtr, BufferUsageHint.StreamCopy); } } RenderingException.FromGLError(); VBOSize = Vertices.Count; } else // size matches, just update the items (no need to create new buffer) { IntPtr VMemIntPtr = GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly); RenderingException.FromGLError(); unsafe { Vertex *VMemPtr = (Vertex *)VMemIntPtr.ToPointer(); for (int i = 0; i < Vertices.Count; i++) { (*(VMemPtr + i)) = Vertices[i]; } } GL.UnmapBuffer(BufferTarget.ArrayBuffer); } } finally { GL.PopClientAttrib(); } }
public void AddShader(ShaderType type, String code) { if (ShaderID <= 0) { throw new RenderingException("Can't add shaders to an invalid program!"); } if (ShaderCompiled) { throw new RenderingException("Can't add shaders to an already compiled program!"); } int shader = GL.CreateShader(type); try { if (shader <= 0) { throw new RenderingException("Unable to generate Shader Object."); } GL.ShaderSource(shader, code); GL.CompileShader(shader); int shader_status; GL.GetShader(shader, ShaderParameter.CompileStatus, out shader_status); if (shader_status != 1) { String shader_infolog = GL.GetShaderInfoLog(shader); String msg = "Unable to compile shader:" + Environment.NewLine + shader_infolog; throw new RenderingException(msg); } GL.AttachShader(ShaderID, shader); } finally { if (shader > 0) { GL.DeleteShader(shader); } } RenderingException.FromGLError(); }