Advertisement
Guest User

Untitled

a guest
Jun 30th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. package passage.games;
  2.  
  3. import java.nio.FloatBuffer;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.util.vector.Vector3f;
  7.  
  8. import static org.lwjgl.opengl.GL11.*;
  9. import static org.lwjgl.opengl.GL15.*;
  10.  
  11. public class Block {
  12. public Vector3f location;
  13. public Vector3f rotation;
  14.  
  15. private int vertexPointer;
  16. private int colorPointer;
  17. private int normalPointer;
  18.  
  19. private FloatBuffer vertexBuffer;
  20. private FloatBuffer colorBuffer;
  21. private FloatBuffer normalBuffer;
  22.  
  23. private float[] vertexData = {
  24. -1.0f,-1.0f,-1.0f,
  25. -1.0f,-1.0f, 1.0f,
  26. -1.0f, 1.0f, 1.0f,
  27.  
  28. 1.0f, 1.0f,-1.0f,
  29. -1.0f,-1.0f,-1.0f,
  30. -1.0f, 1.0f,-1.0f,
  31.  
  32. 1.0f,-1.0f, 1.0f,
  33. -1.0f,-1.0f,-1.0f,
  34. 1.0f,-1.0f,-1.0f,
  35.  
  36. 1.0f, 1.0f,-1.0f,
  37. 1.0f,-1.0f,-1.0f,
  38. -1.0f,-1.0f,-1.0f,
  39.  
  40. -1.0f,-1.0f,-1.0f,
  41. -1.0f, 1.0f, 1.0f,
  42. -1.0f, 1.0f,-1.0f,
  43.  
  44. 1.0f,-1.0f, 1.0f,
  45. -1.0f,-1.0f, 1.0f,
  46. -1.0f,-1.0f,-1.0f,
  47.  
  48. -1.0f, 1.0f, 1.0f,
  49. -1.0f,-1.0f, 1.0f,
  50. 1.0f,-1.0f, 1.0f,
  51.  
  52. 1.0f, 1.0f, 1.0f,
  53. 1.0f,-1.0f,-1.0f,
  54. 1.0f, 1.0f,-1.0f,
  55.  
  56. 1.0f,-1.0f,-1.0f,
  57. 1.0f, 1.0f, 1.0f,
  58. 1.0f,-1.0f, 1.0f,
  59.  
  60. 1.0f, 1.0f, 1.0f,
  61. 1.0f, 1.0f,-1.0f,
  62. -1.0f, 1.0f,-1.0f,
  63.  
  64. 1.0f, 1.0f, 1.0f,
  65. -1.0f, 1.0f,-1.0f,
  66. -1.0f, 1.0f, 1.0f,
  67.  
  68. 1.0f, 1.0f, 1.0f,
  69. -1.0f, 1.0f, 1.0f,
  70. 1.0f, -1.0f, 1.0f
  71. };
  72.  
  73. public float[] colorData = {
  74. 0.583f, 0.771f, 0.014f,
  75. 0.609f, 0.115f, 0.436f,
  76. 0.327f, 0.483f, 0.844f,
  77.  
  78. 0.822f, 0.569f, 0.201f,
  79. 0.435f, 0.602f, 0.223f,
  80. 0.310f, 0.747f, 0.185f,
  81.  
  82. 0.597f, 0.770f, 0.761f,
  83. 0.559f, 0.436f, 0.730f,
  84. 0.359f, 0.583f, 0.152f,
  85.  
  86. 0.483f, 0.596f, 0.789f,
  87. 0.559f, 0.861f, 0.639f,
  88. 0.195f, 0.548f, 0.859f,
  89.  
  90. 0.014f, 0.184f, 0.576f,
  91. 0.771f, 0.328f, 0.970f,
  92. 0.406f, 0.615f, 0.116f,
  93.  
  94. 0.676f, 0.977f, 0.133f,
  95. 0.971f, 0.572f, 0.833f,
  96. 0.140f, 0.616f, 0.489f,
  97.  
  98. 0.997f, 0.513f, 0.064f,
  99. 0.945f, 0.719f, 0.592f,
  100. 0.543f, 0.021f, 0.978f,
  101.  
  102. 0.279f, 0.317f, 0.505f,
  103. 0.167f, 0.620f, 0.077f,
  104. 0.347f, 0.857f, 0.137f,
  105.  
  106. 0.055f, 0.953f, 0.042f,
  107. 0.714f, 0.505f, 0.345f,
  108. 0.783f, 0.290f, 0.734f,
  109.  
  110. 0.722f, 0.645f, 0.174f,
  111. 0.302f, 0.455f, 0.848f,
  112. 0.225f, 0.587f, 0.040f,
  113.  
  114. 0.517f, 0.713f, 0.338f,
  115. 0.053f, 0.959f, 0.120f,
  116. 0.393f, 0.621f, 0.362f,
  117.  
  118. 0.673f, 0.211f, 0.457f,
  119. 0.820f, 0.883f, 0.371f,
  120. 0.982f, 0.099f, 0.879f,
  121. };
  122.  
  123. public float[] normalData = new float[108];
  124.  
  125. public Block(Vector3f location, Vector3f rotation){
  126. this.location = location;
  127. this.rotation = rotation;
  128. }
  129.  
  130. public Block(Vector3f location){
  131. this.location = location;
  132. this.rotation = new Vector3f(0, 0, 0);
  133. }
  134.  
  135. public Block(){
  136. this.location = new Vector3f(0, 0, 0);
  137. this.rotation = new Vector3f(0, 0, 0);
  138. }
  139.  
  140. public void init(){
  141. for (int i = 0; i < vertexData.length; i += 9) {
  142. float[] v1 = {vertexData[i], vertexData[i + 1], vertexData[i + 2]};
  143. float[] v2 = {vertexData[i + 3], vertexData[i + 4], vertexData[i + 5]};
  144. float[] v3 = {vertexData[i + 6], vertexData[i + 7], vertexData[i + 8]};
  145.  
  146. float[] triangleNormal = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);
  147.  
  148. normalData[i] = triangleNormal[0];
  149. normalData[i + 1] = triangleNormal[1];
  150. normalData[i + 2] = triangleNormal[2];
  151. normalData[i + 3] = triangleNormal[0];
  152. normalData[i + 4] = triangleNormal[1];
  153. normalData[i + 5] = triangleNormal[2];
  154. normalData[i + 6] = triangleNormal[0];
  155. normalData[i + 7] = triangleNormal[1];
  156. normalData[i + 8] = triangleNormal[2];
  157. }
  158.  
  159. vertexBuffer = BufferUtils.createFloatBuffer(108);
  160. vertexBuffer.put(vertexData);
  161. vertexBuffer.flip();
  162.  
  163. colorBuffer = BufferUtils.createFloatBuffer(108);
  164. colorBuffer.put(colorData);
  165. colorBuffer.flip();
  166.  
  167. normalBuffer = BufferUtils.createFloatBuffer(108);
  168. normalBuffer.put(normalData);
  169. normalBuffer.flip();
  170.  
  171. vertexPointer = glGenBuffers();
  172. glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
  173. glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
  174. glBindBuffer(GL_ARRAY_BUFFER, 0);
  175.  
  176. colorPointer = glGenBuffers();
  177. glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
  178. glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
  179. glBindBuffer(GL_ARRAY_BUFFER, 0);
  180.  
  181. normalPointer = glGenBuffers();
  182. glBindBuffer(GL_ARRAY_BUFFER, normalPointer);
  183. glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);
  184. glBindBuffer(GL_ARRAY_BUFFER, 0);
  185. }
  186.  
  187. public void render() {
  188. glPushMatrix();
  189.  
  190. glTranslatef(location.x, location.y, location.z);
  191.  
  192. glEnableClientState(GL_VERTEX_ARRAY);
  193. glEnableClientState(GL_COLOR_ARRAY);
  194. glEnableClientState(GL_NORMAL_ARRAY);
  195.  
  196. glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
  197. glVertexPointer(3, GL_FLOAT, 0, 0);
  198.  
  199. glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
  200. glColorPointer(3, GL_FLOAT, 0, 0);
  201.  
  202. glBindBuffer(GL_ARRAY_BUFFER, normalPointer);
  203. glNormalPointer(GL_FLOAT, 0, 0);
  204.  
  205. glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
  206.  
  207. glDisableClientState(GL_VERTEX_ARRAY);
  208. glDisableClientState(GL_COLOR_ARRAY);
  209. glDisableClientState(GL_NORMAL_ARRAY);
  210. glPopMatrix();
  211. }
  212.  
  213. public void update(){
  214.  
  215. }
  216.  
  217. public void destroy(){
  218. glDeleteBuffers(vertexPointer);
  219. glDeleteBuffers(normalPointer);
  220. glDeleteBuffers(colorPointer);
  221. }
  222.  
  223. public static float[] getTriangleNormal(float argument0, float argument1,
  224. float argument2, float argument3, float argument4, float argument5,
  225. float argument6, float argument7, float argument8) {
  226. float ax, ay, az, bx, by, bz, m, rx, ry, rz;
  227.  
  228. // point0 -> point1
  229. ax = argument3 - argument0;
  230. ay = argument4 - argument1;
  231. az = argument5 - argument2;
  232.  
  233. // point0 -> point2
  234. bx = argument6 - argument0;
  235. by = argument7 - argument1;
  236. bz = argument8 - argument2;
  237.  
  238. // cross product
  239. rx = ay * bz - by * az;
  240. ry = az * bx - bz * ax;
  241. rz = ax * by - bx * ay;
  242.  
  243. // magnitude
  244. m = (float) Math.sqrt(rx * rx + ry * ry + rz * rz);
  245.  
  246. // normalize
  247. rx /= m;
  248. ry /= m;
  249. rz /= m;
  250.  
  251. return new float[] { rx, ry, rz };
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement