public Shader(OpenGL gl, string vertexPath, string fragmentPath) { string vertexCode = File.ReadAllText(vertexPath); string fragmentCode = File.ReadAllText(fragmentPath); // Vertex shader uint vertexShader = gl.CreateShader(OpenGL.GL_VERTEX_SHADER); gl.ShaderSource(vertexShader, vertexCode); gl.CompileShader(vertexShader); // Fragment shader uint fragmentShader = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER); gl.ShaderSource(fragmentShader, fragmentCode); gl.CompileShader(fragmentShader); // Create shader program Program = gl.CreateProgram(); gl.AttachShader(Program, vertexShader); gl.AttachShader(Program, fragmentShader); gl.LinkProgram(Program); StringBuilder logStringBuilder = new StringBuilder(512); gl.GetProgramInfoLog(Program, 512, IntPtr.Zero, logStringBuilder); gl.DeleteShader(vertexShader); gl.DeleteShader(fragmentShader); }
/// <summary> /// Creates the shader program. /// </summary> /// <param name="gl">The gl.</param> /// <param name="vertexShaderSource">The vertex shader source.</param> /// <param name="fragmentShaderSource">The fragment shader source.</param> /// <param name="attributeLocations">The attribute locations. This is an optional array of /// uint attribute locations to their names.</param> /// <exception cref="ShaderCompilationException"></exception> public void Create(OpenGL gl, string vertexShaderSource, string fragmentShaderSource, string geometryShaderSource, Dictionary <uint, string> attributeLocations) { // Create the shaders. vertexShader.Create(gl, OpenGL.GL_VERTEX_SHADER, vertexShaderSource); fragmentShader.Create(gl, OpenGL.GL_FRAGMENT_SHADER, fragmentShaderSource); geometryShader.Create(gl, OpenGL.GL_GEOMETRY_SHADER, geometryShaderSource); // Create the program, attach the shaders. shaderProgramObject = gl.CreateProgram(); gl.AttachShader(shaderProgramObject, vertexShader.ShaderObject); gl.AttachShader(shaderProgramObject, fragmentShader.ShaderObject); gl.AttachShader(shaderProgramObject, geometryShader.ShaderObject); // Before we link, bind any vertex attribute locations. if (attributeLocations != null) { foreach (var vertexAttributeLocation in attributeLocations) { gl.BindAttribLocation(shaderProgramObject, vertexAttributeLocation.Key, vertexAttributeLocation.Value); } } // Now we can link the program. gl.LinkProgram(shaderProgramObject); // Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're // going to throw an exception. if (GetLinkStatus(gl) == false) { throw new ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", shaderProgramObject), GetInfoLog(gl)); } }
/// <summary> /// Creates the shader program. /// </summary> /// <param name="gl">The gl.</param> /// <param name="vertexShaderSource">The vertex shader source.</param> /// <param name="fragmentShaderSource">The fragment shader source.</param> /// <param name="attributeLocations">The attribute locations. This is an optional array of /// uint attribute locations to their names.</param> /// <exception cref="ShaderCompilationException"></exception> public void Create(OpenGL gl, string vertexShaderSource, string fragmentShaderSource, Dictionary<uint, string> attributeLocations) { // Create the shaders. vertexShader.Create(gl, OpenGL.GL_VERTEX_SHADER, vertexShaderSource); fragmentShader.Create(gl, OpenGL.GL_FRAGMENT_SHADER, fragmentShaderSource); // Create the program, attach the shaders. shaderProgramObject = gl.CreateProgram(); gl.AttachShader(shaderProgramObject, vertexShader.ShaderObject); gl.AttachShader(shaderProgramObject, fragmentShader.ShaderObject); // Before we link, bind any vertex attribute locations. if (attributeLocations != null) { foreach (var vertexAttributeLocation in attributeLocations) gl.BindAttribLocation(shaderProgramObject, vertexAttributeLocation.Key, vertexAttributeLocation.Value); } // Now we can link the program. gl.LinkProgram(shaderProgramObject); // Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're // going to throw an exception. if (GetLinkStatus(gl) == false) { throw new ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", shaderProgramObject), GetInfoLog(gl)); } }
public UInt32 load() { //opengl program for shaders UInt32 program = gl.CreateProgram(); //var for vertex shader UInt32 vertex = gl.CreateShader(OpenGL.GL_VERTEX_SHADER); //var for fragment shader UInt32 fragment = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER); //read shaders sorce string vertsorce = System.IO.File.ReadAllText(m_vertPath); string fragsorce = System.IO.File.ReadAllText(m_fragPath); //compile vertex shader gl.ShaderSource(vertex, vertsorce); gl.CompileShader(vertex); //store error int[] result = new int[1]; //get information oof compiled shader gl.GetShader(vertex, OpenGL.GL_COMPILE_STATUS, result); //if compilation gives error delete shader and throw error if (result[0] == OpenGL.GL_FALSE) { gl.DeleteShader(vertex); throw (new initializing_vertex_Shader("Failed to Compile Vertex Shader")); } //same as up but for fragment shader gl.ShaderSource(fragment, fragsorce); gl.CompileShader(fragment); int[] resultF = new int[1]; gl.GetShader(fragment, OpenGL.GL_COMPILE_STATUS, resultF); if (result[0] == OpenGL.GL_FALSE) { gl.DeleteShader(fragment); throw (new initializing_fragment_Shader("Failed to Compile fragment Shader")); } //attach vetex shader to opengl program gl.AttachShader(program, vertex); gl.AttachShader(program, fragment); //link and validate program gl.LinkProgram(program); gl.ValidateProgram(program); //free space form unuseable shaders gl.DeleteShader(vertex); gl.DeleteShader(fragment); //return shader id return(program); }
public static uint CompileProgram(this OpenGL gl, byte[] vertexSource, byte[] fragmentSource) { uint vertexShaderID = gl.CompileShader(vertexSource, OpenGL.GL_VERTEX_SHADER); uint fragmentShaderID = gl.CompileShader(fragmentSource, OpenGL.GL_FRAGMENT_SHADER); uint programID = gl.CreateProgram(); gl.AttachShader(programID, vertexShaderID); gl.AttachShader(programID, fragmentShaderID); gl.DeleteShader(vertexShaderID); gl.DeleteShader(fragmentShaderID); gl.LinkProgram(programID); return(programID); }
private void openGLInv_OpenGLDraw(object sender, RenderEventArgs e) { // Get the OpenGL object. OpenGL gl = openGLControl1.OpenGL; // Clear the color and depth buffer. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); // Load the identity matrix. gl.LoadIdentity(); // Rotate around the Y axis. gl.Rotate(rotation, 0.0f, 1.0f, 0.0f); // Draw a coloured pyramid. gl.Begin(OpenGL.GL_TRIANGLES); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.Color(0.0f, 0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.Color(1.0f, 0.5f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); gl.Color(1.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.Color(0.0f, 0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.Color(1.0f, 0.5f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); gl.Color(1.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.End(); gl.AttachShader(OpenGL.GL_FRAGMENT_SHADER, test.ShaderObject); // Nudge the rotation. rotation += 1.0f; }
/// <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); } }
public uint Loader(string vertexShaderPath, string fragmentShaderPath) { // Vertex Shader string vertexShaderSource = System.IO.File.ReadAllText(vertexShaderPath); string fragmentShaderSource = System.IO.File.ReadAllText(fragmentShaderPath); vertexShader = gl.CreateShader(OpenGL.GL_VERTEX_SHADER); gl.ShaderSource(vertexShader, vertexShaderSource); gl.CompileShader(vertexShader); if (GetCompileStatus(gl, vertexShader) == false) { throw new ShaderCompilationException(string.Format("Failed to compile shader with ID {0}.", vertexShader), GetCompileInfoLog(vertexShader)); } // Fragment Shader fragmentShader = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER); gl.ShaderSource(fragmentShader, fragmentShaderSource); gl.CompileShader(fragmentShader); if (GetCompileStatus(gl, fragmentShader) == false) { throw new ShaderCompilationException(string.Format("Failed to compile shader with ID {0}.", fragmentShader), GetCompileInfoLog(fragmentShader)); } // Link shaders shaderProgram = gl.CreateProgram(); gl.AttachShader(shaderProgram, vertexShader); gl.AttachShader(shaderProgram, fragmentShader); gl.LinkProgram(shaderProgram); if (GetLinkStatus(gl) == false) { throw new ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", shaderProgram), GetLinkInfoLog(gl)); } gl.DeleteShader(vertexShader); gl.DeleteShader(fragmentShader); return(shaderProgram); }
public OpenGLShader(OpenGL gl, string vertexShaderPath, string fragmentShaderPath) { this._gl = gl; VertexShader = CreateShader(OpenGL.GL_VERTEX_SHADER, vertexShaderPath); FragmentShader = CreateShader(OpenGL.GL_FRAGMENT_SHADER, fragmentShaderPath); ShaderProgram = _gl.CreateProgram(); _gl.AttachShader(ShaderProgram, VertexShader); _gl.AttachShader(ShaderProgram, FragmentShader); _gl.LinkProgram(ShaderProgram); VertexLocation = (uint)_gl.GetAttribLocation(ShaderProgram, "vertexPosition"); TextureCoordLocation = (uint)_gl.GetAttribLocation(ShaderProgram, "textureCoord"); ViewportSizeUniform = _gl.GetUniformLocation(ShaderProgram, "viewportSize"); TranslationUniform = _gl.GetUniformLocation(ShaderProgram, "translation"); TransformUniform = _gl.GetUniformLocation(ShaderProgram, "transform"); PixelOffsetUniform = _gl.GetUniformLocation(ShaderProgram, "pixelOffset"); TextureSamplerUniform = _gl.GetUniformLocation(ShaderProgram, "textureSampler"); ColorUniform = _gl.GetUniformLocation(ShaderProgram, "color"); }
public void Attach() { //!< 作成したシェーダをアタッチ foreach (var obj in ShaderObjectDict) { Device.AttachShader(ShaderLink, obj.Value); } //!< リンクすることでGPUで使えるようになる Device.LinkProgram(ShaderLink); Device.UseProgram(ShaderLink); }
private void openGLInv_OpenGLInitialized(object sender, EventArgs e) { // TODO: Initialise OpenGL here. // Get the OpenGL object. OpenGL gl = openGLControl1.OpenGL; // Set the clear color. gl.ClearColor(0, 0, 0, 0); gl.ClearColor(this.BackColor.R, this.BackColor.G, this.BackColor.B, this.BackColor.A); test = new SharpGL.Shaders.Shader(); test.Create(gl, OpenGL.GL_FRAGMENT_SHADER, File.ReadAllText(Application.StartupPath + "/modelPreview.glsl")); gl.AttachShader(OpenGL.GL_FRAGMENT_SHADER, test.ShaderObject); }
private void OpenGLControl_OpenGLInitialized(object sender, EventArgs e) { OpenGL gl = this.openGLControl1.OpenGL; string fragmentShaderCode = null; using (StreamReader sr = new StreamReader(FRAGMENT_SHADER_PATH)) { fragmentShaderCode = sr.ReadToEnd(); } string vertexShaderCode = null; using (StreamReader sr = new StreamReader(VERTEX_SHADER_PATH)) { vertexShaderCode = sr.ReadToEnd(); } _prog = new SharpGL.Shaders.ShaderProgram(); _prog.Create(gl, vertexShaderCode, fragmentShaderCode, null); string geometryShaderCode = null; if (File.Exists(GEOMETRY_SHADER_PATH)) { using (StreamReader sr = new StreamReader(GEOMETRY_SHADER_PATH)) { geometryShaderCode = sr.ReadToEnd(); } Shader geometryShader = new Shader(); geometryShader.Create(gl, OpenGL.GL_GEOMETRY_SHADER, geometryShaderCode); gl.AttachShader(_prog.ShaderProgramObject, geometryShader.ShaderObject); } gl.LinkProgram(_prog.ShaderProgramObject); // Now that we've compiled and linked the shader, check it's link status.If it's not linked properly, we're // going to throw an exception. if (_prog.GetLinkStatus(gl) == false) { throw new SharpGL.Shaders.ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", _prog.ShaderProgramObject), _prog.GetInfoLog(gl)); } }
// TODO: rename to createShaderProgram private uint createProgram(uint vertexShader, uint fragmentShader) { uint program = OpenGL.CreateProgram(); OpenGL.AttachShader(program, vertexShader); OpenGL.AttachShader(program, fragmentShader); OpenGL.LinkProgram(program); // check for linking errors int success = 0; byte[] infoLog = new byte[512]; OpenGL.GetProgramiv(program, OpenGL.GL_LINK_STATUS, ref success); if (success == 0) { OpenGL.GetProgramInfoLog(program, infoLog.Length, IntPtr.Zero, infoLog); string log = System.Text.Encoding.UTF8.GetString(infoLog); throw new LinkingException(log); } return(program); }
private uint CreateProgram(List <uint> shaderList) { uint program = gl.CreateProgram(); foreach (uint shader in shaderList) { gl.AttachShader(program, shader); } gl.LinkProgram(program); ProgramErrorInfo(program); foreach (uint shader in shaderList) { gl.DetachShader(program, shader); } return(program); }
private void BuildShaders(OpenGL gl) { string GetCompileError(uint shaderId) { var compileStatus = new int[1]; gl.GetShader(shaderId, OpenGL.GL_COMPILE_STATUS, compileStatus); if (compileStatus[0] != OpenGL.GL_TRUE) { var infoLogLength = new int[1]; gl.GetShader(shaderId, OpenGL.GL_INFO_LOG_LENGTH, infoLogLength); var buffer = new System.Text.StringBuilder(infoLogLength[0]); gl.GetShaderInfoLog(shaderId, infoLogLength[0], IntPtr.Zero, buffer); return(buffer.ToString()); } return(null); } string GetLinkError(uint progId) { var linkStatus = new int[1]; gl.GetProgram(progId, OpenGL.GL_LINK_STATUS, linkStatus); if (linkStatus[0] != OpenGL.GL_TRUE) { var infoLogLength = new int[1]; gl.GetProgram(progId, OpenGL.GL_INFO_LOG_LENGTH, infoLogLength); var buffer = new System.Text.StringBuilder(infoLogLength[0]); gl.GetProgramInfoLog(progId, infoLogLength[0], IntPtr.Zero, buffer); return(buffer.ToString()); } return(null); } ShaderProgramId = gl.CreateProgram(); var vertexShader = Controller.LoadShader("vertex"); var fragmentShader = Controller.LoadShader("fragment"); uint vertexShaderId = gl.CreateShader(OpenGL.GL_VERTEX_SHADER); uint fragmentShaderId = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER); gl.ShaderSource(vertexShaderId, vertexShader); gl.ShaderSource(fragmentShaderId, fragmentShader); gl.CompileShader(vertexShaderId); var vertexCompileError = GetCompileError(vertexShaderId); if (vertexCompileError != null) { System.Windows.Forms.MessageBox.Show(vertexCompileError, "Vertex Compile Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } gl.CompileShader(fragmentShaderId); var fragmentCompileError = GetCompileError(fragmentShaderId); if (fragmentCompileError != null) { System.Windows.Forms.MessageBox.Show(fragmentCompileError, "Fragment Compile Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } gl.AttachShader(ShaderProgramId, vertexShaderId); gl.AttachShader(ShaderProgramId, fragmentShaderId); gl.LinkProgram(ShaderProgramId); var linkError = GetLinkError(ShaderProgramId); if (linkError != null) { System.Windows.Forms.MessageBox.Show(linkError, "Link Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } gl.DeleteShader(vertexShaderId); gl.DeleteShader(fragmentShaderId); gl.UseProgram(ShaderProgramId); ProjectMatrixLocation = gl.GetUniformLocation(ShaderProgramId, "project"); ViewMatrixLocation = gl.GetUniformLocation(ShaderProgramId, "view"); TranslateMatrixLocation = gl.GetUniformLocation(ShaderProgramId, "translate"); ScaleMatrixLocation = gl.GetUniformLocation(ShaderProgramId, "scale"); RotateMatrixLocation = gl.GetUniformLocation(ShaderProgramId, "rotate"); AmbientLightColorLocation = gl.GetUniformLocation(ShaderProgramId, "ambient_light_color"); AmbientLightPowerLocation = gl.GetUniformLocation(ShaderProgramId, "ambient_light_power"); LightSourcePositionLocation = gl.GetUniformLocation(ShaderProgramId, "light_source_position"); LightSourceColorLocation = gl.GetUniformLocation(ShaderProgramId, "light_source_color"); LightSourcePowerLocation = gl.GetUniformLocation(ShaderProgramId, "light_source_power"); EyePositionLocation = gl.GetUniformLocation(ShaderProgramId, "eye_position"); ElectrodePositionsLocation = gl.GetUniformLocation(ShaderProgramId, "electrode_positions"); ElectrodeCountLocation = gl.GetUniformLocation(ShaderProgramId, "electrode_count"); ElectrodeIndicesLocation = gl.GetUniformLocation(ShaderProgramId, "electrode_indices"); ElectrodesUsedCountLocation = gl.GetUniformLocation(ShaderProgramId, "electrodes_used_count"); ElectrodeValuesLocation = gl.GetUniformLocation(ShaderProgramId, "electrode_values"); ElectrodeMaxDistanceLocation = gl.GetUniformLocation(ShaderProgramId, "electrode_max_distance"); }
public void Initialise(OpenGL gl) { #region Load the main program. string fragmentShaderCode = null; using (StreamReader sr = new StreamReader(FRAGMENT_SHADER_PATH)) { fragmentShaderCode = sr.ReadToEnd(); } string vertexShaderCode = null; using (StreamReader sr = new StreamReader(VERTEX_SHADER_PATH)) { vertexShaderCode = sr.ReadToEnd(); } _prog = new SharpGL.Shaders.ShaderProgram(); _prog.Create(gl, vertexShaderCode, fragmentShaderCode, null); string geometryShaderCode = null; if (File.Exists(GEOMETRY_SHADER_PATH)) { using (StreamReader sr = new StreamReader(GEOMETRY_SHADER_PATH)) { geometryShaderCode = sr.ReadToEnd(); } Shader geometryShader = new Shader(); geometryShader.Create(gl, OpenGL.GL_GEOMETRY_SHADER, geometryShaderCode); gl.AttachShader(_prog.ShaderProgramObject, geometryShader.ShaderObject); } gl.LinkProgram(_prog.ShaderProgramObject); // Now that we've compiled and linked the shader, check it's link status.If it's not linked properly, we're // going to throw an exception. if (_prog.GetLinkStatus(gl) == false) { throw new SharpGL.Shaders.ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", _prog.ShaderProgramObject), _prog.GetInfoLog(gl)); } #endregion #region Load clear program. //string clearFragShaderCode = null; //using (StreamReader sr = new StreamReader(CLEAR_FRAGMENT_SHADER_PATH)) //{ // clearFragShaderCode = sr.ReadToEnd(); //} //string clearVertexShaderCode = null; //using (StreamReader sr = new StreamReader(CLEAR_VERTEX_SHADER_PATH)) //{ // clearVertexShaderCode = sr.ReadToEnd(); //} //_clearProg = new SharpGL.Shaders.ShaderProgram(); //_clearProg.Create(gl, clearVertexShaderCode, clearFragShaderCode, null); //gl.LinkProgram(_clearProg.ShaderProgramObject); //// Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're //// going to throw an exception. //if (_clearProg.GetLinkStatus(gl) == false) //{ // throw new SharpGL.Shaders.ShaderCompilationException(string.Format("Failed to link the clear shader program with ID {0}.", _clearProg.ShaderProgramObject), _clearProg.GetInfoLog(gl)); //} //_clearRectangle = new float[] { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; #endregion }