5
\$\begingroup\$

Good day,

I am using LibGDX to try to render a mesh for a hexagon board. I can render the triangles, which is great, but I am having an issue rendering all of the line segments for the mesh and I can't figure out why.


Pictures

Here is an image of what it renders with the primitive type set to GL_TRIANGLES. The colours are just to help me troubleshoot/identify which triangles are where. triangles .

Here is an image of what it renders with the primitive type set to GL_LINES. Whether there is 1 triangle being rendered or 1 million, the horizontal lines do not get rendered. lines .


Code

How I create the mesh:

mesh = new Mesh(true, MAX_VERTS, 0, 
                new VertexAttribute(Usage.Position, POSITION_COMPONENTS, "a_position"),
                new VertexAttribute(Usage.ColorUnpacked, COLOR_COMPONENTS, "a_color"));

mesh.setVertices(verts);

renderable = new Renderable();
renderable.mesh = mesh;
renderable.primitiveType = GL20.GL_TRIANGLES;
renderable.meshPartSize = mesh.getNumVertices();
renderable.meshPartOffset = 0;

When I switch the renderable.primitiveType to GL20.GL_LINES I get the 2nd picture from above.

I've tried to set the renderable.meshPartSize a couple of different ways:

renderable.primitiveType = GL20.GL_LINES;

renderable.meshPartSize = mesh.getNumVertices(); // Doesn't work
renderable.meshPartSize = MAX_VERTS * 2;         // Doesn't work

Here is how I make a triangle:

void drawTriangle(float x, float y, float z, float width, float height, Color color) {  
    // Counter-clockwise winding

    // Vertex 1: Top Left
    verts[idx++] = x;
    verts[idx++] = y;
    verts[idx++] = z;
    verts[idx++] = color.r;
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    // Vertex 2: Bottom Left
    verts[idx++] = x;
    verts[idx++] = y;
    verts[idx++] = z + height;
    verts[idx++] = color.r;
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;

    // Vertex 3: Top Right      
    verts[idx++] = x + width;
    verts[idx++] = y;
    verts[idx++] = z;
    verts[idx++] = color.r;
    verts[idx++] = color.g;
    verts[idx++] = color.b;
    verts[idx++] = color.a;
}

Any help would be appreciated.

Thanks.

\$\endgroup\$
4
  • \$\begingroup\$ You are aware that GL_LINES takes two vertices at a time, so for every two triangles (a, b, c) and (d, e, f), it'll render three lines (a, b), (c, d) and (e, f)? \$\endgroup\$ – user253751 Sep 6 '15 at 6:48
  • \$\begingroup\$ Oh, I didn't realize it wouldn't render line (c, a) or (f, d). Is there an alternative to use instead so that I can get all 5 lines to appear rather than 3? \$\endgroup\$ – lycosis Sep 6 '15 at 11:33
  • \$\begingroup\$ Well if you want to draw 3 lines per triangle, then you need to send 6 vertices (3 will be duplicates of the other 3). Alternatively, draw the triangles but with glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) set, for wireframe rendering. \$\endgroup\$ – user253751 Sep 6 '15 at 11:35
  • \$\begingroup\$ If you don't mind please post an answer cause I would like to see how you made this work. \$\endgroup\$ – dimitris93 Sep 6 '15 at 23:27
1
\$\begingroup\$

Using GL_LINE_STRIP worked for me, you could give that a try if you have not solved this yet.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.