Android Open Source - model-explorer Obj Parser






From Project

Back to project page model-explorer.

License

The source code is released under:

Apache License

If you think the Android project model-explorer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.etaoin.myopengltest.util.io.obj;
//ww  w.  j a va  2s.  co  m
import com.etaoin.myopengltest.core.main.context.ContextManager;
import com.etaoin.myopengltest.util.geometry.Face;
import com.etaoin.myopengltest.util.geometry.TriangleList;
import com.etaoin.myopengltest.util.geometry.Vector3;
import com.etaoin.myopengltest.util.geometry.Vector3List;
import com.etaoin.myopengltest.util.gl.MyGLES20;
import com.etaoin.myopengltest.util.io.ModelParser;
import com.etaoin.myopengltest.util.shapes.Drawable;
import com.etaoin.myopengltest.util.shapes.Model;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * Parser for .obj models
 */
public class ObjParser implements ModelParser {

  private BufferedReader content;
  private MyGLES20 gles20;
  private ContextManager contextManager;
  private Vector3List vertices = new Vector3List();
  private Vector3List normals = new Vector3List();
  private Vector3List textureCoordinates = new Vector3List();
  private TriangleList faces = new TriangleList();
  private Model model;
  private boolean isSmoothEnabled = false; // TODO Use?

  public ObjParser(BufferedReader content, MyGLES20 gles20, ContextManager contextManager) {
    this.content = content;
    this.gles20 = gles20;
    this.contextManager = contextManager;
  }

  public Drawable parse() throws IOException {
    String line;
    while ((line = content.readLine()) != null) {
      line = line.trim();
      if (isVertexDefinition(line)) {
        parseVertex(line);
      } else if (isNormalDefinition(line)) {
        parseNormal(line);
      } else if (isTextureCoordinateDefinition(line)) {
        parseTextureCoordinate(line);
      } else if (isGroupNameDefinition(line)) {
        // Empty
      } else if (isSmoothGroupDefinition(line)) {
        // Empty
      } else if (isFaceDefinition(line)) {
        parseFace(line);
      }
    }

    model = new Model(vertices.toFloatBuffer(), faces.toShortBuffer(), faces.size() * 3, gles20, contextManager);
    return model;
  }

  private boolean isVertexDefinition(String line) {
    return line.startsWith("v ");
  }

  private boolean isNormalDefinition(String line) {
    return line.startsWith("vn ");
  }

  private boolean isTextureCoordinateDefinition(String line) {
    return line.startsWith("vt ");
  }

  private boolean isGroupNameDefinition(String line) {
    return line.startsWith("g ");
  }

  private boolean isSmoothGroupDefinition(String line) {
    return line.startsWith("s ");
  }

  private boolean isFaceDefinition(String line) {
    return line.startsWith("f ");
  }

  private void parseVertex(String line) {
    vertices.add(parseVector(line));
  }

  private void parseNormal(String line) {
    normals.add(parseVector(line));
  }

  private void parseTextureCoordinate(String line) {
    textureCoordinates.add(parseVector(line));
  }

  private void parseFace(String line) {
    Face face = new Face();
    String[] faceParts = line.split(" ");
    for (int i = 1; i < faceParts.length; i++) {
      String[] vertexParts = faceParts[i].split("/");
      int vertexIndex = Integer.parseInt(vertexParts[0]) - 1;
      int normalIndex = Integer.parseInt(vertexParts[1]) - 1;
      int textureCoordinateIndex = Integer.parseInt(vertexParts[2]) - 1;

      face.addVertex(vertexIndex, normalIndex, textureCoordinateIndex);
    }
    faces.add(face);
  }

  private Vector3 parseVector(String line) {
    String[] parts = line.split(" ");
    float xCoordinate = Float.parseFloat(parts[parts.length - 3]);
    float yCoordinate = Float.parseFloat(parts[parts.length - 2]);
    float zCoordinate = Float.parseFloat(parts[parts.length - 1]);
    return new Vector3(xCoordinate, yCoordinate, zCoordinate);
  }
}




Java Source Code List

com.etaoin.myopengltest.core.main.activity.MainGLActivity.java
com.etaoin.myopengltest.core.main.context.ContextManager.java
com.etaoin.myopengltest.core.main.context.Context.java
com.etaoin.myopengltest.core.main.context.GameContext.java
com.etaoin.myopengltest.core.main.events.UserEvent.java
com.etaoin.myopengltest.core.main.renderers.MainGLRenderer.java
com.etaoin.myopengltest.core.main.views.MainGLSurfaceView.java
com.etaoin.myopengltest.util.camera.Camera.java
com.etaoin.myopengltest.util.geometry.Face.java
com.etaoin.myopengltest.util.geometry.TriangleList.java
com.etaoin.myopengltest.util.geometry.Vector3List.java
com.etaoin.myopengltest.util.geometry.Vector3.java
com.etaoin.myopengltest.util.gl.MyGLES20DebugAll.java
com.etaoin.myopengltest.util.gl.MyGLES20DebugNone.java
com.etaoin.myopengltest.util.gl.MyGLES20Factory.java
com.etaoin.myopengltest.util.gl.MyGLES20.java
com.etaoin.myopengltest.util.gl.MyGenericGLES20.java
com.etaoin.myopengltest.util.io.FileReader.java
com.etaoin.myopengltest.util.io.ModelParserFactory.java
com.etaoin.myopengltest.util.io.ModelParser.java
com.etaoin.myopengltest.util.io.obj.ObjParser.java
com.etaoin.myopengltest.util.light.PointLight.java
com.etaoin.myopengltest.util.shaders.SampleFragmentShader.java
com.etaoin.myopengltest.util.shaders.SampleVertexShader.java
com.etaoin.myopengltest.util.shaders.ShaderFactory.java
com.etaoin.myopengltest.util.shaders.Shader.java
com.etaoin.myopengltest.util.shapes.Axis.java
com.etaoin.myopengltest.util.shapes.Background.java
com.etaoin.myopengltest.util.shapes.Drawable.java
com.etaoin.myopengltest.util.shapes.Model.java