Advertisement
Guest User

Errors

a guest
Nov 21st, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package renderEngine;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import models.RawModel;
  11.  
  12. import org.lwjgl.util.vector.Vector2f;
  13. import org.lwjgl.util.vector.Vector3f;
  14.  
  15. public class OBJLoader {
  16.  
  17.     public static RawModel loadObjModel(String fileName, Loader loader){
  18.         FileReader fr = null;
  19.         try {
  20.         fr = new FileReader(new File("res/"+fileName+".obj"));
  21.         } catch (FileNotFoundException e) {
  22.             System.err.println("Couldn't Load File!");
  23.             e.printStackTrace();
  24.         }
  25.         BufferedReader reader = new BufferedReader(fr);
  26.         String line;
  27.         List<Vector3f> vertices = new ArrayList<Vector3f>();
  28.         List<Vector2f> textures = new ArrayList<Vector2f>();
  29.         List<Vector3f> normals = new ArrayList<Vector3f>();
  30.         List<Integer> indices = new ArrayList<Integer>();
  31.         float[] verticesArray = null;
  32.         float[] normalsArray = null;
  33.         float[] texturesArray = null;
  34.         float[] indicesArray = null;
  35.        
  36.         while(true) {
  37.             line = reader.readLine();
  38.             String[] currentLine = line.split(" ");
  39.             if(line.startsWith("v ")) {
  40.                 Vector3f vertex = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]),Float.parseFloat(currentLine[3]));
  41.                 vertices.add(vertex);
  42.             }else if(line.startsWith("vt ")){
  43.                 Vector2f texture = new Vector2f(Float.parseFloat(currentLine[1]),Float.parseFloat(currentLine[2]));
  44.                 textures.add(texture);
  45.             }else if(line.startsWith("vn ")){
  46.                 Vector3f normal = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]),Float.parseFloat(currentLine[3]));
  47.                 normals.add(normal);
  48.             }else if(line.startsWith("f ")){
  49.                 texturesArray = new float[vertices.size()*2];
  50.                 normalsArray = new float[vertices.size()*3];
  51.                 break;
  52.             }
  53.         }
  54.        
  55.         while(line!=null) {
  56.             if(!line.startsWith("f ")){
  57.                 line = reader.readLine();
  58.                 continue;
  59.             }
  60.             String[] currentLine = line.split(" ");
  61.             String[] vertex1 = currentLine[1].split("/");
  62.             String[] vertex2 = currentLine[2].split("/");
  63.             String[] vertex3 = currentLine[3].split("/");
  64.            
  65.             processVertex(vertex1, indices, textures, normals, texturesArray, normalsArray);
  66.             processVertex(vertex2, indices, textures, normals, texturesArray, normalsArray);
  67.             processVertex(vertex3, indices, textures, normals, texturesArray, normalsArray);
  68.             line = reader.readLine();
  69.         }
  70.         reader.close();
  71.        
  72.     }catch (Exception e){
  73.         e.printStackTrace();
  74.     }
  75.        
  76.         verticesArray = new float[vertices.size()*3];
  77.         indicesArray = new int[inmdices.size()];
  78.        
  79.         int vertexPointer = 0;
  80.         for(Vector3f vertex:vertices){
  81.             verticesArray[vertexPointer++] = vertex.x;
  82.             verticesArray[vertexPointer++] = vertex.y;
  83.             verticesArray[vertexPointer++] = vertex.z;
  84.         }
  85.        
  86.         for(int i=0;i<indices.size();i++){
  87.             indicesArray[i] = indices.get(index);
  88.         }
  89.         return loader.loadToVAO(verticesArray, texturesArray, indicesArray);
  90.        
  91.     }
  92.    
  93.     private static void processVertex(String[] vertexData, List<Integer> indices, List<Vector2f> textures, List<Vector3f> normals,
  94.             float[] textureArray, float[] normalsArray) {
  95.        
  96.         int currentVertexPointer = Integer.parseInt(vertexData[0]) - 1;
  97.         indices.add(currentVertexPointer);
  98.         Vector2f currentTex = textures.get(Integer.parseInt(vertexData[1])-1);
  99.         textureArray[currentVertexPointer*2] = currentTex.x;
  100.         textureArray[currentVertexPointer*2+1] = 1 - currentTex.y;
  101.         Vector3f currentNorm = normals.get(Integer.parseInt(vertexData[2])-1);
  102.         normalsArray[currentVertexPointer*3] = currentNorm.x;
  103.         normalsArray[currentVertexPointer*3+1] = currentNorm.y;
  104.         normalsArray[currentVertexPointer*3+2] = currentNorm.z;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement