Exemplo n.º 1
1
        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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create in the context of the supplied OpenGL instance.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void CreateInContext(OpenGL gl)
        {
            //  Create the fragment shader.
            ShaderObject = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER);

            //  Store the current context.
            CurrentOpenGLContext = gl;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create in the context of the supplied OpenGL instance.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void CreateInContext(OpenGL gl)
        {
            //  Create the vertex shader.
            ShaderObject = gl.CreateShader(OpenGL.GL_VERTEX_SHADER);

            //  Store the current context.
            CurrentOpenGLContext = gl;
        }
Exemplo n.º 4
0
 public uint CreateShader(OpenGL gl, string source, uint mode)
 {
     uint shaderId = gl.CreateShader(mode);
     gl.ShaderSource(shaderId, source);
     gl.CompileShader(shaderId);
     StringBuilder glError = new StringBuilder();
     gl.GetShaderInfoLog(shaderId, 1000, new IntPtr(), glError);
     return shaderId;
 }
Exemplo n.º 5
0
        public Shader(OpenGL gl, uint shaderType, string name, string source)
        {
            Name = name;
            Source = source;

            ID = gl.CreateShader(shaderType);
            gl.ShaderSource(ID, Source);
            gl.CompileShader(ID);
        }
Exemplo n.º 6
0
 public override void CreateInContext(OpenGL gl)
 {
     ShaderObject = gl.CreateShader(0x8DD9);
     CurrentOpenGLContext = gl;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a shader.
 /// </summary>
 /// <param name="gl"></param>
 /// <param name="type"></param>
 /// <param name="shaderCode"></param>
 private void CreateShader(OpenGL gl, ShaderTypes type, string shaderCode)
 {
     var id = gl.CreateShader((uint)type);
     gl.ShaderSource(id, shaderCode);
     gl.CompileShader(id);
     ShaderIds.Add(type, id);
 }
Exemplo n.º 8
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());
            }
        }