Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nea.nehe.lesson08;
- import android.content.Context;
- import android.content.res.Resources;
- import android.util.Log;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Vector;
- public class Mesh
- {
- float[] vertices;
- float[] normals;
- float[] uv;
- short[] facesVerts;
- int[] facesUV;
- int[] facesNormals;
- public Mesh(Context paramContext, int paramInt)
- {
- Resources localResources = paramContext.getResources();
- InputStream localInputStream = localResources.openRawResource(paramInt);
- try {
- loadObj(localInputStream);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void loadObj (InputStream in)
- {
- String line = "";
- try
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- StringBuffer b = new StringBuffer();
- String l = reader.readLine();
- while (l != null)
- {
- b.append(l);
- b.append("\n");
- l = reader.readLine();
- }
- line = b.toString();
- reader.close();
- }
- catch (Exception ex)
- { }
- loadObjFromString(line);
- }
- public void loadObjFromString (String obj)
- {
- String[] lines = obj.split("\n");
- vertices = new float[lines.length * 3];
- normals = new float[lines.length * 3];
- uv = new float[lines.length * 3];
- int numVertices = 0;
- int numNormals = 0;
- int numUV = 0;
- int numFaces = 0;
- facesVerts = new short[lines.length * 3];
- facesNormals = new int[lines.length * 3];
- facesUV = new int[lines.length * 3];
- int vertexIndex = 0;
- int normalIndex = 0;
- int uvIndex = 0;
- int faceIndex = 0;
- for (int i = 0; i < lines.length; i++)
- {
- String line = lines[i];
- if (line.startsWith("v "))
- {
- String[] tokens = line.split("[ ]+");
- vertices[vertexIndex] = Float.parseFloat(tokens[1]);
- vertices[vertexIndex + 1] = Float.parseFloat(tokens[2]);
- vertices[vertexIndex + 2] = Float.parseFloat(tokens[3]);
- vertexIndex += 3;
- numVertices++;
- continue;
- }
- if (line.startsWith("vn "))
- {
- String[] tokens = line.split("[ ]+");
- normals[normalIndex] = Float.parseFloat(tokens[1]);
- normals[normalIndex + 1] = Float.parseFloat(tokens[2]);
- normals[normalIndex + 2] = Float.parseFloat(tokens[3]);
- normalIndex += 3;
- numNormals++;
- continue;
- }
- if (line.startsWith("vt"))
- {
- String[] tokens = line.split("[ ]+");
- uv[uvIndex] = Float.parseFloat(tokens[1]);
- uv[uvIndex + 1] = Float.parseFloat(tokens[2]);
- uvIndex += 2;
- numUV++;
- continue;
- }
- if (line.startsWith("f "))
- {
- String[] tokens = line.split("[ ]+");
- String[] parts = tokens[1].split("/");
- facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
- if (parts.length > 2){facesNormals[faceIndex] = getIndex(parts[2], numNormals);}
- if (parts.length > 1){facesUV[faceIndex] = getIndex(parts[1], numUV);}
- faceIndex++;
- parts = tokens[2].split("/");
- facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
- if (parts.length > 2) facesNormals[faceIndex] = getIndex(parts[2], numNormals);
- if (parts.length > 1) facesUV[faceIndex] = getIndex(parts[1], numUV);
- faceIndex++;
- parts = tokens[3].split("/");
- facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
- if (parts.length > 2) facesNormals[faceIndex] = getIndex(parts[2], numNormals);
- if (parts.length > 1) facesUV[faceIndex] = getIndex(parts[1], numUV);
- faceIndex++;
- numFaces++;
- continue;
- }
- }
- float[] verts = new float[(numFaces * 3) * (3 + (numNormals > 0 ? 3 : 0) + (numUV > 0 ? 2 : 0))];
- for (int i = 0, vi = 0; i < numFaces * 3; i++)
- {
- int vertexIdx = facesVerts[i] * 3;
- verts[vi++] = vertices[vertexIdx];
- verts[vi++] = vertices[vertexIdx + 1];
- verts[vi++] = vertices[vertexIdx + 2];
- if (numNormals > 0)
- {
- int normalIdx = facesNormals[i] * 3;
- verts[vi++] = normals[normalIdx];
- verts[vi++] = normals[normalIdx + 1];
- verts[vi++] = normals[normalIdx + 2];
- }
- if (numUV > 0)
- {
- int uvIdx = facesUV[i] * 2;
- verts[vi++] = uv[uvIdx];
- verts[vi++] = uv[uvIdx + 1];
- }
- }
- }
- /* private static int[] convertToFixedPoint (float[] fverts)
- {
- int[] fpverts = new int[fverts.length];
- for (int i = 0; i < fverts.length; i++)
- fpverts[i] = (int)(fverts[i] * 65536);
- ;
- return fpverts;
- }*/
- private static int getIndex (String index, int size)
- {
- if (index == null || index.length() == 0) return 0;
- int idx = Integer.parseInt(index);
- if (idx < 0)
- return size + idx;
- else
- return idx - 1;
- }
- public float[] getNorms()
- {
- return normals;
- }
- public float[] getTexs()
- {
- return uv;
- }
- public float[] getVerts()
- {
- return vertices;
- }
- public short[] getIndices()
- {
- return facesVerts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement