首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FloatBuffer损坏问题

FloatBuffer损坏问题
EN

Stack Overflow用户
提问于 2013-04-21 02:04:36
回答 1查看 94关注 0票数 2

我正在编写一个基于DooM地图布局的3D渲染器/引擎,并将其移植到Android上。我的原始算法非常慢,我使用方法ID为他们的iPhone端口做了改进。这是函数:

代码语言:javascript
复制
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树并收集将呈现哪些行。这些被放入墙的数组中(存储vert和tex坐标/ id ),并按纹理id对其快速排序。然后运行以下函数。

在前3个看跌期权中,我将问题缩小为腐败问题。前三个是一些奇怪的浮点值,然后是正常的。

cFrame对象计算顶点/索引的数量,并从数组传输到浮动缓冲区。下面是类/函数

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-24 03:54:09

弄清楚了,索引指向的是vertBB而不是indBB,导致了损坏。

代码语言:javascript
复制
    verts = vertsBB.asFloatBuffer();
    indices = vertsBB.asShortBuffer();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16123536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档