Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. using OpenTK;
  2. using System;
  3. using OpenTK.Graphics;
  4. using Labs.Utility;
  5. using OpenTK.Graphics.OpenGL;
  6.  
  7. namespace Labs.ACW
  8. {
  9. class cube
  10. {
  11. public enum cubeType
  12. {
  13. Bottom,
  14. Middle,
  15. Top
  16. };
  17.  
  18. Matrix4 Position;
  19. cubeType cubeStructure;
  20.  
  21. public cube(string inCubeStructure, Matrix4 inPosition)
  22. {
  23. switch (inCubeStructure)
  24. {
  25. case "Top":
  26. cubeStructure = cubeType.Top;
  27. break;
  28. case "Middle":
  29. cubeStructure = cubeType.Middle;
  30. break;
  31. case "Bottom":
  32. cubeStructure = cubeType.Bottom;
  33. break;
  34. }
  35.  
  36. Position = inPosition;
  37. }
  38.  
  39. public void OnLoad(EventArgs e)
  40. {
  41. float[] cubeVertices = new float[] {
  42. // Top
  43. 0, 1, 0, 0, 1, 0,
  44. 1, 1, 0, 0, 1, 0,
  45. 1, 1, 1, 0, 1, 0,
  46.  
  47. 0, 1, 0, 0, 1, 0,
  48. 1, 1, 1, 0, 1, 0,
  49. 0, 1, 1, 0, 1, 0,
  50.  
  51. // Side 1 - Front Side
  52. 0, 1, 1, 0, 1, 0,
  53. 1, 1, 1, 0, 1, 0,
  54. 1, 0, 1, 0, 1, 0,
  55.  
  56. 0, 1, 1, 0, 1, 0,
  57. 1, 0, 1, 0, 1, 0,
  58. 0, 0, 1, 0, 1, 0,
  59.  
  60. // Side 2 - Left Side
  61. 0, 1, 0, 0, 1, 0,
  62. 0, 0, 1, 0, 1, 0,
  63. 0, 0, 0, 0, 1, 0,
  64.  
  65. 0, 1, 0, 0, 1, 0,
  66. 0, 1, 1, 0, 1, 0,
  67. 0, 0, 1, 0, 1, 0,
  68.  
  69. // Side 3 - Back Side
  70. 1, 1, 0, 0, 1, 0,
  71. 0, 0, 0, 0, 1, 0,
  72. 1, 0, 0, 0, 1, 0,
  73.  
  74. 1, 1, 0, 0, 1, 0,
  75. 0, 1, 0, 0, 1, 0,
  76. 0, 0, 0, 0, 1, 0,
  77.  
  78. // Side 4 - Right Side
  79. 1, 1, 1, 0, 1, 0,
  80. 1, 0, 0, 0, 1, 0,
  81. 1, 0, 1, 0, 1, 0,
  82.  
  83. 1, 1, 1, 0, 1, 0,
  84. 1, 1, 0, 0, 1, 0,
  85. 1, 0, 0, 0, 1, 0,
  86.  
  87. // Bottom
  88. 0, 0, 0, 0, 1, 0,
  89. 0, 0, 1, 0, 1, 0,
  90. 1, 0, 1, 0, 1, 0,
  91.  
  92. 0, 0, 0, 0, 1, 0,
  93. 1, 0, 1, 0, 1, 0,
  94. 1, 0, 0, 0, 1, 0
  95. };
  96.  
  97. // Bind Vertex Array
  98. GL.BindVertexArray(ACWWindow.mVertexArrayObjectIDArray[0]);
  99.  
  100. // Bind Vertex Buffer
  101. GL.BindBuffer(BufferTarget.ArrayBuffer, ACWWindow.mVertexBufferObjectIDArray[0]);
  102. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(cubeVertices.Length * sizeof(float)), cubeVertices, BufferUsageHint.StaticDraw);
  103.  
  104. // Ensure Cube is loaded correctly
  105. int size;
  106. GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
  107. if (cubeVertices.Length * sizeof(float) != size)
  108. {
  109. throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
  110. }
  111.  
  112. int vPositionLocation = GL.GetAttribLocation(ACWWindow.mShader.ShaderProgramID, "vPosition");
  113. int vNormalLocation = GL.GetAttribLocation(ACWWindow.mShader.ShaderProgramID, "vNormal");
  114.  
  115. // Position and Normal
  116. GL.EnableVertexAttribArray(vPositionLocation);
  117. GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
  118. GL.EnableVertexAttribArray(vNormalLocation);
  119. GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 3 * sizeof(float));
  120. }
  121.  
  122. public void OnRenderFrame(FrameEventArgs e)
  123. {
  124. // Draw Cube
  125. int uModel = GL.GetUniformLocation(ACWWindow.mShader.ShaderProgramID, "uModel");
  126. GL.UniformMatrix4(uModel, true, ref Position);
  127. GL.BindVertexArray(ACWWindow.mVertexArrayObjectIDArray[0]);
  128.  
  129. switch (cubeStructure)
  130. {
  131. case cubeType.Top:
  132. GL.DrawArrays(PrimitiveType.Triangles, 0, 30);
  133. break;
  134. case cubeType.Middle:
  135. GL.DrawArrays(PrimitiveType.Triangles, 6, 30);
  136. break;
  137. case cubeType.Bottom:
  138. GL.DrawArrays(PrimitiveType.Triangles, 6, 36);
  139. break;
  140. }
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement