Guest User

Untitled

a guest
Dec 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. using OpenGL_Testbed.Lib;
  2. using OpenTK;
  3. using OpenTK.Graphics;
  4. using OpenTK.Graphics.OpenGL;
  5. using System;
  6. using System.IO;
  7. using OpenTK.Input;
  8.  
  9. namespace OpenGL_Testbed
  10. {
  11. public class Cameras : GameWindow
  12. {
  13. private ShaderProgram shaderProgram;
  14. private VertexBuffer vbo;
  15. private VertexArray vao;
  16. private Texture texture1;
  17. private Texture texture2;
  18.  
  19. //Camera variables
  20. private Vector3 pos = new Vector3(0, 0, 3);
  21. private Vector3 front = new Vector3(0.0f, 0.0f, -1.0f), right, up = new Vector3(0.0F, 1.0F, 0.0F);
  22. private double pitch, yaw = -90.0F;
  23.  
  24. private int lastMouseX, lastMouseY;
  25. private bool firstMouse = true;
  26.  
  27. private float deltaTime = 0.0F; //Time between the current frame and the last one
  28. private float lastFrameTime = 0.0F; //The timestamp of the last frame
  29.  
  30. private readonly float[] vertices =
  31. {
  32. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
  33. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
  34. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
  35. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
  36. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
  37. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
  38.  
  39. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  40. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
  41. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
  42. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
  43. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
  44. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  45.  
  46. -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  47. -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
  48. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  49. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  50. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  51. -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  52.  
  53. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  54. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
  55. 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  56. 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  57. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  58. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  59.  
  60. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  61. 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
  62. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
  63. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
  64. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  65. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  66.  
  67. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
  68. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
  69. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  70. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  71. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
  72. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
  73. };
  74.  
  75. public Cameras() : base(800, 600, GraphicsMode.Default, "Test Game", GameWindowFlags.FixedWindow, DisplayDevice.Default, 3, 3, GraphicsContextFlags.Default) {}
  76.  
  77. protected override void OnResize(EventArgs e)
  78. {
  79. base.OnResize(e);
  80. GL.Viewport(0, 0, this.Width, this.Height);
  81. }
  82.  
  83. protected override void OnLoad(EventArgs e)
  84. {
  85. //Setup the viewport & capture the mouse
  86. GL.Viewport(0, 0, this.Width, this.Height);
  87.  
  88. CursorVisible = false;
  89.  
  90. //Load the textures
  91. try
  92. {
  93. texture1 = new Texture("resources/textures/container.jpg");
  94. texture2 = new Texture("resources/textures/awesomeface.png");
  95. }
  96. catch (FileNotFoundException fnf)
  97. {
  98. Console.Write(fnf.ToString());
  99. }
  100.  
  101. //Create a vertex shader
  102. Shader vertShader = Shader.CreateShaderFromFile(ShaderType.VertexShader, "coordSystemsVertex");
  103.  
  104. //Create a fragment shader
  105. Shader fragShader = Shader.CreateShaderFromFile(ShaderType.FragmentShader, "coordSystemsFragment");
  106.  
  107. //Create a shader program
  108. shaderProgram = new ShaderProgram();
  109. shaderProgram.Attach(vertShader, delete: true);
  110. shaderProgram.Attach(fragShader, delete: true);
  111. shaderProgram.Link("shaderProgram");
  112.  
  113. vao = new VertexArray();
  114.  
  115. vao.Bind();
  116. //Create a VBO(Vertex Buffer Object)
  117. vbo = new VertexBuffer();
  118. vbo.Bind();
  119. vbo.SetData(vertices, BufferUsageHint.StaticDraw);
  120.  
  121. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
  122. GL.EnableVertexAttribArray(0);
  123.  
  124. GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
  125. GL.EnableVertexAttribArray(1);
  126.  
  127. vbo.Unbind();
  128. vao.Unbind();
  129.  
  130. shaderProgram.Use();
  131. shaderProgram.SetUniform("texture1", 0);
  132. shaderProgram.SetUniform("texture2", 1);
  133.  
  134. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0F), (float)Width / Height, 0.1F, 100.0F);
  135. shaderProgram.SetUniform("projection", projection);
  136. }
  137.  
  138. protected override void OnRenderFrame(FrameEventArgs e)
  139. {
  140. base.OnRenderFrame(e);
  141. GL.Enable(EnableCap.DepthTest);
  142. GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  143. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  144.  
  145. float currentFrame = Environment.TickCount / 1000.0F;
  146. deltaTime = currentFrame - lastFrameTime;
  147. lastFrameTime = currentFrame;
  148.  
  149. GL.ActiveTexture(TextureUnit.Texture0);
  150. texture1.Bind();
  151. GL.ActiveTexture(TextureUnit.Texture1);
  152. texture2.Bind();
  153.  
  154. Matrix4 view = Matrix4.LookAt(pos, pos + front, up);
  155. shaderProgram.SetUniform("view", view);
  156.  
  157. Vector3[] cubePositions =
  158. {
  159. new Vector3( 0.0f, 0.0f, 0.0f),
  160. new Vector3( 2.0f, 5.0f, -15.0f),
  161. new Vector3(-1.5f, -2.2f, -2.5f),
  162. new Vector3(-3.8f, -2.0f, -12.3f),
  163. new Vector3( 2.4f, -0.4f, -3.5f),
  164. new Vector3(-1.7f, 3.0f, -7.5f),
  165. new Vector3( 1.3f, -2.0f, -2.5f),
  166. new Vector3( 1.5f, 2.0f, -2.5f),
  167. new Vector3( 1.5f, 0.2f, -1.5f),
  168. new Vector3(-1.3f, 1.0f, -1.5f)
  169. };
  170.  
  171. vao.Bind();
  172. for(int i = 0; i < 10; i++)
  173. {
  174. float angle = i * 20;
  175. Matrix4 model = Matrix4.CreateFromAxisAngle(new Vector3(1.0f, 0.3f, 0.5f), MathHelper.DegreesToRadians(angle)) * Matrix4.CreateTranslation(cubePositions[i]);
  176. shaderProgram.SetUniform("model", model);
  177.  
  178. GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
  179. }
  180. vao.Unbind();
  181.  
  182. SwapBuffers();
  183. }
  184.  
  185. private void RecalculateLocalUnitVectors()
  186. {
  187. right = Vector3.Normalize(Vector3.Cross(Vector3.UnitY, front));
  188. up = Vector3.Cross(front, right);
  189. }
  190.  
  191. public void MoveCamera(float x = 0.0F, float y = 0.0F, float z = 0.0F)
  192. {
  193. MoveCamera(new Vector3(x, y, z));
  194. }
  195.  
  196. public void MoveCamera(Vector3 xyz)
  197. {
  198. pos += xyz;
  199. }
  200.  
  201. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  202. {
  203. float moveSpeed = 2.5F * deltaTime;
  204. if (Keyboard.GetState().IsKeyDown(Key.Escape)) CursorVisible = true;
  205. if (Keyboard.GetState().IsKeyDown(Key.W)) MoveCamera(moveSpeed * front);
  206. if (Keyboard.GetState().IsKeyDown(Key.S)) MoveCamera(-moveSpeed * front);
  207. if (Keyboard.GetState().IsKeyDown(Key.A)) MoveCamera(moveSpeed * right);
  208. if (Keyboard.GetState().IsKeyDown(Key.D)) MoveCamera(-moveSpeed * right);
  209. }
  210.  
  211. protected override void OnMouseDown(MouseButtonEventArgs e) { CursorVisible = false; }
  212.  
  213. protected override void OnMouseMove(MouseMoveEventArgs e)
  214. {
  215. if (CursorVisible) return;
  216. if(firstMouse)
  217. {
  218. lastMouseX = e.X;
  219. lastMouseY = e.Y;
  220. firstMouse = false;
  221. }
  222.  
  223. double xOffset = e.X - lastMouseX;
  224. double yOffset = e.Y - lastMouseY;
  225.  
  226. lastMouseX = e.X;
  227. lastMouseY = e.Y;
  228.  
  229. double sensitivity = 0.05F;
  230. xOffset *= sensitivity;
  231. yOffset *= sensitivity;
  232. yaw = yaw + xOffset;
  233. pitch = pitch + yOffset;
  234. pitch = MathHelper.Clamp(pitch, -89.0D, 89.0D);
  235.  
  236. Vector3d tempFront;
  237. tempFront.X = Math.Cos(MathHelper.DegreesToRadians(pitch)) * Math.Cos(MathHelper.DegreesToRadians(yaw));
  238. tempFront.Y = Math.Sin(MathHelper.DegreesToRadians(pitch));
  239. tempFront.Z = Math.Cos(MathHelper.DegreesToRadians(pitch)) * Math.Sin(MathHelper.DegreesToRadians(yaw));
  240. front = (Vector3) Vector3d.Normalize(tempFront);
  241. RecalculateLocalUnitVectors();
  242. }
  243.  
  244. protected override void OnClosed(EventArgs e)
  245. {
  246. base.OnClosed(e);
  247.  
  248. //Cleanup
  249. vao.Delete();
  250. vbo.Delete();
  251. }
  252. }
  253. }
Add Comment
Please, Sign In to add comment