Android Open Source - OpenGlDraw_Android Wavefront Obj






From Project

Back to project page OpenGlDraw_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project OpenGlDraw_Android 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.example.opengldraw.utils;
/*w ww  . j  a  va2  s .co m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;

public class WavefrontObj extends Object implements ShapeDataInterface {

  private float[] vertices;
  private float[] colors = {
        50/255f, 205/255f, 50/255f, 1.0f,  50/255f, 205/255f, 50/255f, 1.0f,  50/255f, 205/255f, 50/255f, 1.0f,
        50/255f, 205/255f, 50/255f, 1.0f,  50/255f, 205/255f, 50/255f, 1.0f,  50/255f, 205/255f, 50/255f, 1.0f,
        50/255f, 205/255f, 50/255f, 1.0f,  50/255f, 205/255f, 50/255f, 1.0f };
  private char[] indices;
  private float[] normals;
  private float[] textCoords;
  
  private AssetManager am;
  
  public static FloatBuffer mVertexBuffer;
    public static FloatBuffer mTexBuffer;
    public static FloatBuffer mNormBuffer;
    public static CharBuffer mIndexBuffer;
    public int indicesCount;
  
  public WavefrontObj(Context aCtx) {
    super();
    am = aCtx.getAssets();
  }
  
  /** Loads a wavefront obj file. The file needs to be located in assets directory. Works also with textures */
  public void loadObj(String fileName) {
    try {
      ArrayList<Float> arrayVertices = new ArrayList<Float>();
      ArrayList<Short> arrayIndices = new ArrayList<Short>();
      ArrayList<Float> arrayTextures = new ArrayList<Float>();
      ArrayList<Float> arrayNormals = new ArrayList<Float>();
      
      BufferedReader br = new BufferedReader(new InputStreamReader(am.open(fileName)));
      String strLine = null;
      String[] splitLine;
      float value;
      short shortVal;
      while((strLine = br.readLine()) != null) {
        splitLine = strLine.split(" ");
        if(splitLine[0].equalsIgnoreCase("v")) {
          for(int x = 1; x < 4; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayVertices.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("vt")) {
          for(int x = 1; x < 3; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayTextures.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("vn")) {
          for(int x = 1; x < 4; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayNormals.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("f")) {
          for(int x = 1; x < 4; x++) {
            String[] temp = splitLine[x].split("//");
            shortVal = Short.parseShort(temp[0]);
            arrayIndices.add(--shortVal);
          }
        }
//        if(splitLine[0].equalsIgnoreCase("f")) {
//          for(int x = 1; x < 4; x++) {
//            String[] temp = splitLine[x].split("/");
//            shortVal = Short.parseShort(temp[0]);
//            arrayIndices.add(--shortVal);
//          }
//        }
      }
      
      vertices = new float[arrayVertices.size()];
      for(int y = 0; y < arrayVertices.size(); y++) {
        vertices[y] = arrayVertices.get(y).floatValue();
      }
      indices = new char[arrayIndices.size()];
      for(int y = 0; y < arrayIndices.size(); y++) {
        indices[y] = (char)arrayIndices.get(y).shortValue();
      }
      normals = new float[arrayNormals.size()];
      for(int y = 0; y < arrayNormals.size(); y++) {
        normals[y] = arrayNormals.get(y).floatValue();
      }
      textCoords = new float[arrayTextures.size()];
      for(int y = 0; y < arrayTextures.size(); y++) {
        textCoords[y] = arrayTextures.get(y).floatValue();
      }
      
          ByteBuffer vbb = ByteBuffer.allocateDirect(getVertices().length * 4);
          vbb.order(ByteOrder.nativeOrder());
          mVertexBuffer = vbb.asFloatBuffer();
          mVertexBuffer.put(getVertices());
          mVertexBuffer.position(0);

          ByteBuffer ibb = ByteBuffer.allocateDirect(arrayIndices.size() * 2);
          ibb.order(ByteOrder.nativeOrder());
          mIndexBuffer = ibb.asCharBuffer();
          mIndexBuffer.put(indices);
          mIndexBuffer.position(0);
          
          ByteBuffer bbn = ByteBuffer.allocateDirect(getNormals().length * 4);
          bbn.order(ByteOrder.nativeOrder());
          mNormBuffer = bbn.asFloatBuffer();
          mNormBuffer.put(getNormals());
          mNormBuffer.position(0);
          
          ByteBuffer bbt = ByteBuffer.allocateDirect(getTextCoords().length * 4);
          bbt.order(ByteOrder.nativeOrder());
          mTexBuffer = bbt.asFloatBuffer();
          mTexBuffer.put(getTextCoords());
          mTexBuffer.position(0);
      
          indicesCount = arrayIndices.size();
          vertices = null;
          indices = null;
          normals = null;
          textCoords = null;
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

    /** Load obj file with texture coordinates. Surprisingly works worse than loadObj */
  public void loadObjWithTexture(String fileName) {
    try {
      ArrayList<Float> arrayVertices = new ArrayList<Float>();
      ArrayList<Float> arrayTextures = new ArrayList<Float>();
      ArrayList<Float> arrayNormals = new ArrayList<Float>();
      ArrayList<IndexOrderObj> arrayIndexOrder = new ArrayList<IndexOrderObj>();
      
      BufferedReader br = new BufferedReader(new InputStreamReader(am.open(fileName)));
      String strLine = null;
      String[] splitLine;
      String[]splitText;
      float value;
      while((strLine = br.readLine()) != null) {
        splitLine = strLine.split(" ");
        if(splitLine[0].equalsIgnoreCase("v")) {
          for(int x = 1; x < 4; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayVertices.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("vt")) {
          for(int x = 1; x < 3; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayTextures.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("vn")) {
          for(int x = 1; x < 4; x++) {
            value = Float.parseFloat(splitLine[x]);
            arrayNormals.add(value);
          }
        }
        if(splitLine[0].equalsIgnoreCase("f")) {
          for(int x = 1; x < 4; x++) {
            splitText = splitLine[x].split("/");
            long indexVertex = Integer.parseInt(splitText[0]) - 1;
            int indexText = Integer.parseInt(splitText[1]) - 1;
            int indexNorm = Integer.parseInt(splitText[2]) - 1;
            arrayIndexOrder.add(new IndexOrderObj(indexVertex, indexText, indexNorm));
          }
        }
      }
      ByteBuffer vbb = ByteBuffer.allocateDirect(arrayIndexOrder.size() * 4 * 3);
            vbb.order(ByteOrder.nativeOrder());
            mVertexBuffer = vbb.asFloatBuffer();

            ByteBuffer vtbb = ByteBuffer.allocateDirect(arrayIndexOrder.size() * 4 * 2);
            vtbb.order(ByteOrder.nativeOrder());
            mTexBuffer = vtbb.asFloatBuffer();

            ByteBuffer nbb = ByteBuffer.allocateDirect(arrayIndexOrder.size() * 4 * 3);
            nbb.order(ByteOrder.nativeOrder());
            mNormBuffer = nbb.asFloatBuffer();

            for (int j = 0; j < arrayIndexOrder.size(); j++) {
                    mVertexBuffer.put(arrayVertices.get((int) (arrayIndexOrder.get(j).iVertex * 3)));
                    mVertexBuffer.put(arrayVertices.get((int) (arrayIndexOrder.get(j).iVertex * 3 + 1)));
                    mVertexBuffer.put(arrayVertices.get((int) (arrayIndexOrder.get(j).iVertex * 3 + 2)));

                    mTexBuffer.put(arrayTextures.get(arrayIndexOrder.get(j).iText * 2));
                    mTexBuffer.put(arrayTextures.get((arrayIndexOrder.get(j).iText * 2) + 1));

                    mNormBuffer.put(arrayNormals.get(arrayIndexOrder.get(j).iNorm * 3));
                    mNormBuffer.put(arrayNormals.get((arrayIndexOrder.get(j).iNorm * 3) + 1));
                    mNormBuffer.put(arrayNormals.get((arrayIndexOrder.get(j).iNorm * 3) + 2));
            }

            mIndexBuffer = CharBuffer.allocate(arrayIndexOrder.size());
            indicesCount = arrayIndexOrder.size();
            for (int j = 0; j < arrayIndexOrder.size(); j++) {
                    mIndexBuffer.put((char) j);
            }

            mVertexBuffer.position(0);
            mTexBuffer.position(0);
            mNormBuffer.position(0);
            mIndexBuffer.position(0);
      
//      vertices = new float[arrayIndexOrder.size() * 3];
//      normals = new float[arrayIndexOrder.size() * 3];
//      textCoords = new float[arrayIndexOrder.size() * 2];
//      indices = new short[arrayIndexOrder.size()];
//      for(int y = 0; y < arrayIndexOrder.size(); y++) {
//        vertices[y * 3] = arrayVertices.get((int)arrayIndexOrder.get(y).iVertex * 3).floatValue();
//        vertices[y * 3 + 1] = arrayVertices.get((int)arrayIndexOrder.get(y).iVertex * 3 + 1).floatValue();
//        vertices[y * 3 + 2] = arrayVertices.get((int)arrayIndexOrder.get(y).iVertex * 3 + 2).floatValue();
//        
//        textCoords[y * 2] = arrayTextures.get((int)arrayIndexOrder.get(y).iText * 2).floatValue();
//        textCoords[y * 2 + 1] = arrayTextures.get((int)arrayIndexOrder.get(y).iText * 2 + 1).floatValue();
//        
//        normals[y * 3] = arrayNormals.get((int)arrayIndexOrder.get(y).iNorm * 3).floatValue();
//        normals[y * 3 + 1] = arrayNormals.get((int)arrayIndexOrder.get(y).iNorm * 3 + 1).floatValue();
//        normals[y * 3 + 2] = arrayNormals.get((int)arrayIndexOrder.get(y).iNorm * 3 + 2).floatValue();
//      }
//      
//      for(int y = 0; y < arrayIndices.size(); y++) {
//        indices[y] = (short)y;
//      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  public float[] getVertices() {
    return vertices;
  }

  @Override
  public float[] getColors() {
    return colors;
  }

  @Override
  public float[] getNormals() {
    return normals;
  }

  @Override
  public short[] getDrawOrder() {
    return null;
  }

  @Override
  public float[] getTextCoords() {
    return textCoords;
  }
}




Java Source Code List

com.example.opengldraw.MainActivity.java
com.example.opengldraw.gl.GLSurfaceViewDraw.java
com.example.opengldraw.gl.RendererDraw.java
com.example.opengldraw.gl.ShapeDrawer.java
com.example.opengldraw.shapes.FireEngine.java
com.example.opengldraw.shapes.ShapeCube.java
com.example.opengldraw.shapes.ShapeCup10Verts.java
com.example.opengldraw.shapes.ShapeCup64Verts.java
com.example.opengldraw.shapes.ShapeSquare.java
com.example.opengldraw.utils.IndexOrderObj.java
com.example.opengldraw.utils.ShapeDataInterface.java
com.example.opengldraw.utils.WavefrontObj.java