0

I have an array of some xyz vertices that I am wanting to have rendered to form faces for a 3d shape. This array will eventually be based on input data, but for now I am using an example array.

Currently I am drawing the shape by indexing three vertices at a time to make a series of triangles, but I'm really hoping there is a way (by maybe using a loop or something) to have the adjacent vertices identified and automatically form the triangles or a triangle strip.

Here is my code so far: Any help on how to implement this in place of the "indexing three vertices at a time" would be really appreciated.

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);
}

}

  • Sorry, I should have been clearer. I want to be able to render only the vertices that are adjacent, not triangles that are adjacent. So render all vertices, but with the rule that they must be adjacent. Sorry if what I'm asking seems whacked, I'm new to this. – t alastar Feb 28 '18 at 08:28
  • That sounds about right. Similar to what is happening in here but not rendering a plane: https://stackoverflow.com/questions/5915753/generate-a-plane-with-triangle-strips – t alastar Feb 28 '18 at 09:22

0 Answers0