[Java] [OpenGL ES3.2] ObjObject2.0

分析

在部分建模软件上的面可能不是3个顶点,而是4个顶点的,如果用原来的loadFromStream()就会丢失面

代码

package com.Diamond.gl07;

import java.io.InputStream;
import java.util.ArrayList;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import android.util.Log;

public class ObjObject extends MyObject {
	public float[] vertices = null;
	public float[] normals = null;
	public float[] texCoords = null;
	public int[] indices = null;

    public static ObjObject loadFromStreamOnlyVerticesAndIndices(InputStream is) {
        ArrayList<Float> vertexArray = new ArrayList<Float>();
        ArrayList<Integer> indexArray = new ArrayList<Integer>();

        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String temp = null;

        try {
            while ((temp = br.readLine()) != null) {
                String[] templist = temp.split("[ ]+");
                if (templist[0].trim().equals("v")) {
                    vertexArray.add(Float.parseFloat(templist[1]));
                    vertexArray.add(Float.parseFloat(templist[2]));
                    vertexArray.add(Float.parseFloat(templist[3]));
                } else if (templist[0].trim().equals("f")) {
                    String[] tempf1 = templist[1].split("[/]+");
                    String[] tempf2 = templist[2].split("[/]+");
                    String[] tempf3 = templist[3].split("[/]+");
                    indexArray.add(Integer.parseInt(tempf1[0]) - 1);
                    indexArray.add(Integer.parseInt(tempf2[0]) - 1);
                    indexArray.add(Integer.parseInt(tempf3[0]) - 1);
                }
            }
        } catch (IOException e) {
            Log.e("ObjLoader", "");
            e.printStackTrace();
        }

        float[] vertices = new float[vertexArray.size()];
        for (int i = 0;i < vertexArray.size();i++) {
            vertices[i] = vertexArray.get(i);
        }
        int[] indices = new int[indexArray.size()];
        for (int i = 0;i < indexArray.size();i++) {
            indices[i] = indexArray.get(i);
        }

        ObjObject result = new ObjObject(vertices, null, null, indices);

        return result;
    }

    public static ObjObject loadFromStream(InputStream is) {
        ArrayList<Float> vertexArray = new ArrayList<Float>();
        ArrayList<Float> normalArray = new ArrayList<Float>();
        ArrayList<Float> texCoordArray = new ArrayList<Float>();

        ArrayList<Float> vertexArrayResult = new ArrayList<Float>();
        ArrayList<Float> normalArrayResult = new ArrayList<Float>();
        ArrayList<Float> texCoordArrayResult = new ArrayList<Float>();

        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String temp = null;

        try {
            while ((temp = br.readLine()) != null) {
                String[] templist = temp.split("[ ]+");
                if (templist[0].trim().equals("v")) {
                    vertexArray.add(Float.parseFloat(templist[1]));
                    vertexArray.add(Float.parseFloat(templist[2]));
                    vertexArray.add(Float.parseFloat(templist[3]));
                } else if (templist[0].trim().equals("vn")) {
                	normalArray.add(Float.parseFloat(templist[1]));
                    normalArray.add(Float.parseFloat(templist[2]));
                    normalArray.add(Float.parseFloat(templist[3]));
                } else if (templist[0].trim().equals("vt")) {
                	texCoordArray.add(Float.parseFloat(templist[1]));
                    texCoordArray.add(Float.parseFloat(templist[2]));
                    texCoordArray.add(Float.parseFloat(templist[3]));
                } else if (templist[0].trim().equals("f")) {
                    String[][] temps = new String[templist.size() - 1];
                    for(int i = 0;i < temps.size();i++) {
                        temps[i] = templist[i + 1].split("[/]+");
                    }
                    boolean ibt3 = temps.size() > 3; //is bigger than 3,像极了把int打成ibt
                    
                    int index = Integer.parseInt(temps[0][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));
                    index = Integer.parseInt(temps[1][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));
                    index = Integer.parseInt(temps[2][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));

                    if(ibt3) {
                    index = Integer.parseInt(temps[0][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));
                    index = Integer.parseInt(temps[2][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));
                    index = Integer.parseInt(temps[3][0]) - 1;
                    vertexArrayResult.add(vertexArray.get(3 * index));
                    vertexArrayResult.add(vertexArray.get(3 * index + 1));
                    vertexArrayResult.add(vertexArray.get(3 * index + 2));
                    }

                    if (texCoordArray.size() != 0) {
                        index = Integer.parseInt(temps[0][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[1][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[2][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));

                        if(ibt3) {
                        index = Integer.parseInt(temps[0][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[2][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[3][1]) - 1;
                        texCoordArrayResult.add(texCoordArray.get(3 * index));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                        texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                        }
                    }

                    if (normalArray.size() != 0) {
                        index = Integer.parseInt(temps[0][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[1][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[2][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));

                        if(ibt3) {
                        index = Integer.parseInt(temps[0][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[2][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));
                        index = Integer.parseInt(temps[3][2]) - 1;
                        normalArrayResult.add(normalArray.get(3 * index));
                        normalArrayResult.add(normalArray.get(3 * index + 1));
                        normalArrayResult.add(normalArray.get(3 * index + 2));
                        }
                    }
                }
            }
        } catch (IOException e) {
            Log.e("ObjLoader", "");
            e.printStackTrace();
        }

        float[] vertices = new float[vertexArrayResult.size()];
        for (int i = 0;i < vertexArrayResult.size();i++) {
            vertices[i] = vertexArrayResult.get(i);
        }
        float[] normals = new float[normalArrayResult.size()];
        for (int i = 0;i < normalArrayResult.size();i++) {
            normals[i] = normalArrayResult.get(i);
        }
        float[] texCoords = new float[texCoordArrayResult.size()];
        for (int i = 0;i < texCoordArrayResult.size();i++) {
            texCoords[i] = texCoordArrayResult.get(i);
        }
        int[] indices = new int[indexArray.size()];
        for (int i = 0;i < indexArray.size();i++) {
            indices[i] = indexArray.get(i);
        }

        ObjObject result = new ObjObject(vertices, normals, texCoords, indices);

        return result;
    }
    
    
    
    public ObjObject() {}
    public ObjObject(ObjObject obj) {
        this.vertices = obj.vertices;
        this.normals = obj.normals;
        this.texCoords = obj.texCoords;
        this.indices = obj.indices;
    }
    public ObjObject(float[] vertices, float[] normals, float[] texCoords, int[] indices) {
        this.vertices = vertices;
        this.normals = normals;
        this.texCoords = texCoords;
        this.indices = indices;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值