我有一些 xyz 顶点的数组,我想要渲染这些顶点以形成 3d 形状的面。该数组最终将基于输入数据,但现在我使用的是示例数组。
目前我通过一次索引三个顶点来绘制形状以制作一系列三角形,但我真的希望有一种方法(可能通过使用循环或其他方式)来识别相邻顶点并自动形成三角形或三角带。
到目前为止,这是我的代码:任何关于如何实现它来代替“一次索引三个顶点”的帮助将不胜感激。
public class ShapeDraw{
private FloatBuffer vertexBuffer; // Buffer for vertex-array
private FloatBuffer colorBuffer; // Buffer for color-array
private ByteBuffer indexBuffer; // Buffer for index-array
private float[] vertices = { // 9 vertices of the shape in (y,z,x)
0.0f, 0.37150f, 0.5f, // 1
0.35f, 0.3715f, 0.35f, // 2
0.50f, 0.37150f, 0.0f, // 3
0.35f, 0.3715f, -0.35f, // 4
0.0f, 0.3710f, -0.5f, // 5
-0.35f, 0.3715f, -0.35f, // 6
-0.50f, 0.3715f, 0.f, // 7
0.0f, -0.22f, 0.0f, // 0
-0.35f, 0.3715f, 0.35f // 8
};
private float[] colors = { // Colors of the 9 vertices in RGBA
0.0f, 0.0f, 1.0f, 1.0f, // 0. blue
0.0f, 1.0f, 0.0f, 1.0f, // 1. green
0.0f, 0.0f, 1.0f, 1.0f, // 2. blue
0.0f, 1.0f, 0.0f, 1.0f, // 3. green
0.0f, 0.0f, 1.0f, 1.0f, // 4. blue
0.0f, 1.0f, 0.0f, 1.0f, // 5. green
0.0f, 0.0f, 1.0f, 1.0f, // 6. blue
0.0f, 1.0f, 0.0f, 1.0f, // 7. green
1.0f, 0.0f, 0.0f, 1.0f // 8. red
};
private byte[] indices = { // Vertex indices of the 8 Triangles
0, 7, 1,
1, 7, 2,
2, 7, 3,
3, 7, 4,
4, 7, 5,
5, 7, 6,
6, 7, 8,
8, 7, 0,
0, 4, 2,
0, 4, 6
};
// Constructor - Set up the buffers
public ShapeDraw() {
// Setup vertex-array buffer. Vertices in float. An float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind
// Setup color-array buffer. Colors in float. An float has 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
// Setup index-array buffer. Indices in byte.
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
// Draw the shape
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
// Enable arrays and define their buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE,
indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}