栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

[Java] [OpenGL ES 3.2] 加载obj文件 3.0

[Java] [OpenGL ES 3.2] 加载obj文件 3.0

package com.Diamond.SGL;

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 ObjLoader {
    public float[] vertices = null;
    public float[] normals = null;
    public float[] texCoords = null;
    public int[] indices = null;

    public ObjLoader() {}
    public ObjLoader(InputStream is) {
        ObjLoader result = ObjLoader.loadFromStream_Triangles(is);
        vertices = result.vertices;
        normals = result.normals;
        texCoords = result.texCoords;
        indices = result.indices;
    }
    public ObjLoader(float[] v, float[] n, float[] t, int[] i) {
        vertices = v;
        normals = n;
        texCoords = t;
        indices = i;
    }

    public static ObjLoader loadFromStreamOnlyVerticesAndIndices(InputStream is) {
        ArrayList vertexArray = new ArrayList();
        ArrayList indexArray = new ArrayList();

        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(toInt(tempf1[0]) - 1);
                    indexArray.add(toInt(tempf2[0]) - 1);
                    indexArray.add(toInt(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);
        }

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

        return result;
    }



    public static ObjLoader loadFromStream_Triangles(InputStream is) {
        ArrayList vertexArray = new ArrayList();
        ArrayList normalArray = new ArrayList();
        ArrayList texCoordArray = new ArrayList();

        ArrayList vertexArrayResult = new ArrayList();
        ArrayList normalArrayResult = new ArrayList();
        ArrayList texCoordArrayResult = new ArrayList();

        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.length - 1][];
                    for (int i = 0;i < temps.length;i++) {
                        temps[i] = templist[i + 1].split("[/]+");
                    }

                    int index = 0;
                    int n = temps.length - 2;

                    for (int i = 0; i < n; i++) {
                        index = toInt(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 = toInt(temps[i + 1][0]) - 1;
                        vertexArrayResult.add(vertexArray.get(3 * index));
                        vertexArrayResult.add(vertexArray.get(3 * index + 1));
                        vertexArrayResult.add(vertexArray.get(3 * index + 2));
                        index = toInt(temps[i + 2][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) {
                        for (int i = 0; i < n; i++) {
                            index = toInt(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 = toInt(temps[i + 1][1]) - 1;
                            texCoordArrayResult.add(texCoordArray.get(3 * index));
                            texCoordArrayResult.add(texCoordArray.get(3 * index + 1));
                            texCoordArrayResult.add(texCoordArray.get(3 * index + 2));
                            index = toInt(temps[i + 2][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) {
                        for (int i = 0; i < n; i++) {
                            index = toInt(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 = toInt(temps[i + 1][2]) - 1;
                            normalArrayResult.add(normalArray.get(3 * index));
                            normalArrayResult.add(normalArray.get(3 * index + 1));
                            normalArrayResult.add(normalArray.get(3 * index + 2));
                            index = toInt(temps[i + 2][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);
        }

        ObjLoader result = new ObjLoader(vertices, normals, texCoords, null);

        return result;
    }



    public static ObjLoader loadFromStream_TriangleFan(InputStream is) {
        ArrayList vertexArray = new ArrayList();
        ArrayList normalArray = new ArrayList();
        ArrayList texCoordArray = new ArrayList();

        ArrayList vertexArrayResult = new ArrayList();
        ArrayList normalArrayResult = new ArrayList();
        ArrayList texCoordArrayResult = new ArrayList();

        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.length - 1][];
                    for (int i = 0;i < temps.length;i++) {
                        temps[i] = templist[i + 1].split("[/]+");
                    }

                    int index = 0;
                    int n = temps.length - 2;

                    for (int i = 0; i < n; i++) {
                        index = toInt(temps[i][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) {
                        for (int i = 0; i < n; i++) {
                            index = toInt(temps[i][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) {
                        for (int i = 0; i < n; i++) {
                            index = toInt(temps[i][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);
        }

        ObjLoader result = new ObjLoader(vertices, normals, texCoords, null);

        return result;
    }



    public static int toInt(String str) {
        return Integer.parseInt(str);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/746709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号