Obj 网格无法正确渲染

发布于 2025-01-07 07:07:12 字数 3934 浏览 7 评论 0 原文

我在渲染立方体时遇到问题。我不知道为什么,但这是不正确的。

以下是“立方体”的两张图像:

正面:

Front

背面:Front 背面:

Back

这是 OpenGL 代码: 顶点、面、颜色..

private void initTriangle() {
    float[] coords = {
            -0.5f, 0.f, 0.5f,
            -0.5f, 0.f, -0.5f,
            0.5f, 0.f, -0.5f,
            0.5f, 0.f, 0.5f,
            -0.5f, 1.f, 0.5f,
            0.5f, 1.f, 0.5f,
            0.5f, 1.f, -0.5f,
            -0.5f, 1.f, -0.5f,
    };
    _nrOfVertices = coords.length;

    float[] colors = {
            1f, 0f, 0f, 1f, // point 1 red
            0f, 1f, 0f, 1f, // point 2 green
            0f, 0f, 1f, 1f, // point 3 blue
            1f, 1f, 1f, 1f, // point 4 white
            1f, 1f, 1f, 1f, // point 5 white
            1f, 0f, 0f, 1f, // point 6 white
            1f, 1f, 1f, 1f, // point 7 white
            1f, 1f, 1f, 1f, // point 8 white
            1f, 0f, 0f, 1f, // point 9 white
            1f, 1f, 1f, 1f, // point 10 white
            1f, 1f, 1f, 1f, // point 11 white
            1f, 1f, 1f, 1f, // point 12 white
    };

    short[] indices = new short[] {
            1, 2, 3, // 1
            3, 4, 1, // 2
            5, 6, 7, // 3
            7, 8, 5, // 4
            1, 4, 6, // 5
            6, 5, 1, // 6
            4, 3, 7, // 7
            7, 6, 4, // 8
            3, 2, 8, // 9
            8, 7, 3, // 10
            2, 1, 5, // 11
            5, 8, 2, // 12
    };

    // float has 4 bytes, coordinate * 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    _vertexBuffer = vbb.asFloatBuffer();

    // short has 2 bytes, indices * 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    _indexBuffer = ibb.asShortBuffer();

    // float has 4 bytes, colors (RGBA) * 4 bytes
    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    _colorBuffer = cbb.asFloatBuffer();

    _vertexBuffer.put(coords);
    _indexBuffer.put(indices);
    _colorBuffer.put(colors);

    _vertexBuffer.position(0);
    _indexBuffer.position(0);
    _colorBuffer.position(0);
}

渲染: 公共无效onDrawFrame(GL10 gl){ //gl.glFrontFace(GL10.GL_CCW); gl.glClearColor(0f, 0f, 0f, 1.0f);

    gl.glLoadIdentity();

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // set rotation
    gl.glRotatef(_xAngle, 1f, 0f, 0f);
    gl.glRotatef(_yAngle, 0f, 1f, 0f);


    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
    gl.glFrontFace(GL10.GL_CCW);
}

这里是obj内容:

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 19.02.2012 13:31:12

mtllib box22.mtl

#
# object Box01
#

v  -0.5000 0.0000 0.5000
v  -0.5000 0.0000 -0.5000
v  0.5000 0.0000 -0.5000
v  0.5000 0.0000 0.5000
v  -0.5000 1.0000 0.5000
v  0.5000 1.0000 0.5000
v  0.5000 1.0000 -0.5000
v  -0.5000 1.0000 -0.5000
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
# 6 vertex normals

vt 1.0000 0.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
# 4 texture coords

g Box01
usemtl wire_177088026
f 1/1/1 2/2/1 3/3/1 
f 3/3/1 4/4/1 1/1/1 
f 5/4/2 6/1/2 7/2/2 
f 7/2/2 8/3/2 5/4/2 
f 1/4/3 4/1/3 6/2/3 
f 6/2/3 5/3/3 1/4/3 
f 4/4/4 3/1/4 7/2/4 
f 7/2/4 6/3/4 4/4/4 
f 3/4/5 2/1/5 8/2/5 
f 8/2/5 7/3/5 3/4/5 
f 2/4/6 1/1/6 5/2/6 
f 5/2/6 8/3/6 2/4/6 
# 12 faces

希望你能帮助我。

