2016-03-19 120 views
1

我有這個網格類,它有3個ArrayLists:頂點,邊,面,但我找不到一種方法來以正確的方式繪製它們。我希望背部的頂點和邊緣不能通過相機前面的臉部看到。就像我擁有一個立方體一樣,我希望頂點,面對相機的邊和麪都是可見的,其他的不應該是。繪製多個對象的Opengl es

我不知道這是否足夠清晰,我只是在做這個練習來了解更多關於OpenGL ES的知識。

如果還有另一種方法來處理網格使用頂點,邊和臉作爲類請幫助。謝謝。

這裏是我的目類代碼:

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

我不知道我理解的問題,但一般的3D繪製圖形時,你應該使用一個深度緩衝。你需要啓用它並在其上設置一些適當的功能(只需在網上搜索一下就可以了,這很簡單)。深度緩衝區將確保後面的像素不會覆蓋在前面的像素。

你應該考慮的另一件事是面部撲殺。這只是您可以啓用的順序,並將順序設置爲順時針或逆時針方向並適用於三角形(或其他可能的曲面)。它所做的是,如果屏幕上的投影按不同的順序排列,那麼它會簡單地忽略曲面,然後是您設置的剔除順序。這實際上用作優化,但在單個立方體的情況下,它也可以是解決方案。只需確保在生成形狀時有一致的順序。

在實踐中,你很可能會同時使用兩者。正確繪製深度緩衝區並進行選擇以獲得性能。

至於處理網格和類有很多方法。通常所做的是將網格的各部分按照通用繪圖分組在一起。由於性能提高,您希望減少平局呼叫(電話號碼爲glDraw)。所以在你的情況下,你可以用一次繪製所有的三角形來繪製所有的頂點數據和一些三角形。爲了讓它更有趣一些,你可以在GPU上創建一個頂點緩衝區來保存所有的頂點數據,並不斷重複使用它來減少CPU和GPU之間的數據流量。然後,您可以使用單個緩衝區來繪製所有要繪製的數據的網格,並且您的不同部分只能保存緩衝區中不同部分的位置(偏移量)。例如,如果您的立方體由36個頂點組成,並且您的網格由2個立方體組成,那麼第一個立方體將具有從索引0開始並繪製12個三角形的信息,而第二個將從36*sizeof(Vertex)開始並再次繪製12個三角形。