Example #1
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Title  = "Rotate 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;
            }

            // Create a rotation matrix
            double radian = angle * Math.PI / 180f; // Convert to radians
            float  cosB   = (float)Math.Cos(radian);
            float  sinB   = (float)Math.Sin(radian);

            // Note: WebGL is column major order
            xformMatrix = new Matrix4(
                cosB, sinB, 0f, 0f,
                -sinB, cosB, 0f, 0f,
                0f, 0f, 1f, 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;
        }
Example #2
0
        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;
        }
Example #3
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Title  = "Translate And Then Rotate 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;
            }

            // Calculate a model matrix
            float ANGLE = 60f;  // The rotation angle
            var   Tx    = 0.5f; // Translation distance

            // Create Matrix4 object for model transformation
            Matrix4 modelMatrix = Matrix4.CreateTranslation(Tx, 0f, 0f) *
                                  Matrix4.CreateRotationZ(ANGLE * (float)Math.PI / 180f);

            // 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);

            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;
        }