1 | package com.framsticks.net.client3D.graphics.loaders;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.StringTokenizer;
|
---|
9 |
|
---|
10 | import pl.vorg.mowa.core.graphics.GeometryGroup;
|
---|
11 | import pl.vorg.mowa.core.graphics.Vec2;
|
---|
12 | import pl.vorg.mowa.core.graphics.Vec3;
|
---|
13 |
|
---|
14 | import com.framsticks.net.client3D.Log;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * The OBJ files loader.
|
---|
18 | *
|
---|
19 | * For now, it supports only objects containing only triangles with defined
|
---|
20 | * vertex positions, texture coordinates and normal vectors.
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class OBJLoader {
|
---|
24 | public static GeometryGroup load(String filePath) throws IOException {
|
---|
25 | Log.getInstance().log("dbg", "OBJLoader.load \"" + filePath + "\"");
|
---|
26 | FileReader reader = null;
|
---|
27 | try {
|
---|
28 | reader = new FileReader(filePath);
|
---|
29 | } catch (FileNotFoundException e) {
|
---|
30 | }
|
---|
31 | if (reader == null) {
|
---|
32 | Log.getInstance().log("err", "OBJLoader.load error");
|
---|
33 | return new GeometryGroup();
|
---|
34 | }
|
---|
35 | return load(new BufferedReader(reader));
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static GeometryGroup load(BufferedReader reader) throws IOException {
|
---|
39 | OBJMesh objMesh = parse(reader);
|
---|
40 | OBJMeshConverter converter = new OBJMeshConverter();
|
---|
41 | return converter.convert(objMesh);
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static OBJMesh parse(BufferedReader reader) throws IOException {
|
---|
45 | Log.getInstance().log("dbg", "OBJLoader.parse");
|
---|
46 | OBJMesh mesh = new OBJMesh();
|
---|
47 |
|
---|
48 | ArrayList<Vec3> vertexList = mesh.getVertices();
|
---|
49 | ArrayList<Vec3> normalList = mesh.getNormals();
|
---|
50 | ArrayList<Vec2> texCoordList = mesh.getTexCoords();
|
---|
51 | ArrayList<OBJFace> faceList = mesh.getFaces();
|
---|
52 |
|
---|
53 | String line;
|
---|
54 | while ((line = reader.readLine()) != null) {
|
---|
55 | StringTokenizer tokens = new StringTokenizer(line, " ");
|
---|
56 | if (!tokens.hasMoreTokens()) {
|
---|
57 | continue;
|
---|
58 | }
|
---|
59 | String section = tokens.nextToken();
|
---|
60 | if (section.equals("o")) {
|
---|
61 | if (tokens.hasMoreTokens()) {
|
---|
62 | mesh.setName(tokens.nextToken());
|
---|
63 | }
|
---|
64 | } else if (section.equals("v")) {
|
---|
65 | vertexList.add(new Vec3(Float.parseFloat(tokens.nextToken()),
|
---|
66 | Float.parseFloat(tokens.nextToken()), Float
|
---|
67 | .parseFloat(tokens.nextToken())));
|
---|
68 | } else if (section.equals("vt")) {
|
---|
69 | texCoordList.add(new Vec2(Float.parseFloat(tokens.nextToken()),
|
---|
70 | Float.parseFloat(tokens.nextToken())));
|
---|
71 | } else if (section.equals("vn")) {
|
---|
72 | normalList.add(new Vec3(Float.parseFloat(tokens.nextToken()),
|
---|
73 | Float.parseFloat(tokens.nextToken()), Float
|
---|
74 | .parseFloat(tokens.nextToken())));
|
---|
75 | } else if (section.equals("f")) {
|
---|
76 | int numTokens = tokens.countTokens();
|
---|
77 | OBJFaceVertex[] faceVertices = new OBJFaceVertex[numTokens];
|
---|
78 | String faceStr;
|
---|
79 | String[] indices;
|
---|
80 | for (int i = 0; i < numTokens; i++) {
|
---|
81 | faceStr = tokens.nextToken();
|
---|
82 | indices = faceStr.split("/");
|
---|
83 | faceVertices[i] = new OBJFaceVertex();
|
---|
84 | if (!indices[0].equals(""))
|
---|
85 | faceVertices[i].setVertexIndex(Integer
|
---|
86 | .parseInt(indices[0]) - 1);
|
---|
87 | if (!indices[1].equals(""))
|
---|
88 | faceVertices[i].setTexCoordIndex(Integer
|
---|
89 | .parseInt(indices[1]) - 1);
|
---|
90 | if (!indices[2].equals(""))
|
---|
91 | faceVertices[i].setNormalIndex(Integer
|
---|
92 | .parseInt(indices[2]) - 1);
|
---|
93 | }
|
---|
94 | OBJFace face = new OBJFace();
|
---|
95 | face.setVertices(faceVertices);
|
---|
96 | faceList.add(face);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | Log.getInstance().log("dbg", "OBJLoader.load done");
|
---|
101 | return mesh;
|
---|
102 | }
|
---|
103 | }
|
---|