public FlatShader(OpenGL gl) : base(gl) { _program = new ShaderProgram(); var vertex = new VertexShader(); var fragment = new FragmentShader(); vertex.CreateInContext(gl); vertex.SetSource(File.ReadAllText("Shaders/Flat.vert.glsl")); vertex.Compile(); fragment.CreateInContext(gl); fragment.SetSource(File.ReadAllText("Shaders/Flat.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); } }
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"); }
private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.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 static ShaderProgram CreateSpriteShaderProgram(RenderContext renderContext, string label = "Sprite ShaderProgram") { VertexShader vertexShader = new VertexShader(renderContext, @"#version 330 core layout (location = 0) in vec2 aPos; layout (location = 1) in vec2 aTexCoord; layout (location = 2) in vec4 aColor; out vec2 TexCoord; out vec4 Color; uniform mat4 projection; uniform mat4 transform; void main() { gl_Position = projection * transform * vec4(aPos, 0.0, 1.0); TexCoord = aTexCoord; Color = aColor; }"); FragmentShader fragmentShader = new FragmentShader(renderContext, @"#version 330 core out vec4 FragColor; in vec2 TexCoord; in vec4 Color; uniform sampler2D texture0; void main() { FragColor = texture(texture0, TexCoord) * Color; }"); ShaderProgram shaderProgram = new ShaderProgram(renderContext); shaderProgram.AttachShader(vertexShader); shaderProgram.AttachShader(fragmentShader); shaderProgram.Link(); shaderProgram.DetachShader(vertexShader); shaderProgram.DetachShader(fragmentShader); vertexShader.Dispose(); fragmentShader.Dispose(); shaderProgram.Label = label; return(shaderProgram); }
public static ShaderProgram CreateAtlasFlippingProgram(RenderContext renderContext) { VertexShader vertexShader = new VertexShader(renderContext, @"#version 330 core layout (location = 0) in vec2 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 projection; void main() { gl_Position = projection * vec4(aPos, 1.0, 1.0); TexCoord = aTexCoord; }"); FragmentShader fragmentShader = new FragmentShader(renderContext, @"#version 330 core out vec4 FragColor; in vec2 TexCoord; uniform sampler2D texture0; void main() { vec4 samp = vec4(1.0, 1.0, 1.0, texture(texture0, TexCoord).a); FragColor = samp; }"); ShaderProgram shaderProgram = new ShaderProgram(renderContext); shaderProgram.AttachShader(vertexShader); shaderProgram.AttachShader(fragmentShader); shaderProgram.Link(); shaderProgram.DetachShader(vertexShader); shaderProgram.DetachShader(fragmentShader); vertexShader.Dispose(); fragmentShader.Dispose(); shaderProgram.Label = "Font Import ShaderProgram"; return(shaderProgram); }
public static void CompileShadersHotReload(OpenGL gl) { foreach (var name in shaderNames) { bool vertChanged = ResourceLoader.LoadTextFileIfChanged($"Shaders\\{name}.vert", out var vert); bool fragChanged = ResourceLoader.LoadTextFileIfChanged($"Shaders\\{name}.frag", out var frag); if (vertChanged || fragChanged) { string shaderError = ""; ShaderProgram program = new ShaderProgram(); // Create a vertex shader. VertexShader vertexShader = new VertexShader(); vertexShader.CreateInContext(gl); vertexShader.SetSource(vert); // Create a fragment shader. FragmentShader fragmentShader = new FragmentShader(); fragmentShader.CreateInContext(gl); fragmentShader.SetSource(frag); // Compile them both. vertexShader.Compile(); fragmentShader.Compile(); if ((bool)!vertexShader.CompileStatus) { shaderError += $"{name}.vert: " + vertexShader.InfoLog + "\n\n"; } if ((bool)!fragmentShader.CompileStatus) { shaderError += $"{name}.frag: " + fragmentShader.InfoLog; } // Build a program. program.CreateInContext(gl); // Attach the shaders. program.AttachShader(vertexShader); program.AttachShader(fragmentShader); program.Link(); programs[name] = program; shaderErrors[name] = shaderError; } } }
public ShaderBindable(OpenGL gl, Func <string, Shader> typeDetector, params string[] files) { _gl = gl; if (typeDetector == null) { throw new ArgumentNullException("typeDetector"); } _program = new ShaderProgram(); _program.CreateInContext(gl); foreach (var file in files) { var shader = typeDetector(file); shader.CreateInContext(gl); shader.SetSource(File.ReadAllText(file)); shader.Compile(); _program.AttachShader(shader); } _program.Link(); if (_program.LinkStatus != null && !(bool)_program.LinkStatus) { throw new Exception(_program.InfoLog); } foreach (var attachedShader in _program.AttachedShaders.Where(attachedShader => attachedShader.CompileStatus != null && !(bool)attachedShader.CompileStatus)) { throw new Exception(attachedShader.InfoLog); } Uniforms = new DynamicUniforms(_gl, _program); }
public static ShaderProgram CreateLineShaderProgram(RenderContext renderContext, string label = "Line ShaderProgram") { VertexShader vertexShader = new VertexShader(renderContext, @"#version 330 core layout (location = 0) in vec2 aPos; layout (location = 1) in vec4 aColor; out vec4 Color; uniform mat4 projection; void main() { gl_Position = projection * vec4(aPos, 1.0, 1.0); Color = aColor; }"); FragmentShader fragmentShader = new FragmentShader(renderContext, @"#version 330 core out vec4 FragColor; in vec4 Color; void main() { FragColor = Color; }"); ShaderProgram shaderProgram = new ShaderProgram(renderContext); shaderProgram.AttachShader(vertexShader); shaderProgram.AttachShader(fragmentShader); shaderProgram.Link(); shaderProgram.DetachShader(vertexShader); shaderProgram.DetachShader(fragmentShader); vertexShader.Dispose(); fragmentShader.Dispose(); shaderProgram.Label = label; return(shaderProgram); }
/// <summary> /// Allocate an instance of the type. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocating the instance. /// </param> /// <returns> /// It returns an instance of a specific type. /// </returns> public override object Allocate(GraphicsContext ctx) { ShaderProgram shaderProgram = new ShaderProgram("TestProgram"); ShaderObject vertexShaderObject = new ShaderObject(ShaderStage.Vertex); vertexShaderObject.LoadSource(VertexShaderSource); ShaderObject fragmentShaderObject = new ShaderObject(ShaderStage.Fragment); fragmentShaderObject.LoadSource(FragmentShaderSource); shaderProgram.AttachShader(vertexShaderObject); shaderProgram.AttachShader(fragmentShaderObject); return(shaderProgram); }
/// <summary> /// Allocate an instance of the type mocked for spying. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocating the instance. /// </param> /// <returns> /// It returns an instance of a specific type. /// </returns> public override T AllocateSpy <T>(GraphicsContext ctx) { T shaderProgramSpy = (T)CreateTypeSpy(_InstanceType, "Test"); ShaderProgram shaderProgram = shaderProgramSpy as ShaderProgram; if (shaderProgram != null) { ShaderObject vertexShaderObject = new ShaderObject(ShaderStage.Vertex); vertexShaderObject.LoadSource(VertexShaderSource); ShaderObject fragmentShaderObject = new ShaderObject(ShaderStage.Fragment); fragmentShaderObject.LoadSource(FragmentShaderSource); shaderProgram.AttachShader(vertexShaderObject); shaderProgram.AttachShader(fragmentShaderObject); } return(shaderProgramSpy); }
public static void CompileShaders(OpenGL gl) { foreach (var name in shaderNames) { ShaderProgram program = new ShaderProgram(); string shaderError = ""; // Create a vertex shader. VertexShader vertexShader = new VertexShader(); vertexShader.CreateInContext(gl); vertexShader.SetSource(ResourceLoader.LoadEmbeddedTextFile($"Shaders\\{name}.vert")); // Create a fragment shader. FragmentShader fragmentShader = new FragmentShader(); fragmentShader.CreateInContext(gl); fragmentShader.SetSource(ResourceLoader.LoadEmbeddedTextFile($"Shaders\\{name}.frag")); // Compile them both. vertexShader.Compile(); fragmentShader.Compile(); if ((bool)!vertexShader.CompileStatus) { shaderError += "color.vert: " + vertexShader.InfoLog + "\n\n"; } if ((bool)!fragmentShader.CompileStatus) { shaderError += "color.frag: " + fragmentShader.InfoLog; } // Build a program. program.CreateInContext(gl); // Attach the shaders. program.AttachShader(vertexShader); program.AttachShader(fragmentShader); program.Link(); programs.Add(name, program); shaderErrors.Add(name, shaderError); } }
public void ExampleCreateShaderProgram(GraphicsContext ctx, IEnumerable <Shader> shaderObjects) { ShaderProgram shaderProgram = new ShaderProgram("SampleProgram"); foreach (Shader shader in shaderObjects) { shaderProgram.AttachShader(shader); } shaderProgram.Create(ctx); }
public static ShaderProgram CreateShaderProgram(RenderContext renderContext) { VertexShader vertexShader = CreateVertexShader(renderContext); FragmentShader fragmentShader = CreateFragmentShader(renderContext); ShaderProgram shaderProgram = new ShaderProgram(renderContext); shaderProgram.AttachShader(vertexShader); shaderProgram.AttachShader(fragmentShader); shaderProgram.Link(); shaderProgram.DetachShader(vertexShader); shaderProgram.DetachShader(fragmentShader); vertexShader.Dispose(); fragmentShader.Dispose(); shaderProgram.Label = "IMGUI ShaderProgram"; return(shaderProgram); }
public void OpenGLInit(object sender, OpenGLEventArgs args, string Vertex_shader_textbox, string Fragment_shader_textbox) { //CREATE THE VAO args.OpenGL.GenVertexArrays(1, vao); args.OpenGL.BindVertexArray(vao[0]); //CREATE THE VBO AND POPULATE IT WITH THE QUAD VERTICIES args.OpenGL.GenBuffers(1, vbo); args.OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vbo[0]); args.OpenGL.BufferData(OpenGL.GL_ARRAY_BUFFER, g_vertex_buffer_data.Length * sizeof(float), GCHandle.Alloc(g_vertex_buffer_data, GCHandleType.Pinned).AddrOfPinnedObject(), OpenGL.GL_STATIC_DRAW); args.OpenGL.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, new IntPtr(0)); //Tell opengl what attribute arrays we need args.OpenGL.EnableVertexAttribArray(0); args.OpenGL.EnableVertexAttribArray(1); args.OpenGL.EnableVertexAttribArray(2); args.OpenGL.EnableVertexAttribArray(3); //This is the position attribute pointer (layout(location = 0) in vec2 vertexPosition;) args.OpenGL.VertexAttribPointer(1, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0)); //This is the color attribute pointer layout(location = 1) in vec4 vertexColor; args.OpenGL.VertexAttribPointer(2, 4, OpenGL.GL_UNSIGNED_BYTE, false, 0, new IntPtr(2)); //This is the UV attribute pointer layout(location = 2) in vec2 vertexUV; args.OpenGL.VertexAttribPointer(3, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0)); //UNBIND WHEN FINISHED args.OpenGL.BindVertexArray(0); args.OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); //SET SOME GL CONSTANTS args.OpenGL.Enable(OpenGL.GL_DEPTH_TEST); args.OpenGL.Enable(OpenGL.GL_BLEND); args.OpenGL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); args.OpenGL.ShadeModel(OpenGL.GL_SMOOTH); // Create a vertex shader. vertexShader = new VertexShader(); vertexShader.CreateInContext(args.OpenGL); vertexShader.SetSource(Vertex_shader_textbox); // Create a fragment shader. fragmentShader = new FragmentShader(); fragmentShader.CreateInContext(args.OpenGL); fragmentShader.SetSource(Fragment_shader_textbox); // Compile them both. vertexShader.Compile(); fragmentShader.Compile(); // Build a program. m_shaderProgram.CreateInContext(args.OpenGL); // Attach the shaders. m_shaderProgram.AttachShader(vertexShader); m_shaderProgram.AttachShader(fragmentShader); m_shaderProgram.Link(); //SET UP TEXTURE 1 texture.Create(args.OpenGL, Environment.CurrentDirectory + @"\1.png"); texture.Bind(args.OpenGL); }
public override void Prepare(GL_ControlModern control) { if (!Initialized) { var defaultFrag = new FragmentShader( @"#version 330 in vec4 fragColor; void main(){ gl_FragColor = fragColor; }" ); var defaultVert = new VertexShader( @"#version 330 in vec4 position; layout(location = 1) in vec4 color; layout(location = 2) in vec3 _cp1; layout(location = 3) in vec3 _cp2; out vec4 vertColor; out vec4 cp1; out vec4 cp2; void main(){ cp1 = vec4(_cp1,0); cp2 = vec4(_cp2,0); vertColor = color; gl_Position = position; }" ); #region block shader defaultShaderProgram = new ShaderProgram(defaultFrag, defaultVert); defaultShaderProgram.AttachShader(new Shader( @"#version 330 layout(points) in; layout(triangle_strip, max_vertices = 72) out; in vec4 vertColor[]; in vec4 cp1[]; in vec4 cp2[]; out vec4 fragColor; uniform mat4 mtxMdl; uniform mat4 mtxCam; uniform float scale; float cubeScale; vec4 pos; mat4 mtx = mtxCam*mtxMdl; vec4 points[8] = vec4[]( vec4(-1.0,-1.0,-1.0, 0.0), vec4( 1.0,-1.0,-1.0, 0.0), vec4(-1.0, 1.0,-1.0, 0.0), vec4( 1.0, 1.0,-1.0, 0.0), vec4(-1.0,-1.0, 1.0, 0.0), vec4( 1.0,-1.0, 1.0, 0.0), vec4(-1.0, 1.0, 1.0, 0.0), vec4( 1.0, 1.0, 1.0, 0.0) ); void face(int p1, int p2, int p3, int p4){ gl_Position = mtx * (pos + points[p1]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p2]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p3]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p4]*cubeScale); EmitVertex(); EndPrimitive(); } void faceInv(int p3, int p4, int p1, int p2){ gl_Position = mtx * (pos + points[p1]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p2]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p3]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p4]*cubeScale); EmitVertex(); EndPrimitive(); } void main(){ cubeScale = scale; pos = gl_in[0].gl_Position; fragColor = vertColor[0]; face(0,1,2,3); faceInv(4,5,6,7); faceInv(0,1,4,5); face(2,3,6,7); face(0,2,4,6); faceInv(1,3,5,7); cubeScale = scale*0.5; if(cp1[0]!=vec4(0,0,0,0)){ pos = gl_in[0].gl_Position+cp1[0]; face(0,1,2,3); faceInv(4,5,6,7); faceInv(0,1,4,5); face(2,3,6,7); face(0,2,4,6); faceInv(1,3,5,7); } if(cp2[0]!=vec4(0,0,0,0)){ pos = gl_in[0].gl_Position+cp2[0]; face(0,1,2,3); faceInv(4,5,6,7); faceInv(0,1,4,5); face(2,3,6,7); face(0,2,4,6); faceInv(1,3,5,7); } } " , ShaderType.GeometryShader)); defaultShaderProgram.LinkShaders(); #endregion #region lines shader defaultLinesShaderProgram = new ShaderProgram(defaultFrag, defaultVert); defaultLinesShaderProgram.AttachShader(new Shader( @"#version 330 layout(points) in; layout(line_strip, max_vertices = 72) out; in vec4 vertColor[]; in vec4 cp1[]; in vec4 cp2[]; out vec4 fragColor; uniform mat4 mtxMdl; uniform mat4 mtxCam; uniform float scale; float cubeScale; vec4 pos; mat4 mtx = mtxCam*mtxMdl; vec4 points[8] = vec4[]( vec4(-1.0,-1.0,-1.0, 0.0), vec4( 1.0,-1.0,-1.0, 0.0), vec4(-1.0, 1.0,-1.0, 0.0), vec4( 1.0, 1.0,-1.0, 0.0), vec4(-1.0,-1.0, 1.0, 0.0), vec4( 1.0,-1.0, 1.0, 0.0), vec4(-1.0, 1.0, 1.0, 0.0), vec4( 1.0, 1.0, 1.0, 0.0) ); void face(int p1, int p2, int p4, int p3){ gl_Position = mtx * (pos + points[p1]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p2]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p3]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p4]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p1]*cubeScale); EmitVertex(); EndPrimitive(); } void line(int p1, int p2){ gl_Position = mtx * (pos + points[p1]*cubeScale); EmitVertex(); gl_Position = mtx * (pos + points[p2]*cubeScale); EmitVertex(); EndPrimitive(); } void main(){ cubeScale = scale; pos = gl_in[0].gl_Position; fragColor = vertColor[0]; face(0,1,2,3); face(4,5,6,7); line(0,4); line(1,5); line(2,6); line(3,7); cubeScale = scale*0.5; if(cp1[0]!=vec4(0,0,0,0)){ pos = gl_in[0].gl_Position+cp1[0]; face(0,1,2,3); face(4,5,6,7); line(0,4); line(1,5); line(2,6); line(3,7); } if(cp2[0]!=vec4(0,0,0,0)){ pos = gl_in[0].gl_Position+cp2[0]; face(0,1,2,3); face(4,5,6,7); line(0,4); line(1,5); line(2,6); line(3,7); } } " , ShaderType.GeometryShader)); defaultLinesShaderProgram.LinkShaders(); #endregion var connectLinesFrag = new FragmentShader( @"#version 330 uniform vec4 color; void main(){ gl_FragColor = color; }" ); var connectLinesVert = new VertexShader( @"#version 330 in vec4 position; layout(location = 1) in vec3 _controlPoint; out vec4 controlPoint; void main(){ controlPoint = vec4(_controlPoint,0.0); gl_Position = position; }" ); #region connections shader connectLinesShaderProgram = new ShaderProgram(connectLinesFrag, connectLinesVert); connectLinesShaderProgram.AttachShader(new Shader( @"#version 330 layout(lines) in; layout(line_strip, max_vertices = 19) out; in vec4 controlPoint[]; uniform mat4 mtxMdl; uniform mat4 mtxCam; mat4 mtx = mtxCam*mtxMdl; vec4 p0 = gl_in[0].gl_Position; vec4 p1 = gl_in[0].gl_Position + controlPoint[0]; vec4 p2 = gl_in[1].gl_Position + controlPoint[1]; vec4 p3 = gl_in[1].gl_Position; void getPointAtTime(float t){ float u = 1f - t; float tt = t * t; float uu = u * u; float uuu = uu * u; float ttt = tt * t; gl_Position = mtx * (uuu * p0 + 3 * uu * t * p1 + 3 * u *tt * p2 + ttt * p3); EmitVertex(); } void main(){ gl_Position = mtx * p1; EmitVertex(); for(float t = 0; t<=1.0; t+=0.0625){ getPointAtTime(t); } gl_Position = mtx * p2; EmitVertex(); EndPrimitive(); } " , ShaderType.GeometryShader)); #endregion connectLinesShaderProgram.LinkShaders(); Initialized = true; } }