FloatBuffer腐败问题

时间:2013-04-20 18:04:36

标签: java android memory opengl-es

我正在根据DooM地图布局编写3D渲染器/引擎并将其移植到Android。我的原始算法非常慢,我使用方法ID为他们的iPhone端口改进了它。这是功能:

public void renderScene(GL10 gl, Map map) {
    int currentTexture = renderWalls[0].texID;
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[currentTexture]);

    cFrame.reset();

    for (int i = 0; i < numWalls; i++) {
        Wall wall = renderWalls[i];

        // Draw if texture change
        if (wall.texID != currentTexture) {
            cFrame.transfer();
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            gl.glVertexPointer(3, GL10.GL_FLOAT, 4 * 5, cFrame.verts);
            // Create a buffer that points 3 floats past the beginning.
            FloatBuffer texData = cFrame.verts.duplicate();
            texData.position(3);
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 4 * 5, texData);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, cFrame.numIndices,
                    GL10.GL_UNSIGNED_SHORT, cFrame.indices);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            cFrame.reset();
            currentTexture = wall.texID;
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[currentTexture]);
        }

        cFrame.vertices[(cFrame.numVerts * 5)] = wall.p1.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 1] = wall.top;
        cFrame.vertices[(cFrame.numVerts * 5) + 2] = wall.p1.y;

        cFrame.vertices[(cFrame.numVerts * 5) + 3] = wall.uv1.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 4] = wall.uv1.y;

        cFrame.numVerts++;

        cFrame.vertices[(cFrame.numVerts * 5)] = wall.p1.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 1] = wall.bottom;
        cFrame.vertices[(cFrame.numVerts * 5) + 2] = wall.p1.y;

        cFrame.vertices[(cFrame.numVerts * 5) + 3] = wall.uv2.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 4] = wall.uv2.y;

        cFrame.numVerts++;

        cFrame.vertices[(cFrame.numVerts * 5)] = wall.p2.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 1] = wall.top;
        cFrame.vertices[(cFrame.numVerts * 5) + 2] = wall.p2.y;

        cFrame.vertices[(cFrame.numVerts * 5) + 3] = wall.uv3.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 4] = wall.uv3.y;

        cFrame.numVerts++;

        cFrame.vertices[(cFrame.numVerts * 5)] = wall.p2.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 1] = wall.bottom;
        cFrame.vertices[(cFrame.numVerts * 5) + 2] = wall.p2.y;

        cFrame.vertices[(cFrame.numVerts * 5) + 3] = wall.uv4.x;
        cFrame.vertices[(cFrame.numVerts * 5) + 4] = wall.uv4.y;

        cFrame.numVerts++;

        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 2);
        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 3);
        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 4);
        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 3);
        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 2);
        cFrame.indexes[cFrame.numIndices++] = (short) (cFrame.numVerts - 1);
    }
    cFrame.transfer();
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 4 * 5, cFrame.verts);
    // Create a buffer that points 3 floats past the beginning.
    FloatBuffer texData = cFrame.verts.duplicate();
    texData.position(3);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 4 * 5, texData);
    gl.glDrawElements(GL10.GL_TRIANGLES, cFrame.numIndices,
            GL10.GL_UNSIGNED_SHORT, cFrame.indices);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);   
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

我浏览BSP树并收集将呈现的行。这些被放入一个Walls数组(存储vert和tex coords / id)并通过纹理id快速排序。然后运行以下函数。

我将问题缩小到前三次投注的腐败问题。前三个是一些奇怪的浮动值,其余是正常的。

cFrame对象计算顶点/索引的数量,并从数组传递到floatbuffer。这是类/函数

class CurrentFrame {
    public short numVerts, numIndices;
    public FloatBuffer verts;
    public ShortBuffer indices;

    public float vertices[];
    public short indexes[];

    public CurrentFrame(int maxVal) {
        ByteBuffer vertsBB = ByteBuffer.allocateDirect(maxVal * 4);
        vertsBB.order(ByteOrder.nativeOrder());
        verts = vertsBB.asFloatBuffer();

        ByteBuffer indBB = ByteBuffer.allocateDirect(maxVal * 2);
        indBB.order(ByteOrder.nativeOrder());
        indices = vertsBB.asShortBuffer();

        vertices = new float[maxVal];
        indexes = new short[maxVal];
    }

    public void reset() {
        cFrame.numIndices = 0;
        cFrame.numVerts = 0;
        cFrame.verts.position(0);
        cFrame.indices.position(0);
    }

    public void transfer() {
        verts.position(0);
        indices.position(0);
        verts.put(vertices, 0, numVerts * 5);
        indices.put(indexes, 0, numIndices);
        verts.position(0);
        indices.position(0);
    }
}

1 个答案:

答案 0 :(得分:0)

想通了,指数指向vertBB而不是indBB,导致腐败。

    verts = vertsBB.asFloatBuffer();
    indices = vertsBB.asShortBuffer();