gpt4 book ai didi

android - 在 java 中编写 .3ds 或 .obj 加载程序

转载 作者:行者123 更新时间:2023-11-30 04:41:34 25 4
gpt4 key购买 nike

我将 blender 中的模型导出到 .obj 文件。我设法创建了一个非常简单的将顶点和索引加载到数组的类。我的问题是我还想要纹理坐标 (vt) 和法线 (vn)。因此,例如,对于一个简单的立方体,我需要 4 个顶点 * 6 个面才能使用纹理,但我的 .obj 文件中只有 8 个,而且我不知道如何处理索引vt 因为我只能有一个数组/缓冲区用于索引,但我在 .obj 文件中得到两个不同的 v/vt。

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

最佳答案

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

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

此代码适用于 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;
}



}

关于android - 在 java 中编写 .3ds 或 .obj 加载程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com