Example #1
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            var eye = Vector3.Zero;

            eye.Z     = 16;
            modelview = Matrix4.LookAt(eye, Vector3.UnitZ, Vector3.UnitY);

            GL.MatrixMode(MatrixMode.Modelview);

            GL.LoadMatrix(ref modelview);


            //GL.Disable(EnableCap.Blend);
            //Blt(10, 40, TextureWidth, TextureHeight);
            GL.Enable(EnableCap.Blend);



            //GL.Enable(EnableCap.Blend);
            //GL.PushMatrix();
            //GL.Scale(0.05, 0.05, 0.05);
            //DrawText(0, 0, "Test");
            //GL.PopMatrix();
            //GL.Disable(EnableCap.Blend);
            //GL.Disable(EnableCap.Texture2D);



            scene.Draw();

            SwapBuffers();

            _ready = true;
        }
Example #2
0
        public static void Main1()
        {
            game = new GameWindow(WINDOW_BASE_WIDTH, WINDOW_BASE_HEIGHT, new GraphicsMode(32, 8, 0, 4));

            float size = 80;
            float ligthX = size / 2f, ligthY = size - size / 10f, lightZ = size / 2f;
            float ligthTopX = size / 2f, ligthTopY = size - size / 10f, lightTopZ = size / 2f;
            int   count          = 15;
            int   earthTexture   = 1;
            int   caverTexture   = 1;
            int   mishaTextures  = 1;
            int   footBallTextur = 1;

            game.Load += (sender, e) =>
            {
                // setup settings, load textures, sounds
                game.VSync = VSyncMode.On;

                GL.ClearColor(Color.SkyBlue);
                GL.Enable(EnableCap.DepthTest);

                Matrix4 p = Matrix4.CreatePerspectiveFieldOfView((float)(45 * game.Width / game.Height * Math.PI / 180), (float)game.Width / game.Height, 1, 500);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref p);

                Matrix4 modelview = Matrix4.LookAt(size / 2, size / 2, size - size / 10, size / 2, size / 2, 0, 0, 1, 0);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadMatrix(ref modelview);


                earthTexture    = LoadTexture("texture.png", 1, flip_y: true);
                current_texture = LoadTexture("grass.jpg", 1);
                caverTexture    = LoadTexture("caver.jpg", 1);
                mishaTextures   = LoadTexture("Misha.jpg", 1);
                footBallTextur  = LoadTexture("football.png", 1);

                //Basically enables the alpha channel to be used in the color buffer
                GL.Enable(EnableCap.Blend);
                //The operation/order to blend
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                //Use for pixel depth comparing before storing in the depth buffer
                GL.Enable(EnableCap.DepthTest);
                GL.Enable(EnableCap.Normalize);

                float[] mat_diffuse = { 0.1f, 0.1f, 0.1f };
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, mat_diffuse);
                //1.0f, 1.0f, 1.0f
                float[] whiteSpecularLight = new float[] { 1.0f, 1.0f, 1.0f };
                float[] blackAmbientLight  = new float[] { 0.1f, 0.1f, 0.1f };
                float[] whiteDiffuseLight  = new float[] { 1.0f, 1.0f, 1.0f };

                GL.Light(LightName.Light0, LightParameter.Specular, whiteSpecularLight);
                GL.Light(LightName.Light0, LightParameter.Ambient, blackAmbientLight);
                GL.Light(LightName.Light0, LightParameter.Diffuse, whiteDiffuseLight);
                GL.Material(MaterialFace.Front, MaterialParameter.Shininess, 30);

                GL.LightModel(LightModelParameter.LightModelAmbient, new float[] { 0.1f, 0.1f, 0.1f, 1.0f });
                //GL.LightModel(LightModelParameter.LightModelTwoSide, 1);
                GL.LightModel(LightModelParameter.LightModelLocalViewer, 1);
                GL.Material(MaterialFace.Front, MaterialParameter.Ambient, new float[] { 0.3f, 0.3f, 0.3f, 1.0f });
                GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, new float[] { 0.5f, 0.5f, 0.5f, 1.0f });
                GL.Material(MaterialFace.Front, MaterialParameter.Specular, new float[] { 0.5f, 0.5f, 0.5f, 1.0f });
                GL.Material(MaterialFace.Front, MaterialParameter.Emission, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });

                GL.LightModel(LightModelParameter.LightModelColorControl, 1);


                //GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, 0.8f);
                //GL.Light(LightName.Light0, LightParameter.SpotDirection, new float[] { 0f, -1f, 0f });
                //GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, 2f);
                //GL.Light(LightName.Light0, LightParameter.SpotCutoff, 34f);
                GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, 1.8f);
                GL.Light(LightName.Light0, LightParameter.LinearAttenuation, 0.0f);
                GL.Light(LightName.Light0, LightParameter.QuadraticAttenuation, 0.0f);
                //GL.Light(LightName.Light0, LightParameter.SpotExponent, 1f);
                GL.Light(LightName.Light0, LightParameter.Position, new float[] { ligthX, ligthY, lightZ, 1f });


                GL.ShadeModel(ShadingModel.Smooth);
                GL.Enable(EnableCap.Lighting);
                GL.Enable(EnableCap.Light0);
                GL.Enable(EnableCap.ColorMaterial);
            };

            game.Resize += (sender, e) =>
            {
                GL.Viewport(0, 0, game.Width, game.Height);

                Matrix4 p = Matrix4.CreatePerspectiveFieldOfView((float)(45 * game.Width / game.Height * Math.PI / 180), (float)game.Width / game.Height, 1, 500);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref p);
            };

            game.UpdateFrame += (sender, e) =>
            {
                // add game logic, input handling
                if (game.Keyboard[Key.Escape])
                {
                    game.Exit();
                }

                if (game.Keyboard[Key.Space])
                {
                    Matrix4 p = Matrix4.CreatePerspectiveFieldOfView((float)(45 * game.Width / game.Height * Math.PI / 180), (float)game.Width / game.Height, 1, 500);
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.LoadMatrix(ref p);

                    Matrix4 modelview = Matrix4.LookAt(size / 2, size / 2, size - size / 10, size / 2, size / 2, 0, 0, 1, 0);
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.LoadMatrix(ref modelview);
                }

                if (game.Keyboard[Key.Enter])
                {
                    game.WindowState = OpenTK.WindowState.Fullscreen;
                }

                if (game.Keyboard[Key.R])
                {
                    ligthX += 0.1f;
                    ligthY += 0.1f;
                    lightZ += 0.1f;
                }

                if (game.Keyboard[Key.T])
                {
                    ligthX -= 0.1f;
                    ligthY -= 0.1f;
                    lightZ -= 0.1f;
                }

                if (game.Keyboard[Key.Down])
                {
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.Translate(0, 0, -1);
                }

                if (game.Keyboard[Key.Up])
                {
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.Translate(0, 0, 1);
                }

                if (game.Keyboard[Key.Left])
                {
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.Translate(-1, 0, 0);
                }

                if (game.Keyboard[Key.Right])
                {
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.Translate(1, 0, 0);
                }

                if (game.Keyboard[Key.D])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(1, 0, 1, 0);
                }

                if (game.Keyboard[Key.A])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(-1, 0, 1, 0);
                }

                //123

                if (game.Keyboard[Key.S])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(1, 1, 0, 0);
                }

                if (game.Keyboard[Key.W])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(-1, 1, 0, 0);
                }

                if (game.Keyboard[Key.Q])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(1, 0, 0, 1);
                }

                if (game.Keyboard[Key.E])
                {
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.Rotate(-1, 0, 0, 1);
                }
            };

            game.RenderFrame += (sender, e) =>
            {
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                //light

                GL.Light(LightName.Light0, LightParameter.Position, new float[] { ligthX, ligthY, lightZ, 1f });
                GL.ShadeModel(ShadingModel.Smooth);


                Color[] colors = new Color[6];
                for (int i = 0; i < colors.Length; i++)
                {
                    colors[i] = Color.White;
                }
                //DrawCube(0, 2, 0.01f, 2, ligthX - 1f, ligthY + 2f, lightZ - 1f, 15, colors);
                GL.Color3(Color.Black);
                DrawLine(ligthTopX, size, lightTopZ, ligthX, ligthY, lightZ);
                GL.Color3(Color.LightGoldenrodYellow);
                Sphere(size / 40f, 15, 15, ligthX, ligthY, lightZ, 0, true);


                GL.Disable(EnableCap.Texture2D);
                colors = new Color[]
                {
                    Color.Transparent, Color.Orange, Color.DarkSlateBlue, Color.DarkSlateBlue, Color.Sienna, Color.Orange
                };

                float k = 12f;
                GL.Enable(EnableCap.Texture2D);
                GL.BindTexture(TextureTarget.Texture2D, earthTexture);

                DrawCube(0, size, size, size * 3, 0, 0, 0, 100, colors, false);
                GL.Disable(EnableCap.Texture2D);

                DrawCube(0, size, size / 25f, size / 10, 0, size - size / 5, 0, 70, colors);

                //axes
                //GL.Color3(Color.Red);
                //GL.Begin(PrimitiveType.Lines);

                //GL.Vertex3(0, 0, 0);
                //GL.Vertex3(20, 0, 0);
                //GL.Color3(Color.Yellow);
                //GL.Vertex3(0, 0, 0);
                //GL.Vertex3(0, 40, 0);
                //GL.Color3(Color.Green);
                //GL.Vertex3(0, 0, 0);
                //GL.Vertex3(0, 0, 60);
                //GL.End();

                k = 10;

                GL.Enable(EnableCap.Texture2D);
                GL.Color3(Color.Transparent);
                GL.BindTexture(TextureTarget.Texture2D, current_texture);
                Sphere(size / k, 80, 80, size / k + 2, size / k, size / k, 0);
                GL.BindTexture(TextureTarget.Texture2D, earthTexture);
                Sphere(size / k, 80, 80, size - size / k, size / k, size / k + 2, 0);
                GL.Disable(EnableCap.Texture2D);



                GL.Enable(EnableCap.Texture2D);
                GL.BindTexture(TextureTarget.Texture2D, footBallTextur);
                Sphere(size / 12, 60, 60, size / 3 - size / 12, size / 12, size / 2, 0);

                GL.Disable(EnableCap.Texture2D);
                // GL.Translate(size / 2, size / 2, 0);

                GL.Color4(0.2f, 0.7f, 0.7f, 0.4f);
                Sphere(size / 6f, 70, 70, size / 2f - size / 6f, size / 2f - size / 6f, size / 2f - size / 6f, 0);

                game.SwapBuffers();
            };

            // Run the game at 60 updates per second
            game.Run(60f);
        }
        static void Main()
        {
            using (var w = new GameWindow(720, 480, null, "ComGr", GameWindowFlags.Default, DisplayDevice.Default, 4, 0, OpenTK.Graphics.GraphicsContextFlags.ForwardCompatible))
            {
                int hProgram = 0;

                int   vaoTriangle        = 0;
                int[] triangleIndices    = null;
                int   vboTriangleIndices = 0;

                double time = 0;

                w.Load += (o, ea) =>
                {
                    //set up opengl
                    GL.Enable(EnableCap.FramebufferSrgb);
                    GL.ClearColor(0.5f, 0.5f, 0.5f, 0);
                    //GL.ClearDepth(1);
                    //GL.Disable(EnableCap.DepthTest);
                    //GL.DepthFunc(DepthFunction.Less);
                    //GL.Disable(EnableCap.CullFace);

                    //load, compile and link shaders
                    //see https://www.khronos.org/opengl/wiki/Vertex_Shader
                    var VertexShaderSource = @"
                        #version 400 core

                        in vec3 pos;
                        uniform float time;
                        out float someFloat;

                        void main()
                        {
                          gl_Position = vec4(pos, 1.0) + vec4(sin(time) * 0.5, cos(time) * 0.5, 0.0, 0.0);
                          someFloat = pos.x + 0.5;
                        }
                        ";
                    var hVertexShader      = GL.CreateShader(ShaderType.VertexShader);
                    GL.ShaderSource(hVertexShader, VertexShaderSource);
                    GL.CompileShader(hVertexShader);
                    GL.GetShader(hVertexShader, ShaderParameter.CompileStatus, out int status);
                    if (status != 1)
                    {
                        throw new Exception(GL.GetShaderInfoLog(hVertexShader));
                    }

                    //see https://www.khronos.org/opengl/wiki/Fragment_Shader
                    var FragmentShaderSource = @"
                        #version 400 core

                        out vec4 colour;
                        in float someFloat;

                        void main()
                        {
                          colour = vec4(someFloat, 0.75, 0.0, 1.0);
                        }
                        ";
                    var hFragmentShader      = GL.CreateShader(ShaderType.FragmentShader);
                    GL.ShaderSource(hFragmentShader, FragmentShaderSource);
                    GL.CompileShader(hFragmentShader);
                    GL.GetShader(hFragmentShader, ShaderParameter.CompileStatus, out status);
                    if (status != 1)
                    {
                        throw new Exception(GL.GetShaderInfoLog(hFragmentShader));
                    }

                    //link shaders to a program
                    hProgram = GL.CreateProgram();
                    GL.AttachShader(hProgram, hFragmentShader);
                    GL.AttachShader(hProgram, hVertexShader);
                    GL.LinkProgram(hProgram);
                    GL.GetProgram(hProgram, GetProgramParameterName.LinkStatus, out status);
                    if (status != 1)
                    {
                        throw new Exception(GL.GetProgramInfoLog(hProgram));
                    }

                    //upload model vertices to a vbo
                    var triangleVertices = new float[]
                    {
                        0.0f, -0.5f, 0.0f,
                        0.5f, 0.5f, 0.0f,
                        -0.5f, 0.5f, 0.0f
                    };
                    var vboTriangleVertices = GL.GenBuffer();
                    GL.BindBuffer(BufferTarget.ArrayBuffer, vboTriangleVertices);
                    GL.BufferData(BufferTarget.ArrayBuffer, triangleVertices.Length * sizeof(float), triangleVertices, BufferUsageHint.StaticDraw);

                    // upload model indices to a vbo
                    triangleIndices    = new int[] { 0, 1, 2 };
                    vboTriangleIndices = GL.GenBuffer();
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, vboTriangleIndices);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, triangleIndices.Length * sizeof(int), triangleIndices, BufferUsageHint.StaticDraw);

                    //set up a vao
                    vaoTriangle = GL.GenVertexArray();
                    GL.BindVertexArray(vaoTriangle);
                    var posAttribIndex = GL.GetAttribLocation(hProgram, "pos");
                    if (posAttribIndex != -1)
                    {
                        GL.EnableVertexAttribArray(posAttribIndex);
                        GL.BindBuffer(BufferTarget.ArrayBuffer, vboTriangleVertices);
                        GL.VertexAttribPointer(posAttribIndex, 3, VertexAttribPointerType.Float, false, 0, 0);
                    }

                    //check for errors during all previous calls
                    var error = GL.GetError();
                    if (error != ErrorCode.NoError)
                    {
                        throw new Exception(error.ToString());
                    }
                };

                w.UpdateFrame += (o, fea) =>
                {
                    //perform logic

                    time += fea.Time;
                };

                w.RenderFrame += (o, fea) =>
                {
                    //clear screen and z-buffer
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                    //switch to our shader
                    GL.UseProgram(hProgram);
                    var timeUniformIndex = GL.GetUniformLocation(hProgram, "time");
                    if (timeUniformIndex != -1)
                    {
                        GL.Uniform1(timeUniformIndex, (float)time);
                    }

                    //model-view-projection matrix (not yet used)
                    var mvp =
                        //model
                        Matrix4.Identity

                        //view
                        * Matrix4.LookAt(new Vector3(0, 0, -10), new Vector3(0, 0, 0), new Vector3(0, 1, 0)) //view

                        //projection
                        * Matrix4.CreatePerspectiveFieldOfView(45 * (float)(Math.PI / 180d), w.ClientRectangle.Width / (float)w.ClientRectangle.Height, 0.1f, 100f);

                    //render our model
                    GL.BindVertexArray(vaoTriangle);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, vboTriangleIndices);
                    GL.DrawElements(PrimitiveType.Triangles, triangleIndices.Length, DrawElementsType.UnsignedInt, 0);

                    //display
                    w.SwapBuffers();

                    var error = GL.GetError();
                    if (error != ErrorCode.NoError)
                    {
                        throw new Exception(error.ToString());
                    }
                };

                w.Resize += (o, ea) =>
                {
                    GL.Viewport(w.ClientRectangle);
                };

                w.Run();
            }
        }
Example #4
0
 public void update()
 {
     position  = new Vector3((float)(Math.Sin(AngleX) * Math.Cos(AngleY) * Depth + offset.X), (float)(Math.Sin(AngleY) * Depth + offset.Y), (float)(Math.Cos(AngleX) * (float)Math.Cos(AngleY) * Depth + offset.Z));
     Transfrom = Matrix4.LookAt(position, LookAt, new Vector3(0, 1, 0));
 }