android - 如何使用 OpenGL-ES 2 在 Android 中加载和显示 .obj 文件

标签 android opengl-es opengl-es-2.0

我正在尝试将 .obj 文件加载到我的 Android 应用程序中并使用 OpenGL 2 显示它。

您可以在此处找到该文件:编辑:我删除了该文件,您可以使用任何包含下面提到的值的 .obj 文件进行测试。

stackoverflow上有很多类似的问题,但我没有找到不需要一些大型库的简单解决方案。

该文件仅包含以下值类型:

  • g
  • v
  • vt
  • vn
  • f

我尝试了 libgdx,它工作正常,但对于我需要的东西来说有点矫枉过正。

我尝试了 oObjLoader https://github.com/seanrowens/oObjLoader没有 LWJGL。解析似乎有效,但如何在简单场景中显示值?

下一步是将图像作为纹理附加到对象。但现在我很乐意按原样显示文件。

我对不同的解决方案持开放态度,例如预转换文件,因为在应用程序中它只会是这个。

谢谢!

状态更新 基本加载和显示现在有效,如我自己的答案所示。

最佳答案

我最终编写了一个新的解析器,可以像这样使用它来构建 FloatBuffers 以在您的渲染器中使用:

ObjLoader objLoader = new ObjLoader(context, "Mug.obj");

numFaces = objLoader.numFaces;

// Initialize the buffers.
positions = ByteBuffer.allocateDirect(objLoader.positions.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
positions.put(objLoader.positions).position(0);

normals = ByteBuffer.allocateDirect(objLoader.normals.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
normals.put(objLoader.normals).position(0);

textureCoordinates = ByteBuffer.allocateDirect(objLoader.textureCoordinates.length * mBytesPerFloat)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
textureCoordinates.put(objLoader.textureCoordinates).position(0);

这是解析器:

public final class ObjLoader {

    public final int numFaces;

    public final float[] normals;
    public final float[] textureCoordinates;
    public final float[] positions;

    public ObjLoader(Context context, String file) {

        Vector<Float> vertices = new Vector<>();
        Vector<Float> normals = new Vector<>();
        Vector<Float> textures = new Vector<>();
        Vector<String> faces = new Vector<>();

        BufferedReader reader = null;
        try {
            InputStreamReader in = new InputStreamReader(context.getAssets().open(file));
            reader = new BufferedReader(in);

            // read file until EOF
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                switch (parts[0]) {
                    case "v":
                        // vertices
                        vertices.add(Float.valueOf(parts[1]));
                        vertices.add(Float.valueOf(parts[2]));
                        vertices.add(Float.valueOf(parts[3]));
                        break;
                    case "vt":
                        // textures
                        textures.add(Float.valueOf(parts[1]));
                        textures.add(Float.valueOf(parts[2]));
                        break;
                    case "vn":
                        // normals
                        normals.add(Float.valueOf(parts[1]));
                        normals.add(Float.valueOf(parts[2]));
                        normals.add(Float.valueOf(parts[3]));
                        break;
                    case "f":
                        // faces: vertex/texture/normal
                        faces.add(parts[1]);
                        faces.add(parts[2]);
                        faces.add(parts[3]);
                        break;
                }
            }
        } catch (IOException e) {
            // cannot load or read file
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }

        numFaces = faces.size();
        this.normals = new float[numFaces * 3];
        textureCoordinates = new float[numFaces * 2];
        positions = new float[numFaces * 3];
        int positionIndex = 0;
        int normalIndex = 0;
        int textureIndex = 0;
        for (String face : faces) {
            String[] parts = face.split("/");

            int index = 3 * (Short.valueOf(parts[0]) - 1);
            positions[positionIndex++] = vertices.get(index++);
            positions[positionIndex++] = vertices.get(index++);
            positions[positionIndex++] = vertices.get(index);

            index = 2 * (Short.valueOf(parts[1]) - 1);
            textureCoordinates[normalIndex++] = textures.get(index++);
            // NOTE: Bitmap gets y-inverted
            textureCoordinates[normalIndex++] = 1 - textures.get(index);

            index = 3 * (Short.valueOf(parts[2]) - 1);
            this.normals[textureIndex++] = normals.get(index++);
            this.normals[textureIndex++] = normals.get(index++);
            this.normals[textureIndex++] = normals.get(index);
        }
    }
}

关于android - 如何使用 OpenGL-ES 2 在 Android 中加载和显示 .obj 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41012719/

相关文章:

android - 通过 FileProvider 和 Intent 将缓存文件附加到 GMail 不起作用

javascript - Android 中的页面转换不流畅

java - 将 jPCT 与 Vuforia/QCAR SDK 集成

iphone - iPhone 上的 glgConvertTo_32

ios - GLSL:更改两个以上的顶点尺寸会使着色器崩溃

java - 当我完全删除该值时 onTextChanged

ios - OpenGL ES glFragColor 取决于 iOS 中片段着色器的条件

c++ - 调用函数后程序忽略所有内容

C 看似微不足道的优化尚未完成

android - MVVM (Android) 中的文件结构