Advertisement
Guest User

Untitled

a guest
Aug 10th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. public class Model {
  2.  
  3. ArrayList<Vertex> vertices = new ArrayList<Vertex>();
  4. ArrayList<Vector3f> pos = new ArrayList<Vector3f>();
  5. ArrayList<Vector2f> tex = new ArrayList<Vector2f>();
  6. ArrayList<Vector3f> norm = new ArrayList<Vector3f>();
  7. ArrayList<FaceData> faces = new ArrayList<FaceData>();
  8.  
  9. public ModelTransformation transformation;
  10.  
  11. int vbo;
  12.  
  13. public int indexCount;
  14. public int vertexCount;
  15. Texture texture;
  16.  
  17. public LightingTechnique effect;
  18.  
  19. public Model(String name)
  20. {
  21. texture = new Texture("asd", "/res/texture.png", GL13.GL_TEXTURE0);
  22. parseObj(name);
  23.  
  24. FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.size() * 8);
  25.  
  26. for(Vertex v : vertices)
  27. {
  28. vertBuffer.put(v.getElements());
  29.  
  30. vertexCount++;
  31. }
  32.  
  33. vertBuffer.flip();
  34.  
  35. vbo = GL15.glGenBuffers();
  36. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
  37. GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
  38. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  39.  
  40. effect = new LightingTechnique();
  41. effect.init();
  42. effect.enable();
  43. effect.setTextureUnit(0);
  44. effect.disable();
  45.  
  46. transformation = new ModelTransformation();
  47. transformation.worldPos(0, 0, 0);
  48. }
  49.  
  50. public void startRender()
  51. {
  52. effect.enable();
  53.  
  54. effect.setWorld(transformation.getWorld());
  55. effect.setWVP(transformation.getWVP(Main.cam));
  56. effect.setEyeWorldPos(Main.cam.pos);
  57. effect.setMatSpecularIntensity(0.0f);
  58. effect.setMatSpecularPower(0);
  59.  
  60. GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
  61.  
  62. GL20.glEnableVertexAttribArray(0);
  63. GL20.glEnableVertexAttribArray(1);
  64. GL20.glEnableVertexAttribArray(2);
  65.  
  66. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
  67. GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 32, 0);
  68. GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 32, 12);
  69. GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 32, 20);
  70. }
  71.  
  72. public void render()
  73. {
  74. GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertexCount);
  75. }
  76.  
  77. public void endRender()
  78. {
  79. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  80.  
  81. GL20.glDisableVertexAttribArray(0);
  82. GL20.glDisableVertexAttribArray(1);
  83. GL20.glDisableVertexAttribArray(2);
  84.  
  85. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  86.  
  87. effect.disable();
  88. }
  89.  
  90. public void parseObj(String path)
  91. {
  92. try {
  93. BufferedReader reader = new BufferedReader(new FileReader(new File("").getAbsolutePath() + "/res/" + path));
  94.  
  95. String line = reader.readLine();
  96.  
  97. while(line != null)
  98. {
  99. if(line.startsWith("v "))
  100. {
  101. int start = line.indexOf(" ");
  102. String sub = line.substring(start + 1);
  103.  
  104. String[] vertexStr = sub.split(" " );
  105.  
  106. pos.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
  107. }
  108. if(line.startsWith("vt "))
  109. {
  110. int start = line.indexOf(" ");
  111. String sub = line.substring(start + 1);
  112.  
  113. String[] vertexStr = sub.split(" " );
  114.  
  115. tex.add(new Vector2f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1])));
  116. }
  117. if(line.startsWith("vn "))
  118. {
  119. int start = line.indexOf(" ");
  120. String sub = line.substring(start + 1);
  121.  
  122. String[] vertexStr = sub.split(" " );
  123.  
  124. norm.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
  125. }
  126. if(line.startsWith("f "))
  127. {
  128. int start = line.indexOf(" ");
  129. String sub = line.substring(start + 1);
  130.  
  131. String[] vertexStr = sub.split(" ");
  132.  
  133. for(int i = 0; i < vertexStr.length; i++)
  134. {
  135. String verStr = vertexStr[i].replaceAll("//", "/0/");
  136.  
  137. String[] asd = verStr.split("/");
  138.  
  139. FaceData data = new FaceData();
  140. data.vertexID = Integer.parseInt(asd[0]);
  141.  
  142. data.texID = Integer.parseInt(asd[1]);
  143.  
  144. data.normalID = Integer.parseInt(asd[2]);
  145.  
  146. faces.add(data);
  147. }
  148. }
  149.  
  150. line = reader.readLine();
  151. }
  152.  
  153. reader.close();
  154.  
  155. } catch (FileNotFoundException e) {
  156. e.printStackTrace();
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. }
  160.  
  161. for(int i = 0; i < faces.size(); i++)
  162. {
  163. Vertex v = new Vertex();
  164. v.pos = pos.get(faces.get(i).vertexID - 1);
  165.  
  166. if(faces.get(i).texID != 0)
  167. {
  168. Vector2f t = new Vector2f();
  169. t = tex.get(faces.get(i).texID - 1);
  170. t.y = t.y;
  171.  
  172. v.texCoord.set(t);
  173. }
  174. else
  175. v.texCoord = new Vector2f(0,0);
  176.  
  177. v.normal = norm.get(faces.get(i).normalID - 1);
  178.  
  179. vertices.add(v);
  180. }
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement