1 | package com.framsticks.net.client3D.graphics.loaders;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import pl.vorg.mowa.core.graphics.FloatVertexAttrib;
|
---|
6 | import pl.vorg.mowa.core.graphics.Geometry;
|
---|
7 | import pl.vorg.mowa.core.graphics.GeometryGroup;
|
---|
8 | import pl.vorg.mowa.core.graphics.IndexBuffer;
|
---|
9 | import pl.vorg.mowa.core.graphics.PrimitiveType;
|
---|
10 | import pl.vorg.mowa.core.graphics.Vec2;
|
---|
11 | import pl.vorg.mowa.core.graphics.Vec3;
|
---|
12 | import pl.vorg.mowa.core.graphics.VertexStream;
|
---|
13 |
|
---|
14 | import com.framsticks.net.client3D.Log;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A class converting OBJMesh to GeometryGroup, vertex arrays to VertexStreams,
|
---|
18 | * etc.
|
---|
19 | *
|
---|
20 | * For now, it supports only uniform meshes (i.e., it contains only triangles).
|
---|
21 | *
|
---|
22 | * @author vorg
|
---|
23 | */
|
---|
24 | public class OBJMeshConverter {
|
---|
25 | public GeometryGroup convert(OBJMesh mesh) {
|
---|
26 | Log.getInstance().log("dbg", "OBJMeshConverter.convert");
|
---|
27 | GeometryGroup group = new GeometryGroup();
|
---|
28 | ArrayList<OBJFace> faces = mesh.getFaces();
|
---|
29 |
|
---|
30 | int faceVertexCount = 0;
|
---|
31 | for (OBJFace face : mesh.getFaces()) {
|
---|
32 | if (faceVertexCount == 0) {
|
---|
33 | faceVertexCount = face.getVertices().length;
|
---|
34 | } else if (faceVertexCount != face.getVertices().length) {
|
---|
35 | // Two different face types in the same mesh. The mesh
|
---|
36 | // would have to be divided into smaller ones.
|
---|
37 | Log.getInstance().log(
|
---|
38 | "err",
|
---|
39 | "OBJMeshConverter.convert unsupported mesh type"
|
---|
40 | + "(faceVertexCount:"
|
---|
41 | + face.getVertices().length + "!="
|
---|
42 | + faceVertexCount + ")");
|
---|
43 | return group;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | // Build indexes.
|
---|
48 | ArrayList<Integer> indexList = new ArrayList<Integer>();
|
---|
49 | ArrayList<OBJFaceVertex> faceVertexList = new ArrayList<OBJFaceVertex>();
|
---|
50 |
|
---|
51 | Geometry geometry = new Geometry();
|
---|
52 |
|
---|
53 | if (faceVertexCount == 3) {
|
---|
54 | geometry.setPrimitiveType(PrimitiveType.Triangles);
|
---|
55 | } else if (faceVertexCount == 4) {
|
---|
56 | geometry.setPrimitiveType(PrimitiveType.Quads);
|
---|
57 | } else {
|
---|
58 | Log.getInstance()
|
---|
59 | .log("err",
|
---|
60 | "OBJMeshConverter.convert only triangles and quads are supported (faceVertexCount:"
|
---|
61 | + faceVertexCount + ")");
|
---|
62 | return group;
|
---|
63 | }
|
---|
64 |
|
---|
65 | for (int i = 0; i < faces.size(); i++) {
|
---|
66 | OBJFaceVertex[] faceVertices = faces.get(i).getVertices();
|
---|
67 | for (int j = 0; j < faceVertexCount; j++) {
|
---|
68 | OBJFaceVertex v = faceVertices[j];
|
---|
69 | int index = faceVertexList.indexOf(v);
|
---|
70 | if (index == -1) {
|
---|
71 | faceVertexList.add(v);
|
---|
72 | index = faceVertexList.size() - 1;
|
---|
73 | }
|
---|
74 | indexList.add(index);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | VertexStream vertexStream = buildBuffers(mesh, indexList,
|
---|
79 | faceVertexList);
|
---|
80 | geometry.setVertexStream(vertexStream);
|
---|
81 |
|
---|
82 | group.getChildren().add(geometry);
|
---|
83 | group.updateBoundingBox();
|
---|
84 |
|
---|
85 | return group;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private VertexStream buildBuffers(OBJMesh mesh,
|
---|
89 | ArrayList<Integer> indexList,
|
---|
90 | ArrayList<OBJFaceVertex> faceVertexList) {
|
---|
91 | VertexStream vertexStream = new VertexStream();
|
---|
92 |
|
---|
93 | IndexBuffer indexBuffer = new IndexBuffer();
|
---|
94 | int[] idxBuf = new int[indexList.size()];
|
---|
95 | for (int i = 0; i < indexList.size(); i++) {
|
---|
96 | idxBuf[i] = indexList.get(i);
|
---|
97 | }
|
---|
98 | indexBuffer.setBuffer(idxBuf);
|
---|
99 | vertexStream.setIndexBuffer(indexBuffer);
|
---|
100 |
|
---|
101 | ArrayList<Vec3> vertices = mesh.getVertices();
|
---|
102 | FloatVertexAttrib posAttrib = new FloatVertexAttrib("pos");
|
---|
103 | float[] posBuf = new float[3 * faceVertexList.size()];
|
---|
104 | for (int i = 0; i < faceVertexList.size(); i++) {
|
---|
105 | Vec3 v = vertices.get(faceVertexList.get(i).getVertexIndex());
|
---|
106 | posBuf[i * 3] = v.getX();
|
---|
107 | posBuf[i * 3 + 1] = v.getY();
|
---|
108 | posBuf[i * 3 + 2] = v.getZ();
|
---|
109 | }
|
---|
110 | posAttrib.setBuffer(posBuf);
|
---|
111 | vertexStream.addAttrib(posAttrib);
|
---|
112 |
|
---|
113 | ArrayList<Vec3> normals = mesh.getNormals();
|
---|
114 | if (normals.size() > 0) {
|
---|
115 | FloatVertexAttrib normalAttrib = new FloatVertexAttrib("normal");
|
---|
116 | float[] normalBuf = new float[3 * faceVertexList.size()];
|
---|
117 | for (int i = 0; i < faceVertexList.size(); i++) {
|
---|
118 | Vec3 v = normals.get(faceVertexList.get(i).getNormalIndex());
|
---|
119 | normalBuf[i * 3] = v.getX();
|
---|
120 | normalBuf[i * 3 + 1] = v.getY();
|
---|
121 | normalBuf[i * 3 + 2] = v.getZ();
|
---|
122 | }
|
---|
123 | normalAttrib.setBuffer(normalBuf);
|
---|
124 | vertexStream.addAttrib(normalAttrib);
|
---|
125 | }
|
---|
126 |
|
---|
127 | ArrayList<Vec2> texCoords = mesh.getTexCoords();
|
---|
128 | if (texCoords.size() > 0) {
|
---|
129 | FloatVertexAttrib texCoord0Attrib = new FloatVertexAttrib(
|
---|
130 | "texCoord0");
|
---|
131 | float[] texCoord0Buf = new float[2 * texCoords.size()];
|
---|
132 | for (int i = 0; i < texCoords.size(); i++) {
|
---|
133 | Vec2 tc = texCoords.get(faceVertexList.get(i)
|
---|
134 | .getTexCoordIndex());
|
---|
135 | texCoord0Buf[i * 2] = tc.getX();
|
---|
136 | texCoord0Buf[i * 2 + 1] = tc.getY();
|
---|
137 | }
|
---|
138 | texCoord0Attrib.setBuffer(texCoord0Buf);
|
---|
139 | vertexStream.addAttrib(texCoord0Attrib);
|
---|
140 | }
|
---|
141 |
|
---|
142 | return vertexStream;
|
---|
143 | }
|
---|
144 | }
|
---|