gpt4 book ai didi

java - FloatBuffer 损坏问题

转载 作者:行者123 更新时间:2023-11-29 09:03:40 24 4
gpt4 key购买 nike

我正在编写一个基于 DooM map 布局的 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 数组(存储垂直和 tex 坐标/id)并按纹理 id 对其进行快速排序。然后运行以下函数。

我将问题缩小为前 3 个 put 的损坏问题。前三个是一些奇怪的浮点值,然后其余的都是正常的。

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

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);
}
}

最佳答案

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

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

关于java - FloatBuffer 损坏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16123536/

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