Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OpenGL_Testbed.Lib;
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using System;
- using System.IO;
- using OpenTK.Input;
- namespace OpenGL_Testbed
- {
- public class Cameras : GameWindow
- {
- private ShaderProgram shaderProgram;
- private VertexBuffer vbo;
- private VertexArray vao;
- private Texture texture1;
- private Texture texture2;
- //Camera variables
- private Vector3 pos = new Vector3(0, 0, 3);
- private Vector3 front = new Vector3(0.0f, 0.0f, -1.0f), right, up = new Vector3(0.0F, 1.0F, 0.0F);
- private double pitch, yaw = -90.0F;
- private int lastMouseX, lastMouseY;
- private bool firstMouse = true;
- private float deltaTime = 0.0F; //Time between the current frame and the last one
- private float lastFrameTime = 0.0F; //The timestamp of the last frame
- private readonly float[] vertices =
- {
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
- };
- public Cameras() : base(800, 600, GraphicsMode.Default, "Test Game", GameWindowFlags.FixedWindow, DisplayDevice.Default, 3, 3, GraphicsContextFlags.Default) {}
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- GL.Viewport(0, 0, this.Width, this.Height);
- }
- protected override void OnLoad(EventArgs e)
- {
- //Setup the viewport & capture the mouse
- GL.Viewport(0, 0, this.Width, this.Height);
- CursorVisible = false;
- //Load the textures
- try
- {
- texture1 = new Texture("resources/textures/container.jpg");
- texture2 = new Texture("resources/textures/awesomeface.png");
- }
- catch (FileNotFoundException fnf)
- {
- Console.Write(fnf.ToString());
- }
- //Create a vertex shader
- Shader vertShader = Shader.CreateShaderFromFile(ShaderType.VertexShader, "coordSystemsVertex");
- //Create a fragment shader
- Shader fragShader = Shader.CreateShaderFromFile(ShaderType.FragmentShader, "coordSystemsFragment");
- //Create a shader program
- shaderProgram = new ShaderProgram();
- shaderProgram.Attach(vertShader, delete: true);
- shaderProgram.Attach(fragShader, delete: true);
- shaderProgram.Link("shaderProgram");
- vao = new VertexArray();
- vao.Bind();
- //Create a VBO(Vertex Buffer Object)
- vbo = new VertexBuffer();
- vbo.Bind();
- vbo.SetData(vertices, BufferUsageHint.StaticDraw);
- GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
- GL.EnableVertexAttribArray(0);
- GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
- GL.EnableVertexAttribArray(1);
- vbo.Unbind();
- vao.Unbind();
- shaderProgram.Use();
- shaderProgram.SetUniform("texture1", 0);
- shaderProgram.SetUniform("texture2", 1);
- Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0F), (float)Width / Height, 0.1F, 100.0F);
- shaderProgram.SetUniform("projection", projection);
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- base.OnRenderFrame(e);
- GL.Enable(EnableCap.DepthTest);
- GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- float currentFrame = Environment.TickCount / 1000.0F;
- deltaTime = currentFrame - lastFrameTime;
- lastFrameTime = currentFrame;
- GL.ActiveTexture(TextureUnit.Texture0);
- texture1.Bind();
- GL.ActiveTexture(TextureUnit.Texture1);
- texture2.Bind();
- Matrix4 view = Matrix4.LookAt(pos, pos + front, up);
- shaderProgram.SetUniform("view", view);
- Vector3[] cubePositions =
- {
- new Vector3( 0.0f, 0.0f, 0.0f),
- new Vector3( 2.0f, 5.0f, -15.0f),
- new Vector3(-1.5f, -2.2f, -2.5f),
- new Vector3(-3.8f, -2.0f, -12.3f),
- new Vector3( 2.4f, -0.4f, -3.5f),
- new Vector3(-1.7f, 3.0f, -7.5f),
- new Vector3( 1.3f, -2.0f, -2.5f),
- new Vector3( 1.5f, 2.0f, -2.5f),
- new Vector3( 1.5f, 0.2f, -1.5f),
- new Vector3(-1.3f, 1.0f, -1.5f)
- };
- vao.Bind();
- for(int i = 0; i < 10; i++)
- {
- float angle = i * 20;
- Matrix4 model = Matrix4.CreateFromAxisAngle(new Vector3(1.0f, 0.3f, 0.5f), MathHelper.DegreesToRadians(angle)) * Matrix4.CreateTranslation(cubePositions[i]);
- shaderProgram.SetUniform("model", model);
- GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
- }
- vao.Unbind();
- SwapBuffers();
- }
- private void RecalculateLocalUnitVectors()
- {
- right = Vector3.Normalize(Vector3.Cross(Vector3.UnitY, front));
- up = Vector3.Cross(front, right);
- }
- public void MoveCamera(float x = 0.0F, float y = 0.0F, float z = 0.0F)
- {
- MoveCamera(new Vector3(x, y, z));
- }
- public void MoveCamera(Vector3 xyz)
- {
- pos += xyz;
- }
- protected override void OnKeyDown(KeyboardKeyEventArgs e)
- {
- float moveSpeed = 2.5F * deltaTime;
- if (Keyboard.GetState().IsKeyDown(Key.Escape)) CursorVisible = true;
- if (Keyboard.GetState().IsKeyDown(Key.W)) MoveCamera(moveSpeed * front);
- if (Keyboard.GetState().IsKeyDown(Key.S)) MoveCamera(-moveSpeed * front);
- if (Keyboard.GetState().IsKeyDown(Key.A)) MoveCamera(moveSpeed * right);
- if (Keyboard.GetState().IsKeyDown(Key.D)) MoveCamera(-moveSpeed * right);
- }
- protected override void OnMouseDown(MouseButtonEventArgs e) { CursorVisible = false; }
- protected override void OnMouseMove(MouseMoveEventArgs e)
- {
- if (CursorVisible) return;
- if(firstMouse)
- {
- lastMouseX = e.X;
- lastMouseY = e.Y;
- firstMouse = false;
- }
- double xOffset = e.X - lastMouseX;
- double yOffset = e.Y - lastMouseY;
- lastMouseX = e.X;
- lastMouseY = e.Y;
- double sensitivity = 0.05F;
- xOffset *= sensitivity;
- yOffset *= sensitivity;
- yaw = yaw + xOffset;
- pitch = pitch + yOffset;
- pitch = MathHelper.Clamp(pitch, -89.0D, 89.0D);
- Vector3d tempFront;
- tempFront.X = Math.Cos(MathHelper.DegreesToRadians(pitch)) * Math.Cos(MathHelper.DegreesToRadians(yaw));
- tempFront.Y = Math.Sin(MathHelper.DegreesToRadians(pitch));
- tempFront.Z = Math.Cos(MathHelper.DegreesToRadians(pitch)) * Math.Sin(MathHelper.DegreesToRadians(yaw));
- front = (Vector3) Vector3d.Normalize(tempFront);
- RecalculateLocalUnitVectors();
- }
- protected override void OnClosed(EventArgs e)
- {
- base.OnClosed(e);
- //Cleanup
- vao.Delete();
- vbo.Delete();
- }
- }
- }
Add Comment
Please, Sign In to add comment