1
votes

I have this mesh class, which has 3 ArrayLists: vertices, edges, faces, but I can't find a way to draw them in a correct way. I want the vertices and the edges on the back not to be visible through the faces in front of my camera. Like if I have a cube I want the vertices, edges and faces that are facing the camera to be visible, the rest should be not.

I don't know if this is clear enough and I'm just doing this as an exercise to learn more about OpenGL ES.

If there's another way to deal with a meshes using vertex, edge and a face as classes please help. Thanks.

Here is my mesh class code:

public class Mesh {

protected ArrayList<Vertex> vertices;
protected ArrayList<Edge> edges;
protected ArrayList<Triangle> triangles;

public Vector Pos;
public float rx,ry,rz;
public float r,g,b,alfa;
private boolean colorSet;

private static boolean vertexOn,edgeOn,FaceOn;

Mesh()
{
    Pos=new Vector();
    vertices=new ArrayList<Vertex>();
    edges=new ArrayList<Edge>();
    triangles=new ArrayList<Triangle>();

    r=g=b=0; alfa=1;
    colorSet=false;

    vertexOn=true;
    edgeOn=true;
    FaceOn=true;
}

Mesh(Vector pos)
{
    Pos=pos;
    vertices=new ArrayList<Vertex>();
    edges=new ArrayList<Edge>();
    triangles=new ArrayList<Triangle>();

    r=g=b=0; alfa=1;
    colorSet=false;

    vertexOn=true;
    edgeOn=true;
    FaceOn=true;
}


public void add(Vertex v)
{
    vertices.add(v);
}

public void add(Edge e)
{
    edges.add(e);
}

public void add(Triangle t)
{
    triangles.add(t);
}

public static void setOn(boolean v,boolean e,boolean f)
{
    vertexOn=v; edgeOn=e; FaceOn=f;
}

public void setColor(float r,float g,float b,float alfa)
{
    colorSet=true;
    this.r=r; this.g=g; this.b=b; this.alfa=alfa;
}

public ArrayList<Vertex> getVertices()
{
    return vertices;
}

public ArrayList<Edge> getEdges()
{
    return edges;
}

public ArrayList<Triangle> getTriangls()
{
    return triangles;
}

public void draw(GL10 gl)
{
    gl.glPushMatrix();
    gl.glTranslatef(Pos.x,Pos.y,Pos.z);
    gl.glRotatef(rx,1,0,0);
    gl.glRotatef(ry,0,1,0);
    gl.glRotatef(rz,0,0,1);

    if(colorSet)
    {
        if(vertexOn)
            for(Vertex v: vertices)
            {
                v.setcolor(r,g,b,alfa);
                v.draw(gl);
            }

        if(FaceOn)
            for(Triangle t: triangles)
            {
                t.setcolor(r,g,b,alfa);
                t.draw(gl);
            }


        if(edgeOn)
            for(Edge e: edges)
            {
                e.setcolor(r,b,g,alfa);
                e.draw(gl);
            }
    }//if a single color is set for the whole mesh with edges and vertices
    else
    {
        if(vertexOn)
            for(Vertex v: vertices)
            {
                v.draw(gl);
            }

        if(FaceOn)
            for(Triangle t: triangles)
            {
                t.draw(gl);
            }

        if(edgeOn)
            for(Edge e: edges)
            {
                e.draw(gl);
            }
    }//if no color for the whole mesh has been set

    gl.glPopMatrix();
}//Draw

}//class
1

1 Answers

1
votes

I am not sure I understand the issue but generally when drawing shapes in 3D you should use a depth buffer. You need to enable it and set some appropriate function on it (just search the web a bit on it, it is pretty simple to use). The depth buffer will assure that the pixels in the back will not be drawn over those in front.

Another thing you should look into is face culling. This is just something you can enable and set the order to clock-wise or counter-clock-wise and works on triangles (or other possible surfaces). What it does is it will simply ignore the surface if the on-screen projection of the surface is ordered differently then then the order you set to cull. This is actually used as an optimization but in case of a single cube it can also be a solution. Just make sure you have a consistent order when generating the shapes.

In practice you would most likely want to use both. The depth buffer to draw correctly and the culling to gain performance.

As for dealing with meshes and classes there are many ways. Usually what is done is the parts of the mesh are grouped together by common drawing. Due to increased performance you would want to reduce the draw calls (number of glDraw calls). So in your case you would draw all the triangles with a single draw call giving all the vertex data at once and a number of triangles. To make it a bit more interesting you could then actually create a vertex buffer on the GPU holding all your vertex data and keep reusing that one to reduce the data traffic between the CPU and the GPU. You could then use a single buffer for the mesh which has all the data to draw and your different parts would only hold the positions (offsets) of different parts in the buffer. For instance if your cube consists of 36 vertices and your mesh consists of 2 cubes then then the first cube would have the information to start at index 0 and draw 12 triangles while the second would would start at 36*sizeof(Vertex) and again draw 12 triangles.