Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Longarmx (posted 2014-10-17 03:59:02, viewed 418 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123  
public Mesh loadMesh(String name) {
   name = MODEL_PATH + name + OBJ_EXT;

   String[] splitArray = name.split("\\.");
   String ext = splitArray[splitArray.length - 1];

   if (!ext.equals("obj")) {
       System.err
          .println("Error: File format not supported for mesh data: "
             + ext);
       new Exception().printStackTrace();
       System.exit(1);
   }

   try {
       BufferedReader meshReader = new BufferedReader(new FileReader(name));
       String line;
       
       ArrayList<Vertex> vertices = new ArrayList<Vertex>();
       ArrayList<Vector3f> positions = new ArrayList<Vector3f>();
       ArrayList<Vector2f> uvs = new ArrayList<Vector2f>();
       ArrayList<Vector3f> normals = new ArrayList<Vector3f>();
       ArrayList<Integer> indices = new ArrayList<Integer>();
       
       while((line = meshReader.readLine()) != null) {
      String[] tokens = line.split(" ");
      tokens = Util.removeEmptyStrings(tokens);
      
      if(tokens.length == 0 || tokens[0].equals("#"))
          continue;
      // If these are vertex tokens
      else if(tokens[0].equals("v")) {
          positions.add(new Vector3f(Float
             .valueOf(tokens[1]), Float.valueOf(tokens[2]),
             Float.valueOf(tokens[3])));
      // If these are texture tokens
      } else if(tokens[0].equals("vt")) {
          uvs.add(new Vector2f(Float
             .valueOf(tokens[1]), Float.valueOf(tokens[2])));
      // If these are normal tokens
      } else if(tokens[0].equals("vn")) {
          normals.add(new Vector3f(Float
             .valueOf(tokens[1]), Float.valueOf(tokens[2]),
             Float.valueOf(tokens[3])));
      // If these are faces
      } else if(tokens[0].equals("f")) {
          int index1 = Integer.parseInt(tokens[1].split("/")[0]) - 1;
          int index2 = Integer.parseInt(tokens[2].split("/")[0]) - 1;
          int index3 = Integer.parseInt(tokens[3].split("/")[0]) - 1;
          
          int uv1, uv2, uv3;
          
          // If there aren't any uvs
          if(tokens[1].split("/")[1].length() == 0) {
         uv1 = 0;
         uv2 = 0;
         uv3 = 0;
          } else {
         uv1 = Integer.parseInt(tokens[1].split("/")[1]) - 1;
         uv2 = Integer.parseInt(tokens[2].split("/")[1]) - 1;
         uv3 = Integer.parseInt(tokens[3].split("/")[1]) - 1;
          }
          
          int normal1 = Integer.parseInt(tokens[1].split("/")[2]) - 1;
          int normal2 = Integer.parseInt(tokens[2].split("/")[2]) - 1;
          int normal3 = Integer.parseInt(tokens[3].split("/")[2]) - 1;
          
          if(uvs.size() <= uv1) {
         uvs.add(new Vector2f(0, 0));
          }
          
          int index = vertices.size();

          indices.add(index);
          indices.add(index+1);
          indices.add(index+2);
          
          vertices.add(new Vertex(positions.get(index1), uvs.get(uv1), normals.get(normal1)));
          vertices.add(new Vertex(positions.get(index2), uvs.get(uv2), normals.get(normal2)));
          vertices.add(new Vertex(positions.get(index3), uvs.get(uv3), normals.get(normal3)));
          
          if(tokens.length > 4) {
         int index4 = Integer.parseInt(tokens[4].split("/")[0]) - 1;
         
         int uv4;
         
         // If there aren't any uvs
         if (tokens[1].split("/")[1].length() == 0) {
             uv4 = 0;
         } else {
             uv4 = Integer.parseInt(tokens[4].split("/")[1]) - 1;
         }
         
         int normal4 = Integer.parseInt(tokens[4].split("/")[2]) - 1;
         
         indices.add(index);
         indices.add(index+2);
         indices.add(index+3);
         
         vertices.add(new Vertex(positions.get(index4), uvs.get(uv4), normals.get(normal4)));
          }
          
      }
      
       }

       meshReader.close();

       Vertex[] vertexData = new Vertex[vertices.size()];
       vertices.toArray(vertexData);

       Integer[] indexData = new Integer[indices.size()];
       indices.toArray(indexData);

       // Set the mesh data with the vertices and indices
       setVertices(vertexData, Util.toIntArray(indexData), false);
   } catch (Exception e) {
       e.printStackTrace();
       System.exit(1);
   }

   return this;
    }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Riven (849 views)
2019-09-04 15:33:17

hadezbladez (5813 views)
2018-11-16 13:46:03

hadezbladez (2616 views)
2018-11-16 13:41:33

hadezbladez (6230 views)
2018-11-16 13:35:35

hadezbladez (1503 views)
2018-11-16 13:32:03

EgonOlsen (4738 views)
2018-06-10 19:43:48

EgonOlsen (5796 views)
2018-06-10 19:43:44

EgonOlsen (3281 views)
2018-06-10 19:43:20

DesertCoockie (4178 views)
2018-05-13 18:23:11

nelsongames (5513 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!