我正在编写基于 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 对其进行快速排序。然后运行以下函数。
我将问题缩小到前 3 个看跌期权的腐败问题。前三个是一些奇怪的浮点值,然后其余的都是正常的。
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);
}
}