1
我一直致力於在Android的OpenGL ES 2.0中使用Triangle Strips渲染球體。當球體旋轉時,我遇到了一個問題,它本身似乎是重疊的。OpenGL ES 2球體渲染
我的用於創建頂點一覽表代碼是
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();
渲染中涉及很多不同的部分。然而,我認爲問題出現在頂點世代。