2

我尝试使用 VBO 来渲染我的 3d 对象。

我创建了这个类:

public class Object3DTexturedRendered {

private TextureRendered texture;
private int vboId = 0;
private int vboiId = 0;
private int vbotId = 0;
private int verticesTexCoords[][];
private int verticesIndex[];
private FloatBuffer verticesBuffer;
private FloatBuffer texturesBuffer;
private IntBuffer indexBuffer;

public Object3DTexturedRendered() {

}

public Object3DTexturedRendered(TextureRendered _texture) {
    texture = _texture;
}

public void renderVBO() {
    glEnable(GL_TEXTURE_2D);

    if (texture.getTexture() != null) {
        texture.getTexture().bind();
    }

    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vboId);
    glVertexPointer(3, GL_FLOAT, 0, 0L);

    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vbotId);
    glTexCoordPointer(2, GL_FLOAT, 0, 0L);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, vboiId);
    glDrawRangeElements(GL_TRIANGLES, 0, (verticesIndex.length - 1), verticesIndex.length,
                                          GL_UNSIGNED_INT, 0L);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, 0);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, 0);

    glDisable(GL_TEXTURE_2D);
}

public void initRenderVBO(FloatPointRendered[] listPointsRendered, int _verticesTexCoords[][], int _verticesIndex[]) {
    verticesTexCoords = _verticesTexCoords;
    verticesIndex = _verticesIndex;

    verticesBuffer = BufferUtils.createFloatBuffer(verticesTexCoords.length * 3);
    texturesBuffer = BufferUtils.createFloatBuffer(verticesTexCoords.length * 2);
    for (int[] _ipoint : verticesTexCoords) {
        FloatPointRendered _point = listPointsRendered[_ipoint[0]];
        float[] uv = texture.getXYtoFloat(_ipoint[1], _ipoint[2]);
        verticesBuffer.put(_point.positionToFloatArray());
        texturesBuffer.put(uv);
    }
    verticesBuffer.flip();
    texturesBuffer.flip();

    indexBuffer = BufferUtils.createIntBuffer(verticesIndex.length);
    indexBuffer.put(verticesIndex);
    indexBuffer.flip();

    if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
        IntBuffer vboID = BufferUtils.createIntBuffer(3);
        ARBVertexBufferObject.glGenBuffersARB(vboID);
        vboId = vboID.get(0);
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vboId);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, verticesBuffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);

        vbotId = vboID.get(1);
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vbotId);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, texturesBuffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);

        vboiId = vboID.get(2);
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, vboiId);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBuffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
    }
}

我的对象都被正确渲染了,但是我的纹理有问题。我的纹理是跨步的。看看这张图片......为什么我的纹理是这样的,以及我如何修复这个错误。

Battleofground.fr_game_render_error_vbo_texture.png

4

1 回答 1

0

解决 !

事实上,我没有正确地将我的纹理剪切到渲染的 GL_TRIANGLES 中,因此纹理坐标没有正确计算以应用于 GL_TRIANGLES !

于 2016-05-12T12:13:40.707 回答