在java中编写.3ds或.obj加载器

时间:2011-05-05 00:20:43

标签: android opengl-es loader .obj 3ds

我在混合器中将模型导出到.obj文件。我设法创造了一个非常简单的 将顶点和索引加载到数组的类。我的问题是我想要纹理坐标(vt)和法线(vn)。所以,例如,我需要4个顶点* 6个面,一个简单的立方体能够使用纹理,但我只在我的.obj文件中得到8,以及我没有关于如何处理指数的线索vt,因为我只能有一个数组/缓冲区用于索引,但我在.obj文件中得到两个不同的v / vt。

那里是否有任何装载器只返回顶点,纹理,法线和一个索引数组的数组或类似数据?或者如何写一个例子?到目前为止,我只在完整的3D引擎中找到了装载机,这不是我想要的。

2 个答案:

答案 0 :(得分:1)

4个顶点* 6个面超出了你的需要。实际上效率不高。您获得的导出顶点将使用索引进行优化。使用Opengl-es,您可以从哪里获取顶点(数组),然后使用另一个数组中的索引绘制顶点。结果你得到8个顶点而不是24个顶点,你需要更少的内存来存储。因此效率为16/24 * 100%。想象一下,你将拥有一个包含1000个顶点的模型。

顶点索引意味着在具有适当偏移量的另一个数组中GPU将获得顶点(size_of_vertex(3浮点数)*索引)和UV坐标的适当偏移量(size_of_UVcoord(2浮点数)*索引)

这个代码用于opengl ES 2.0,但你可以得到一个想法:

GLES20.glUseProgram(programTextured);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
sqTex.getVertexBuffer().position(sqTex.VERT_OFFSET);
GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(programTextured, "aPosition") 3,                                    GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());               GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aPosition"));

sqTex.getVertexBuffer().position(sqTex.TEXT_OFFSET);
GLES20.glVertexAttribPointer(
                                GLES20.glGetAttribLocation(programTextured, "aTextureCoord"), 2,
                                GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, sqTex.getIndexBuffer());

和sqTEx是TexturedSquare的一个实例:

public class TexturedSquare {

        // private float[] vertices=new float[4];

        float vertices[] = { -1.0f, -1.0f, 0.0f,0.0f,0.0f, // 0, Top Left  //x,y,z,u,v
                        1.0f, -1.0f, 0.0f,0.0f,1.0f, // 1, Bottom Left
                        1.0f, 1.0f, 0.0f,1.0f,1.0f, // 2, Bottom Right
                        -1.0f, 1.0f, 0.0f,1.0f,0.0f, // 3, Top Right
        };

        public static int VERT_OFFSET=0;
        public static int TEXT_OFFSET=3;

        short[] indices = { 0, 1, 2, 2, 3, 0 };;

        // Our vertex buffer.
        private FloatBuffer vertexBuffer;

        // Our index buffer.
        private ShortBuffer indexBuffer;

        public TexturedSquare()
        {
                ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
                vbb.order(ByteOrder.nativeOrder());
                vertexBuffer = vbb.asFloatBuffer();
                vertexBuffer.put(vertices);
                vertexBuffer.position(0);

                // short is 2 bytes, therefore we multiply the number if
                // vertices with 2.
                ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
                ibb.order(ByteOrder.nativeOrder());
                indexBuffer = ibb.asShortBuffer();
                indexBuffer.put(indices);
                indexBuffer.position(0);

        }

        FloatBuffer getVertexBuffer(){
                return vertexBuffer;
        }

        ShortBuffer getIndexBuffer(){
                return indexBuffer;
        }



}

答案 1 :(得分:0)

看一下jPCT(或jPCT-AE),它是一个优秀的Java(和/或Android)3D库。它支持开箱即用的3ds / obj加载。

相关问题