public BackgroundShader(OpenGL gl) : base(gl) { _program = new ShaderProgram(); var vertex = new VertexShader(); var fragment = new FragmentShader(); vertex.CreateInContext(gl); vertex.SetSource(File.ReadAllText("Shaders/Background.vert.glsl")); vertex.Compile(); fragment.CreateInContext(gl); fragment.SetSource(File.ReadAllText("Shaders/Background.frag.glsl")); fragment.Compile(); _program.CreateInContext(gl); _program.AttachShader(vertex); _program.AttachShader(fragment); _program.Link(); Debug.WriteLine(_program.InfoLog); foreach (var attachedShader in _program.AttachedShaders) { Debug.WriteLine(attachedShader.InfoLog); } }
private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args) { OpenGL gl = args.OpenGL; gl.Enable(OpenGL.GL_DEPTH_TEST); float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f }; float[] light0pos = new float[] { 0.0f, 5.0f, 10.0f, 1.0f }; float[] light0ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; float[] light0diffuse = new float[] { 0.3f, 0.3f, 0.3f, 1.0f }; float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f }; float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular); gl.Enable(OpenGL.GL_LIGHTING); gl.Enable(OpenGL.GL_LIGHT0); gl.ShadeModel(OpenGL.GL_SMOOTH); // Create a vertex shader. VertexShader vertexShader = new VertexShader(); vertexShader.CreateInContext(gl); vertexShader.SetSource( "void main()" + Environment.NewLine + "{" + Environment.NewLine + "gl_Position = ftransform();" + Environment.NewLine + "}" + Environment.NewLine); // Create a fragment shader. FragmentShader fragmentShader = new FragmentShader(); fragmentShader.CreateInContext(gl); fragmentShader.SetSource( "void main()" + Environment.NewLine + "{" + Environment.NewLine + "gl_FragColor = vec4(0.4,0.4,0.8,1.0);" + Environment.NewLine + "}" + Environment.NewLine); // Compile them both. vertexShader.Compile(); fragmentShader.Compile(); // Build a program. program.CreateInContext(gl); // Attach the shaders. program.AttachShader(vertexShader); program.AttachShader(fragmentShader); program.Link(); }
public OctreeShader(OpenGL gl) : base(gl) { _program = new ShaderProgram(); var vertex = new VertexShader(); var fragment = new FragmentShader(); var geo = new GeometryShader(); vertex.CreateInContext(gl); vertex.SetSource(File.ReadAllText("Shaders/Octree.vert.glsl")); vertex.Compile(); fragment.CreateInContext(gl); fragment.SetSource(File.ReadAllText("Shaders/Octree.frag.glsl")); fragment.Compile(); geo.CreateInContext(gl); geo.SetSource(File.ReadAllText("Shaders/Octree.geo.glsl")); geo.Compile(); _program.CreateInContext(gl); _program.AttachShader(vertex); _program.AttachShader(fragment); _program.AttachShader(geo); _program.Link(); Debug.WriteLine(_program.InfoLog); foreach (var attachedShader in _program.AttachedShaders) { Debug.WriteLine(attachedShader.InfoLog); } _modelLocation = gl.GetUniformLocation(_program.ProgramObject, "Model"); _viewLocation = gl.GetUniformLocation(_program.ProgramObject, "View"); _projectionLocation = gl.GetUniformLocation(_program.ProgramObject, "Projection"); }
public void CreateBasicShader(OpenGL _context) { //ThrowIfNull(_context); // Create a vertex shader. VertexShader vertexShader = new VertexShader(); vertexShader.CreateInContext(_context); vertexShader.SetSource( "void main()" + Environment.NewLine + "{" + Environment.NewLine + "gl_Position = ftransform();" + Environment.NewLine + "}" + Environment.NewLine); // Create a fragment shader. FragmentShader fragmentShader = new FragmentShader(); fragmentShader.CreateInContext(_context); fragmentShader.SetSource( "void main()" + Environment.NewLine + "{" + Environment.NewLine + "gl_FragColor = vec4(0.4,0.4,0.8,1.0);" + Environment.NewLine + "}" + Environment.NewLine); // Compile them both. vertexShader.Compile(); fragmentShader.Compile(); // Build a program. ShaderProgram.CreateInContext(_context); // Attach the shaders. ShaderProgram.AttachShader(vertexShader); ShaderProgram.AttachShader(fragmentShader); ShaderProgram.Link(); }
/// <summary> /// Sets the full shader source. /// </summary> /// <param name="source">The source.</param> public void SetFullShaderSource(string source) { // To compile a full shader, we must have a file like this: // -- Vertex // <a vertex shader> // -- Fragment // <a fragment shader> // Prepare the source code. string vertexShader = string.Empty; string fragmentShader = string.Empty; // Keep track of what we're building, 0 is nothing // 1 is the vertex shader, 2 is the fragment shader. int code = 0; // Go through the source line by line. var lines = source.Split('\n'); foreach (var line in lines) { // If the line is a vertex shader, switch to vertex mode. if (line.StartsWith("--") && line.Contains("Vertex")) { code = 1; } else if (line.StartsWith("--") && line.Contains("Fragment")) { code = 2; } else { // Add the line to the appropriate shader. switch (code) { case 1: vertexShader += line + Environment.NewLine; break; case 2: fragmentShader += line + Environment.NewLine; break; } } } // If we have a vertex shader, build it. if (string.IsNullOrEmpty(vertexShader) == false) { VertexShader shader = new VertexShader(); shader.CreateInContext(CurrentOpenGLContext); shader.SetSource(vertexShader); AttachShader(shader); } if (string.IsNullOrEmpty(fragmentShader) == false) { FragmentShader shader = new FragmentShader(); shader.CreateInContext(CurrentOpenGLContext); shader.SetSource(fragmentShader); AttachShader(shader); } }