Example #1
0
        public void SetDefaultWorldCamera(Vector3 target)
        {
            // Do a little trigonometry, we want to look at our target from a distance of 1500 units at an angle of 70deg
            var distance     = 2000;
            var angle        = MathHelper.DegreesToRadians(60);
            var location     = target + new Vector3(0, -distance * (float)Math.Cos(angle), distance * (float)Math.Sin(angle));
            var cameraMatrix = Matrix4.CreateRotationY(angle) * Matrix4.CreateRotationZ(MathHelper.PiOver2) * Matrix4.CreateTranslation(location);

            // Set camera
            ActiveCamera = new Camera(cameraMatrix, "worldspawn");
        }
Example #2
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {//OnUpdateFrame is called every Frame
            base.OnUpdateFrame(e);

            time += (float)e.Time;

            //Buffer handeling

            GL.BindBuffer(BufferTarget.ArrayBuffer, shaders[activeShader].GetBuffer("vPosition"));

            GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(shaders[activeShader].GetAttribute("vPosition"), 3, VertexAttribPointerType.Float, false, 0, 0);

            if (shaders[activeShader].GetAttribute("vColor") != -1)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, shaders[activeShader].GetBuffer("vColor"));
                GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw);
                GL.VertexAttribPointer(shaders[activeShader].GetAttribute("vColor"), 3, VertexAttribPointerType.Float, true, 0, 0);
            }



            //uniform matrix

            /*mviewdata[0] = Matrix4.CreateRotationY(1f * time) *
             *  Matrix4.CreateRotationX(0.4f * time) *
             *  Matrix4.CreateTranslation(1.0f, 0.0f, -3.0f) *
             *  Matrix4.CreatePerspectiveFieldOfView(1.3f, ClientSize.Width / (float)ClientSize.Height, 1.0f, 40.0f);*/

            mviewdata[0] = Matrix4.CreateRotationY(1f * time) *
                           Matrix4.CreateRotationX(0.4f * time) *
                           Matrix4.CreateScale(10) *
                           Matrix4.CreateTranslation(0.0f, 0.0f, -100.0f) *
                           Matrix4.CreatePerspectiveFieldOfView(1.3f, ClientSize.Width / (float)ClientSize.Height, 1.0f, 400.0f);

            GL.UniformMatrix4(shaders[activeShader].GetUniform("modelview"), false, ref mviewdata[0]);
            //Index Buffer
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw);
            //Enable Program
            GL.UseProgram(shaders[activeShader].ProgramID);
            //Clear buffer
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            shader.Use();

            controller.NewFrame(this);

            ImGui.SliderFloat("Scale", ref scale, 0, 10);
            ImGui.SliderFloat("Angle", ref angle, -3.14f, 3.14f);
            ImGui.SliderFloat("Angle_z", ref angle_z, -3.14f, 3.14f);
            ImGui.SliderFloat("Distance", ref dist, 0, 10.0f);
            ImGui.Checkbox("Perspective", ref persp);

            shader.SetUniform("scaleFactor", scale);

            var model = Matrix4.CreateRotationY(angle) * Matrix4.CreateRotationX(angle_z) * Matrix4.CreateTranslation(0, 0, -dist);

            shader.SetUniform("model", model);

            //var projection = Matrix4.CreateOrthographic(10, 10, -2, 2);
            //var projection = Matrix4.CreateOrthographicOffCenter(0, 1, 1, 0, -2, 2);
            //var projection = Matrix4.CreatePerspectiveOffCenter(-500,1 , 1, -500, 0.1f, 100.0f);
            var projection = persp
                ? Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 2), (float)Width / Height, 0.1f, 100.0f)
                : Matrix4.CreateOrthographic(10, 10, -7, 7);


            shader.SetUniform("projection", projection);

            //scale += 0.001f;

            //GL.BindVertexArray(VertexArrayObject);
            //GL.PointSize(50);
            //GL.DrawArrays(PrimitiveType.Points, 0, 4);

            GL.DrawElements(PrimitiveType.Triangles, mesh.Indeces.Length, DrawElementsType.UnsignedInt, 0);

            controller.Render();

            Context.SwapBuffers();
            base.OnRenderFrame(e);
        }