Esempio n. 1
0
        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());
            }
        }