/// <summary> /// Main function in this program, render shader with his file name /// </summary> public void RenderShader(string nameOfShaderFile, SettingModel model) { string fShaderSource = null; ShaderLoader.LoadShader(nameOfShaderFile, out fShaderSource); if (fShaderSource == null) { _logger.LoadShaderError(); return; } if (!ShaderLoader.InitShaders(fShaderSource, out program)) { _logger.InitShaderError(); return; } nVertices = InitVertexBuffers(); if (nVertices <= 0) { _logger.WritePosError(); return; } uniforms = new Uniforms(model, program); GL.ClearColor(Color.SteelBlue); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Translate a Triangle"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to write the positions of vertices to a vertex shader"); return; } // Get the storage location of u_Translation u_Translation = GL.GetUniformLocation(program, "u_Translation"); if (u_Translation < 0) { Logger.Append("Failed to get the storage location of u_Translation"); return; } // Pass the translation distance to the vertex shader GL.Uniform4(u_Translation, Tx, Ty, Tz, 0f); // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); // Set window title and canvas size Title = "Hello Point (2)"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Get a storage location of a_Position int a_Position = GL.GetAttribLocation(program, "a_Position"); if (a_Position < 0) { Logger.Append("Failed to get the storage location of a_Position"); return; } // Pass vertex position to attribute variable GL.VertexAttrib3(a_Position, 0f, 0f, 0f); // Specify the color for clearing "canvas" GL.ClearColor(Color4.Black); GL.Enable(EnableCap.ProgramPointSize); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Change a point color"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load shaders from files"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Get the storage location of a_Position a_Position = GL.GetAttribLocation(program, "a_Position"); if (a_Position < 0) { Logger.Append("Failed to get the storage location of a_Position"); return; } // Get the storage location of u_FragColor u_FragColor = GL.GetUniformLocation(program, "u_FragColor"); if (u_FragColor < 0) { Logger.Append("Failed to get the storage location of u_FragColor"); return; } // Enable point size GL.Enable(EnableCap.ProgramPointSize); // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Continually Rotate A Triangle"; Width = 400; Height = 400; // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Load the fragment shader from a file"); return; } // Initialize shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize shaders"); return; } // Set vertex information nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to set vertex information"); return; } // Get the storage location of u_ModelMatrix u_ModelMatrix = GL.GetUniformLocation(program, "u_ModelMatrix"); if (u_ModelMatrix < 0) { Logger.Append("Failed to get the storage location of u_ModelMatrix"); return; } // Set the color for clearing a canvas GL.ClearColor(Color.BlueViolet); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Hello Quad (gl.TRIANGLE_FAN)"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to write the positions of vertices to a vertex shader"); return; } // Enable the point size GL.Enable(EnableCap.ProgramPointSize); // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Scale A Triangle (Matrix)"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to write the positions of vertices to a vertex shader"); return; } // Note: WebGL is column major order xformMatrix = new Matrix4( Sx, 0f, 0f, 0f, 0f, Sy, 0f, 0f, 0f, 0f, Sz, 0f, 0f, 0f, 0f, 1f ); // Get the storage location of u_xformMatrix u_xformMatrix = GL.GetUniformLocation(program, "u_xformMatrix"); if (u_xformMatrix < 0) { Logger.Append("Failed to get the storage location of u_xformMatrix"); return; } // Pass the rotation matrix to the vertex shader GL.UniformMatrix4(u_xformMatrix, false, ref xformMatrix); // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Rotate And Then Translate A Triangle"; Width = 400; Height = 400; Console.WriteLine("Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load vertex shader from file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load fragment shader from file"); return; } // Initialize shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to set the positions of the vertices"); return; } float ANGLE = 60f; // The rotation angle var Tx = 0.5f; // Translation distance // Create Matrix4 object for model transformation // And calculate a model matrix Matrix4 modelMatrix = Matrix4.CreateRotationZ(ANGLE * (float)Math.PI / 180f) * Matrix4.CreateTranslation(Tx, 0f, 0f); // Pass the model matrix to the vertex shader int u_ModelMatrix = GL.GetUniformLocation(program, "u_ModelMatrix"); if (u_ModelMatrix < 0) { Logger.Append("Failed to get the storage location of u_ModelMatrix"); return; } GL.UniformMatrix4(u_ModelMatrix, false, ref modelMatrix); // Specify color for clearing canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); // Load shader from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null || fShaderSource == null) { Logger.Append("Failed to load vertex or framgment shader"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program.id)) { Logger.Append("Failed to initialize the shaders"); return; } // Get the storage location of attribute variables and uniform variables program.a_Position = GL.GetAttribLocation(program.id, "a_Position"); program.a_TexCoord = GL.GetAttribLocation(program.id, "a_TexCoord"); program.u_MvpMatrix = GL.GetUniformLocation(program.id, "u_MvpMatrix"); if (program.a_Position < 0 || program.a_TexCoord < 0 || program.u_MvpMatrix < 0) { Logger.Append("Failed to get the storage location of a_Position, a_TexCoord, u_MvpMatrix"); return; } // Set the vertex information cube = InitVertexBuffersForCube(); plane = InitVertexBuffersForPlane(); if (cube == null || plane == null) { Logger.Append("Failed to set the vertex information"); return; } // Set texture texture = InitTextures(); if (texture < 0) { Logger.Append("Failed to intialize the texture"); return; } // Initialize framebuffer object (FBO) frameBuffer = InitFramebufferObject(); if (frameBuffer == null) { Logger.Append("Failed to intialize the framebuffer object (FBO)"); return; } // Enable depth test GL.Enable(EnableCap.DepthTest); // GL.Enable(EnableCap.CullFace); modelMatrix = Matrix4.Identity; viewMatrix = Matrix4.LookAt(0f, 0f, 5f, 0f, 0f, 0f, 0f, 1f, 0f); // Prepare view projection matrix for color buffer SetProjMatrix(); projMatrixFBO = Matrix4.CreatePerspectiveFieldOfView(0.8f, OFFSCREEN_WIDTH / (float)OFFSCREEN_HEIGHT, 0.1f, 100f); // The projection matrix viewMatrixFBO = Matrix4.LookAt(0f, 2f, 7f, 0f, 0f, 0f, 0f, 1f, 0f); // The view matrix modelMatrixFBO = Matrix4.Identity; mvpMatrixFBO = modelMatrixFBO * viewMatrixFBO * projMatrixFBO; canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Rotate A Triangle"; Width = 400; Height = 400; Console.WriteLine("OpenGL Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize the shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to write the positions of vertices to a vertex shader"); return; } // Pass the data required to rotate the shape to the vertex shader double radian = angle * Math.PI / 180.0; cosB = Math.Cos(radian); sinB = Math.Sin(radian); // Get the storage location of u_CosB u_CosB = GL.GetUniformLocation(program, "u_CosB"); if (u_CosB < 0) { Logger.Append("Failed to get the storage location of u_CosB"); return; } // Get the storage location of u_SinB u_SinB = GL.GetUniformLocation(program, "u_SinB"); if (u_SinB < 0) { Logger.Append("Failed to get the storage location of u_SinB"); return; } GL.Uniform1(u_CosB, (float)cosB); GL.Uniform1(u_SinB, (float)sinB); // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Rotate A Triangle (Matrix4)"; Width = 400; Height = 400; Console.WriteLine("Version: " + GL.GetString(StringName.Version)); Console.WriteLine("Video Adapter: " + GL.GetString(StringName.Renderer)); // Load shaders from files string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShaderSource == null) { Logger.Append("Failed to load the vertex shader from a file"); return; } if (fShaderSource == null) { Logger.Append("Failed to load the fragment shader from a file"); return; } // Initialize shaders if (!ShaderLoader.InitShaders(vShaderSource, fShaderSource, out program)) { Logger.Append("Failed to initialize the shaders"); return; } // Write the positions of vertices to a vertex shader nVertices = InitVertexBuffers(); if (nVertices < 0) { Logger.Append("Failed to write the positions of vertices to a vertex shader"); return; } // Create Matrix4 object for the rotation matrix Matrix4 modelMatrix = new Matrix4(); // Set the rotation matrix float ANGLE = 90 * (float)Math.PI / 180f; modelMatrix = Matrix4.CreateRotationZ(ANGLE); // Pass the rotation matrix to the vertex shader u_MvpMatrix = GL.GetUniformLocation(program, "u_MvpMatrix"); if (u_MvpMatrix < 0) { Logger.Append("Failed to get the storage location of u_MvpMatrix"); return; } GL.UniformMatrix4(u_MvpMatrix, false, ref modelMatrix); WindowBorder = WindowBorder.Fixed; //viewProjMatrix = Matrix4.CreatePerspectiveFieldOfView(0.8, ) // Specify the color for clearing the canvas GL.ClearColor(Color.Black); canDraw = true; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Title = "Shadow"; // Load shader from files string vShadowShaderSource = null; string fShadowShaderSource = null; string vShaderSource = null; string fShaderSource = null; ShaderLoader.LoadShader("./Shaders/ShadowVertexShader.glsl", out vShadowShaderSource); ShaderLoader.LoadShader("./Shaders/ShadowFragmentShader.glsl", out fShadowShaderSource); ShaderLoader.LoadShader("./Shaders/VertexShader.glsl", out vShaderSource); ShaderLoader.LoadShader("./Shaders/FragmentShader.glsl", out fShaderSource); if (vShadowShaderSource == null || fShadowShaderSource == null || vShaderSource == null || fShaderSource == null) { Logger.Append("Failed to load vertex or framgment shader"); return; } // Initialize shaders for generating a shadow map shadowProgram.id = ShaderLoader.CreateProgram(vShadowShaderSource, fShadowShaderSource); if (shadowProgram.id == 0) { Logger.Append("Failed to crate a shadow program"); return; } shadowProgram.a_Position = GL.GetAttribLocation(shadowProgram.id, "a_Position"); shadowProgram.u_MvpMatrix = GL.GetUniformLocation(shadowProgram.id, "u_MvpMatrix"); if (shadowProgram.a_Position < 0 || shadowProgram.u_MvpMatrix < 0) { Logger.Append("Failed to get the storage location of attribute or uniform variable from shadowProgram"); return; } // Initialize shaders for regular drawing normalProgram.id = ShaderLoader.CreateProgram(vShaderSource, fShaderSource); if (normalProgram.id == 0) { Logger.Append("Failed to crate a normal program"); return; } normalProgram.a_Position = GL.GetAttribLocation(normalProgram.id, "a_Position"); normalProgram.a_Color = GL.GetAttribLocation(normalProgram.id, "a_Color"); normalProgram.u_MvpMatrix = GL.GetUniformLocation(normalProgram.id, "u_MvpMatrix"); normalProgram.u_MvpMatrixFromLight = GL.GetUniformLocation(normalProgram.id, "u_MvpMatrixFromLight"); normalProgram.u_ShadowMap = GL.GetUniformLocation(normalProgram.id, "u_ShadowMap"); if (normalProgram.a_Position < 0 || normalProgram.a_Color < 0 || normalProgram.u_MvpMatrix < 0 || normalProgram.u_MvpMatrixFromLight < 0 || normalProgram.u_ShadowMap < 0) { Logger.Append("Failed to get the storage location of attribute or uniform variable from normalProgram"); return; } // Set the vertex information triangle = InitVertexBuffersForTriangle(); plane = InitVertexBuffersForPlane(); if (triangle == null || plane == null) { Logger.Append("Failed to set the vertex information"); return; } // Initialize framebuffer object (FBO) fbo = InitFramebufferObject(); if (fbo == null) { Logger.Append("Failed to intialize the framebuffer object (FBO)"); return; } GL.ActiveTexture(TextureUnit.Texture0); // Set a texture object to the texture unit GL.BindTexture(TextureTarget.Texture2D, fbo.texture); // Set the clear color and enable the depth test GL.ClearColor(Color4.Black); GL.Enable(EnableCap.DepthTest); // GL.Enable(EnableCap.CullFace); viewMatrixFromLight = Matrix4.LookAt(LIGHT_X, LIGHT_Y, LIGHT_Z, 0f, 0f, 0f, 0f, 1f, 0f); viewMatrix = Matrix4.LookAt(0f, 7f, 9f, 0f, 0f, 0f, 0f, 1f, 0f); SetProjMatrixes(); canDraw = true; }