Skip to content

Commit ce9f831

Browse files
committed
fixed wavefront when negative vertex index
1 parent f1bb7ed commit ce9f831

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ ChangeLog
180180

181181
(f) fixed, (i) improved, (n) new feature
182182

183+
- 2.5.1 (20/05/2019)
184+
- (f) wavefront loader fixed for faces point to negative indices
183185
- 2.5.0 (19/05/2019)
184186
- (n) new blending toggle
185187
- (n) new color toggle
76 Bytes
Binary file not shown.

app/src/main/java/org/andresoviedo/app/model3D/demo/ExampleSceneLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ protected Void doInBackground(Void... params) {
143143
try {
144144
// this has heterogeneous faces
145145
Object3DData obj53 = Object3DBuilder.loadV5(parent, Uri.parse("assets://assets/models/ToyPlane.obj"));
146+
InputStream open = ContentUtils.getInputStream(Uri.parse("assets://assets/models/"+obj53.getTextureFile()));
147+
obj53.setTextureData(IOUtils.read(open));
146148
obj53.centerAndScale(2.0f);
147149
obj53.setPosition(new float[] { 2f, 0f, 0f });
148150
obj53.setColor(new float[] { 1.0f, 1.0f, 1f, 1.0f });

engine/src/main/java/org/andresoviedo/android_3d_model_engine/services/Object3DBuilder.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,8 @@ public static Object3DData generateArrays(Object3DData obj) throws IOException {
576576
}
577577
}
578578
if (texture != null) {
579-
Log.i("Object3DBuilder","Loading texture "+texture);
580-
InputStream is = ContentUtils.getInputStream(texture);
581-
if (is != null) {
582-
textureData = IOUtils.read(is);
583-
is.close();
584-
} else {
585-
obj.addError("Texture '"+texture+"' not found in provided files");
586-
}
579+
obj.setTextureFile(texture);
580+
Log.i("Object3DBuilder","Texture "+texture);
587581
} else {
588582
Log.i("Object3DBuilder", "Found material(s) but no texture");
589583
}

engine/src/main/java/org/andresoviedo/android_3d_model_engine/services/wavefront/WavefrontLoader.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public class WavefrontLoader {
7676
int numTextures = 0;
7777
int numNormals = 0;
7878
int numFaces = 0;
79+
int numPolygon = 0;
80+
int numTriangles = 0;
7981
int numVertsReferences = 0;
8082

8183
// buffers
@@ -187,6 +189,8 @@ public void analyzeModel(InputStream is) {
187189
numFaces += (faceSize - 2);
188190
// (faceSize-2)x3 = converting polygon to triangles
189191
numVertsReferences += (faceSize - 2) * 3;
192+
numPolygon += (faceSize > 3)? 1 : 0;
193+
numTriangles += (faceSize == 3)? 1 : 0;
190194
} else if (line.startsWith("mtllib ")) // build material
191195
{
192196
materials = new Materials(line.substring(7));
@@ -219,6 +223,8 @@ else if (line.charAt(0) == 'o') // object group
219223

220224
Log.i("WavefrontLoader","Number of vertices:"+numVerts);
221225
Log.i("WavefrontLoader","Number of faces:"+numFaces);
226+
Log.i("WavefrontLoader","- Number of polygons:"+numPolygon);
227+
Log.i("WavefrontLoader","- Number of triangles:"+numTriangles);
222228
}
223229

224230
/**
@@ -850,6 +856,7 @@ public static class Faces {
850856
*/
851857
public ArrayList<int[]> facesNormIdxs;
852858

859+
private FloatBuffer vertex;
853860
private FloatBuffer normals;
854861
private ArrayList<Tuple3> texCoords;
855862

@@ -869,6 +876,7 @@ public Faces(int numFaces){
869876

870877
Faces(int totalFaces, IntBuffer buffer, FloatBuffer vs, FloatBuffer ns, ArrayList<Tuple3> ts) {
871878
this.totalFaces = totalFaces;
879+
vertex = vs;
872880
normals = ns;
873881
texCoords = ts;
874882

@@ -951,10 +959,6 @@ public boolean addFace(String line) {
951959
// the token
952960

953961
int vertIdx = Integer.parseInt(faceTokens[0]);
954-
/*if (vertIdx > 65535){
955-
Log.e("WavefrontLoader","Ignoring face because its out of range (>65535)");
956-
continue;
957-
}*/
958962
if (numSeps > 1){
959963
if (vt == null) vt = new int[3];
960964
try{
@@ -974,7 +978,12 @@ public boolean addFace(String line) {
974978
// add 0's if the vt or vn index values are missing;
975979
// 0 is a good choice since real indices start at 1
976980

977-
if (WavefrontLoader.INDEXES_START_AT_1) {
981+
if (vertIdx < 0){
982+
vertIdx = this.vertex.capacity()/3+vertIdx;
983+
if (vt != null) vt[faceIndex] = texCoords.size() + vt[faceIndex];
984+
if (vn != null) vn[faceIndex] = normals.capacity()/3 + vn[faceIndex];
985+
}
986+
else if (WavefrontLoader.INDEXES_START_AT_1) {
978987
vertIdx--;
979988
if (vt != null) vt[faceIndex] = vt[faceIndex] - 1;
980989
if (vn != null) vn[faceIndex] = vn[faceIndex] - 1;

0 commit comments

Comments
 (0)