感谢您的关注(:

I have a problem with the rendering of a cube. I don't know why but it isn't correct.

Here are two images of the "cube":

Front:

Front

Back:

Back

Here's the OpenGL code:
Vertices, faces, colors..

private void initTriangle() {
    float[] coords = {
            -0.5f, 0.f, 0.5f,
            -0.5f, 0.f, -0.5f,
            0.5f, 0.f, -0.5f,
            0.5f, 0.f, 0.5f,
            -0.5f, 1.f, 0.5f,
            0.5f, 1.f, 0.5f,
            0.5f, 1.f, -0.5f,
            -0.5f, 1.f, -0.5f,
    };
    _nrOfVertices = coords.length;

    float[] colors = {
            1f, 0f, 0f, 1f, // point 1 red
            0f, 1f, 0f, 1f, // point 2 green
            0f, 0f, 1f, 1f, // point 3 blue
            1f, 1f, 1f, 1f, // point 4 white
            1f, 1f, 1f, 1f, // point 5 white
            1f, 0f, 0f, 1f, // point 6 white
            1f, 1f, 1f, 1f, // point 7 white
            1f, 1f, 1f, 1f, // point 8 white
            1f, 0f, 0f, 1f, // point 9 white
            1f, 1f, 1f, 1f, // point 10 white
            1f, 1f, 1f, 1f, // point 11 white
            1f, 1f, 1f, 1f, // point 12 white
    };

    short[] indices = new short[] {
            1, 2, 3, // 1
            3, 4, 1, // 2
            5, 6, 7, // 3
            7, 8, 5, // 4
            1, 4, 6, // 5
            6, 5, 1, // 6
            4, 3, 7, // 7
            7, 6, 4, // 8
            3, 2, 8, // 9
            8, 7, 3, // 10
            2, 1, 5, // 11
            5, 8, 2, // 12
    };

    // float has 4 bytes, coordinate * 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    _vertexBuffer = vbb.asFloatBuffer();

    // short has 2 bytes, indices * 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    _indexBuffer = ibb.asShortBuffer();

    // float has 4 bytes, colors (RGBA) * 4 bytes
    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    _colorBuffer = cbb.asFloatBuffer();

    _vertexBuffer.put(coords);
    _indexBuffer.put(indices);
    _colorBuffer.put(colors);

    _vertexBuffer.position(0);
    _indexBuffer.position(0);
    _colorBuffer.position(0);
}

Rendering:
public void onDrawFrame(GL10 gl) {
//gl.glFrontFace(GL10.GL_CCW);
gl.glClearColor(0f, 0f, 0f, 1.0f);

    gl.glLoadIdentity();

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // set rotation
    gl.glRotatef(_xAngle, 1f, 0f, 0f);
    gl.glRotatef(_yAngle, 0f, 1f, 0f);


    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
    gl.glFrontFace(GL10.GL_CCW);
}

here the obj content:

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 19.02.2012 13:31:12

mtllib box22.mtl

#
# object Box01
#

v  -0.5000 0.0000 0.5000
v  -0.5000 0.0000 -0.5000
v  0.5000 0.0000 -0.5000
v  0.5000 0.0000 0.5000
v  -0.5000 1.0000 0.5000
v  0.5000 1.0000 0.5000
v  0.5000 1.0000 -0.5000
v  -0.5000 1.0000 -0.5000
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
# 6 vertex normals

vt 1.0000 0.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
# 4 texture coords

g Box01
usemtl wire_177088026
f 1/1/1 2/2/1 3/3/1 
f 3/3/1 4/4/1 1/1/1 
f 5/4/2 6/1/2 7/2/2 
f 7/2/2 8/3/2 5/4/2 
f 1/4/3 4/1/3 6/2/3 
f 6/2/3 5/3/3 1/4/3 
f 4/4/4 3/1/4 7/2/4 
f 7/2/4 6/3/4 4/4/4 
f 3/4/5 2/1/5 8/2/5 
f 8/2/5 7/3/5 3/4/5 
f 2/4/6 1/1/6 5/2/6 
f 5/2/6 8/3/6 2/4/6 
# 12 faces

Hope you can help me.

Thank for your attention (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2025-01-14 07:07:12

确保您的索引正确。

OBJ 格式从索引 1 开始计数。但是,数组中的实际索引将从 0 开始。因此,您必须从 OBJ 文件中的每个索引中减去 1,才能使其按预期工作。

Make sure your indices are correct.

The OBJ format starts counting with indices at 1. However, the actual indexing within your array will start at 0. So you will have to subtract 1 from each index within the OBJ file for it to work as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文