我已经被困了一段时间,弄清楚为什么我无法从高度图图像中生成具有法线的纹理网格。我使用的是TerrainTest (http://libgdx.googlecode.com/svn/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/TerrainTest.java)代码,只添加了生成法线和texcods的代码:
private void calcNormals(short[] indices, float[] verts) {
Vector3 point1=new Vector3(), point2=new Vector3(), point3=new Vector3();
for (int i = 0; i < indices.length; i += 3) {
int i1 = getPositionStart(indices[i + 0]);
int i2 = getPositionStart(indices[i + 1]);
int i3 = getPositionStart(indices[i + 2]);
point1.set(verts[i1 + 0], verts[i1 + 1], verts[i1 + 2]);
point2.set(verts[i2 + 0], verts[i2 + 1], verts[i2 + 2]);
point3.set(verts[i3 + 0], verts[i3 + 1], verts[i3 + 2]);
Vector3 v1 = new Vector3().set(point2).sub(point1);
Vector3 v2 = new Vector3().set(point3).sub(point1);
Vector3 nor = v1.crs(v2).nor();
addNormal(indices[i + 0], verts, nor.x, nor.y, nor.z);
addNormal(indices[i + 1], verts, nor.x, nor.y, nor.z);
addNormal(indices[i + 2], verts, nor.x, nor.y, nor.z);
}
}
纹理坐标我添加在修改buildVertices()方法:
public void buildVertices() {
int heightPitch = height + 1;
int widthPitch = width + 1;
int idx = 0;
int hIdx = 0;
Gdx.app.log("DBG","height map size="+heightMap.length);
for (int z = 0; z < heightPitch; z++) {
for (int x = 0; x < widthPitch; x++) {
// POSITION
vertices[idx++] = x;
vertices[idx++] = heightMap[hIdx++] * strength;
vertices[idx++] = z;
// NORMAL, skip these for now
idx += 3;
// COLOR
vertices[idx++] = Color.WHITE.toFloatBits();
// TEXTURE
vertices[idx++] = (x / (float) width);
vertices[idx++] = (z / (float) height);
}
}
}
这是我在TerrainTest中修改的唯一代码。TerrainChunk类。
然后创建一个网格:
int vertexSize = 3 + 3 + 1 + 2;
TerrainChunk chunk = new TerrainChunk(4, 4, vertexSize, "textures/heightmap.png", 10);
groundMesh = new Mesh(true, chunk.vertices.length / 3, chunk.indices.length,
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE),
new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
groundMesh.setVertices(chunk.vertices);
groundMesh.setIndices(chunk.indices);
之后,我只创建一个btBvhTriangleMeshShape和一个模型从网格:
Material material = new Material(ColorAttribute.createDiffuse(Color.WHITE), ColorAttribute.createSpecular(Color.WHITE),
FloatAttribute.createShininess(16f));
Model result = new Model();
int chunkNum = 0;
MeshPart meshPart = new MeshPart();
meshPart.id = "terrainChunk" + chunkNum;
meshPart.indexOffset = 0;
meshPart.numVertices = groundMesh.getNumIndices();
meshPart.primitiveType = GL20.GL_TRIANGLES;
meshPart.mesh = groundMesh;
btTriangleIndexVertexArray bttiva = new btTriangleIndexVertexArray(meshPart);
btBvhTriangleMeshShape heightFieldShape = new btBvhTriangleMeshShape(bttiva, true);
NodePart nodePart = new NodePart();
nodePart.material = material;
nodePart.meshPart = meshPart;
Node node = new Node();
node.id = "terrainNode" + chunkNum;
node.parts.add(nodePart);
result.meshes.add(groundMesh);
result.materials.add(material);
result.nodes.add(node);
result.meshParts.add(meshPart);
result.manageDisposable(groundMesh);
我用DebugDrawModes渲染Mesh和btShape。DBG_DrawNormals | DebugDrawModes。所以我可以看到btShape的线和法线。
无论我做什么,网格和刚体的法线都指向向下(-y),网格的纹理在其底部以均匀的颜色绘制(如纹理的平均颜色)。奇怪的是,无论我是否改变法线计算(即使用-normal。Y而不是+normal.y), debugdraw的法线不会改变!只有从下面才能看到网眼。我已经在groundMesh.getVertices()中打印了所有数据,一切对我来说都是正确的。下面是为每个顶点打印的9个组件(用于测试的2x2 TerrainChunk):
DBG: Vertex: 0.0,9.803922,0.0,0.07673953,0.9784201,0.19184695,-1.7014117E38,0.0,0.0
DBG: Vertex: 1.0,9.725491,0.0,0.15213917,0.96989024,0.19017535,-1.7014117E38,0.5,0.0
DBG: Vertex: 2.0,9.6470585,0.0,0.07541871,0.96157986,0.26396275,-1.7014117E38,1.0,0.0
DBG: Vertex: 0.0,9.607843,1.0,0.11272618,0.9581794,0.2630302,-1.7014117E38,0.0,0.5
DBG: Vertex: 1.0,9.529411,1.0,0.21905276,0.93097335,0.29206917,-1.7014117E38,0.5,0.5
DBG: Vertex: 2.0,9.372549,1.0,0.14450188,0.9212023,0.36125556,-1.7014117E38,1.0,0.5
DBG: Vertex: 0.0,9.333333,2.0,0.11272618,0.9581794,0.2630302,-1.7014117E38,0.0,1.0
DBG: Vertex: 1.0,9.215687,2.0,0.21905276,0.93097335,0.29206917,-1.7014117E38,0.5,1.0
DBG: Vertex: 2.0,8.980392,2.0,0.21905276,0.93097335,0.29206917,-1.7014117E38,1.0,1.0
法线(索引3,4和5)都指向上!但是debugdraw的法线是向下的,网格只能从下面看到。
如果我改变法线的计算方式,所以它产生了一些其他的值,我可以看到这证实当我打印顶点的内容,但debugdraw和mesh保持不变。这是怎么可能的,就像网格完全忽略了正常值。
问题
这似乎是一个问题在渲染管道的某个地方。背面剔除的法线似乎不是由提供的法线决定的,而是由三角形的缠绕决定的(由顶点顺序引起)。
解决方案
一个解决方法是,在构建网格时改变提供索引的顺序。
所以,例如在LibGDX TerrainTest(你基于你的代码)索引创建在buildIndices方法:
indices[idx++] = i1;
indices[idx++] = i2;
indices[idx++] = i3;
indices[idx++] = i3;
indices[idx++] = i4;
indices[idx++] = i1;
更改每个三角形的最后两个下标的顺序,如下所示:
indices[idx++] = i1;
indices[idx++] = i3; //i3 is exchanged
indices[idx++] = i2; //with i2
indices[idx++] = i3;
indices[idx++] = i1; //i1 is exchanged
indices[idx++] = i4; //with i4
这将改变三角形的"缠绕",现在它应该为你正确渲染。
解决方案解决方案将在LibGDX错误跟踪器中提交一个错误,以便他们得到修复,因为我不认为这是预期的行为。
(PS:我希望这还不算太晚,但由于这让我很头疼,我想也许它可以帮助一些人。)