private void linkProgram(OpenGL gl, ActiveProgramValues programValues, uint glProgramHandle) { //TODO add debug assertions in case a shader fails to compile, or a program fails to link var glShaderHandles = new List<uint>(programValues.AttachedShaderValues.Count); foreach (var shaderValues in programValues.AttachedShaderValues) { var glShaderHandle = gl.CreateShader(shaderValues.Stage.GlEnumValue()); glShaderHandles.Add(glShaderHandle); gl.ShaderSource(glShaderHandle, shaderValues.Source); gl.CompileShader(glShaderHandle); gl.AttachShader(glProgramHandle, glShaderHandle); } gl.LinkProgram(glProgramHandle); foreach (var glShaderHandle in glShaderHandles) { gl.DetachShader(glProgramHandle, glShaderHandle); gl.DeleteShader(glShaderHandle); } }
/// <summary> /// Creates the program and attaches all the shaders to it. /// </summary> /// <param name="gl"></param> private void CreateProgram(OpenGL gl) { ShaderProgramId = gl.CreateProgram(); foreach (var shader in ShaderIds) { gl.AttachShader(ShaderProgramId, shader.Value); } }
private void PlaySound() { try { gl.GenVertexArrays(1, vao); gl.BindVertexArray(vao[0]); gl.GenBuffers(1, vbo); float[] vertices = new float[] { -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f }; gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vbo[0]); unsafe { fixed(float *verts = vertices) { var ptr = new IntPtr(verts); // fill the buffer to the currently binded buffer gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices.Length * sizeof(float), ptr, OpenGL.GL_STATIC_DRAW); } } // Create the shader program. var vertexShaderSource = ManifestResourceLoader.LoadTextFile("SoundShader.vert"); var fragmentShaderSource = ManifestResourceLoader.LoadTextFile("SoundShader.frag"); vertexShader = gl.CreateShader(OpenGL.GL_VERTEX_SHADER); gl.ShaderSource(vertexShader, vertexShaderSource); gl.CompileShader(vertexShader); // Create and compile the fragment shader fragmentShader = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER); gl.ShaderSource(fragmentShader, fragmentShaderSource); gl.CompileShader(fragmentShader); // Link the vertex and fragment shader into a shader program shaderProgram = gl.CreateProgram(); gl.AttachShader(shaderProgram, vertexShader); gl.AttachShader(shaderProgram, fragmentShader); gl.BindFragDataLocation(shaderProgram, (uint)0, "gl_FragColor"); gl.LinkProgram(shaderProgram); int[] infoLength = new int[] { 0 }; int bufSize = infoLength[0]; // Get the compile info. StringBuilder il = new StringBuilder(bufSize); gl.GetProgramInfoLog(shaderProgram, bufSize, IntPtr.Zero, il); gl.UseProgram(shaderProgram); posAttrib = gl.GetAttribLocation(shaderProgram, "position"); gl.EnableVertexAttribArray((uint)posAttrib); gl.VertexAttribPointer((uint)posAttrib, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0)); timeLoc = gl.GetUniformLocation(shaderProgram, "iGlobalTime"); sampleLoc = gl.GetUniformLocation(shaderProgram, "Spectrum"); waveLoc = gl.GetUniformLocation(shaderProgram, "Wavedata"); int resLoc = gl.GetUniformLocation(shaderProgram, "iResolution"); gl.Uniform3(resLoc, (float)this.openGLControl1.Width, (float)this.openGLControl1.Height, (float)(this.openGLControl1.Width * this.openGLControl1.Height)); Thread thread = new Thread(() => PlaySoundT(((USound)selectedObject).SoundBuffer)); thread.Start(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace.ToString()); } }