【发布时间】:2015-07-13 18:45:50
【问题描述】:
我一直致力于在适用于 Android 的 OpenGL ES 2.0 中使用 Triangle Strips 渲染球体。我遇到了一个问题,当球体旋转时,它似乎与自身重叠。
我创建顶点列表的代码是
private static final int FLOATS_PER_VERTEX = 5;
private final float[] vertexData;
private final List<DrawCommand> drawList = new ArrayList<>();
private int offset = 0;
private ObjectBuilder(int sizeInVertices)
{
vertexData = new float[sizeInVertices * FLOATS_PER_VERTEX];
}
private void appendSphere(double radius, int depth)
{
double x, y, z, h, altitude, azimuth;
// Ensure that the depth is between 1 and MAX_DEPTH
int clampDepth = Math.max(1, Math.min(5, depth));
// Calculate the sphere values
int numStrips = (int) Math.pow(2, clampDepth - 1) * 5;
final int numVerticesPerStrip = (int) Math.pow(2, clampDepth) * 3;
double altitudeStepAngle = ONE_TWENTY_DEGREES / Math.pow(2, clampDepth);
double azimuthStepAngle = THREE_SIXTY_DEGREES / numStrips;
// Loop through each strip
for (int i = 0; i < numStrips; i++)
{
final int startVertex = offset / FLOATS_PER_VERTEX;
// Calculate the position of the first vertex in the strip
altitude = NINETY_DEGREES;
azimuth = i * azimuthStepAngle;
// Draw the rest of the strip
for (int j = 0; j < numVerticesPerStrip; j += 2)
{
// First point - Vertex.
y = radius * Math.sin(altitude);
h = radius * Math.cos(altitude);
z = h * Math.sin(azimuth);
x = h * Math.cos(azimuth);
vertexData[offset++] = (float) x;
vertexData[offset++] = (float) y;
vertexData[offset++] = (float) z;
// First point - Texture.
vertexData[offset++] = (float) (1 - azimuth / THREE_SIXTY_DEGREES);
vertexData[offset++] = (float) (1 - (altitude + NINETY_DEGREES) / ONE_EIGHTY_DEGREES);
// Second point - Vertex.
altitude -= altitudeStepAngle;
azimuth -= azimuthStepAngle / 2.0;
y = radius * Math.sin(altitude);
h = radius * Math.cos(altitude);
z = h * Math.sin(azimuth);
x = h * Math.cos(azimuth);
vertexData[offset++] = (float) x;
vertexData[offset++] = (float) y;
vertexData[offset++] = (float) z;
// Second point - Texture.
vertexData[offset++] = (float) (1 - azimuth / THREE_SIXTY_DEGREES);
vertexData[offset++] = (float) (1 - (altitude + NINETY_DEGREES) / ONE_EIGHTY_DEGREES);
azimuth += azimuthStepAngle;
}
drawList.add(new DrawCommand()
{
@Override
public void draw()
{
glDrawArrays(GL_TRIANGLE_STRIP, startVertex, numVerticesPerStrip);
}
});
}
}
我的渲染代码将多个查看矩阵和透视矩阵相乘,然后这样做:
positionObjectInScene(0f, 0f, 0f);
textureProgram.useProgram();
textureProgram.setUniforms(modelViewProjectionMatrix, texture);
planet.bindData(textureProgram);
glFrontFace(GL_CW);
planet.draw();
很明显,渲染中涉及到很多不同的部分。但是,我认为问题出在顶点生成中。
【问题讨论】: