2009-06-29 94 views

回答

1

你需要通過對象加載來完成。您無法使用Open GL ES調用3D形狀圖元。

期待通過傑夫·拉馬什的博客,有很多真正好的資源如何有目標負載。 link text

+0

感謝您的答覆。不過,我被告知,你可以通過使用'quads'在OpenGLES中繪製一個圓柱體。在這一點上我想避免使用3D建模軟件。任何人都可以給出更具體的答案嗎? – RexOnRoids 2009-06-29 06:10:00

1

你確實可以通過計算物體的幾何形狀繪製的OpenGL ES的圓柱體。開源GLUT|ES項目在其glutes_geometry.c源文件中具有用於實體(柱面,球體等)的幾何繪製例程。不幸的是,這些函數使用glBegin()和glEnd()調用,這些調用在OpenGL ES中不存在。

爲OpenGL ES的部分工作缸實施

的代碼可以在論壇主題here被發現。

3

第一步是寫一個繪製三角形的子程序。我會留給你的。然後畫出一系列構成圓柱體形狀的三角形。訣竅是用一個多邊形來近似一個具有64個邊的多邊形的圓。下面是一些關於我的頭頂的僞代碼。

for (i = 0; i < 64; i++) 
{ 
    angle = 360 * i/63; // Or perhaps 2 * PI * i/63 
    cx[i] = sin(angle); 
    cy[i] = cos(angle); 
} 

for (i = 0; i < 63; i++) 
{ 
    v0 = Vertex(cx[i], cy[i], 0); 
    v1 = Vertex(cx[i + 1], cy[i + 1], 0); 
    v2 = Vertex(cx[i], cy[i], 1); 
    v3 = Vertex(cx[i + 1], cy[i + 1], 1); 

    DrawTriangle(v0, v1, v2); 
    DrawTriangle(v1, v3, v2); 
    // If you have it: DrawQuad(v0, v1, v3, v2); 
} 

代碼中幾乎肯定存在一個錯誤。最有可能的是,我已經搞亂了三角形繪製中的纏繞順序,所以最終可能只有一半的三角形明顯可見,或者只有後面可見的三角形很奇怪。

性能很快就會要你畫三角形帶和球迷的效率,但這應該讓你開始。

0

可以通過計算幾何程序上畫筒。最重要的是,您應該使其支持triangle stripping,並且您還需要計算映射座標以及可能的法線。所以從頭開始需要一些思考。

我已經創建了C#進行Unity3D模塊所做的正是這一點,讓你來調整參數。由於幾何計算在任何地方都是一樣的,因此應該可以輕鬆地轉換爲C或C++。觀看video瞭解詳情,並從GitHub下載代碼。

1

我希望這可以幫助你,這是我實現的圓柱體在OpenGLES 2.0的Android

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 

import javax.microedition.khronos.opengles.GL10; 

public class Cylinder { 

    public Cylinder(int n) { 

     this.numOfVertex = n; 

     float[] vertex = new float[3 * (n + 1) * 2]; 
     byte[] baseIndex = new byte[n]; 
     byte[] topIndex = new byte[n]; 
     byte[] edgeIndex = new byte[n*2 + 2]; 

     double perAngle = 2 * Math.PI/n; 

     for (int i = 0; i < n; i++) { 
      double angle = i * perAngle; 
      int offset = 6 * i; 

      vertex[offset + 0] = (float)(Math.cos(angle) * radious) + cx; 
      vertex[offset + 1] = -height; 
      vertex[offset + 2] = (float)(Math.sin(angle) * radious) + cy; 

      vertex[offset + 3] = (float)(Math.cos(angle) * radious) + cx; 
      vertex[offset + 4] = height; 
      vertex[offset + 5] = (float)(Math.sin(angle) * radious) + cy; 

      topIndex[i] = (byte)(2*i); 

      baseIndex[i] = (byte)(2*i +1); 

      edgeIndex[2*i + 1] = baseIndex[i]; 
      edgeIndex[2*i] = topIndex[i]; 

     } 


     edgeIndex[2*n] = topIndex[0]; 
     edgeIndex[2*n+1] = baseIndex[0]; 

     ByteBuffer vbb = ByteBuffer 
       .allocateDirect(vertex.length * 4) 
       .order(ByteOrder.nativeOrder()); 

     mFVertexBuffer = vbb.asFloatBuffer(); 
     mFVertexBuffer.put(vertex); 
     mFVertexBuffer.position(0); 

     normalBuffer = mFVertexBuffer; 

     mCircleBottom = ByteBuffer.allocateDirect(baseIndex.length); 
     mCircleBottom.put(baseIndex); 
     mCircleBottom.position(0); 

     mCircleTop = ByteBuffer.allocateDirect(topIndex.length); 
     mCircleTop.put(topIndex); 
     mCircleTop.position(0); 

     mEdge = ByteBuffer.allocateDirect(edgeIndex.length); 
     mEdge.put(edgeIndex); 
     mEdge.position(0); 
    } 

    public void draw(GL10 gl) 
    { 
     gl.glCullFace(GL10.GL_BACK); 
     gl.glEnable(GL10.GL_CULL_FACE); 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer); 
     gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer); 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

     gl.glPushMatrix(); 

     gl.glColor4f(1f, 0, 0, 0); 
     gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, numOfVertex * 2 + 2, GL10.GL_UNSIGNED_BYTE, mEdge); 
     gl.glPopMatrix(); 
     gl.glPushMatrix(); 

     gl.glColor4f(0.9f, 0, 0, 0); 
     gl.glDrawElements(GL10.GL_TRIANGLE_FAN, numOfVertex, GL10.GL_UNSIGNED_BYTE, mCircleTop); 
     gl.glPopMatrix(); 

     gl.glPushMatrix(); 

     gl.glTranslatef(0, 2*height, 0); 
     gl.glRotatef(-180, 1, 0, 0); 

     gl.glColor4f(0.9f,0, 0, 0); 
     gl.glDrawElements(GL10.GL_TRIANGLE_FAN, numOfVertex , GL10.GL_UNSIGNED_BYTE, mCircleBottom); 
     gl.glPopMatrix(); 

    } 

    private FloatBuffer mFVertexBuffer; 
    private FloatBuffer normalBuffer; 
    private ByteBuffer mCircleBottom; 
    private ByteBuffer mCircleTop; 
    private ByteBuffer mEdge; 
    private int numOfVertex; 
    private int cx = 0; 
    private int cy = 0; 
    private int height = 1; 
    private float radious = 1; 
}