2012-04-23 113 views
0

我學習OpenGL(在Android上),在我的代碼中,我指定了4個三角形,形成了一個四面體,但由於某種原因,其中2個邊似乎缺失,我不明白爲什麼。我幾乎以任何我能想到的方式調整了它,但仍然沒有運氣。Android OpenGL:爲什麼我缺少2個三角形?

這是我的代碼,請告訴我,如果我做錯了什麼。

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

import javax.microedition.khronos.opengles.GL10; 

public class Tetrahedron { 

    private IntBuffer ver; 
    private IntBuffer nor; 
    private ByteBuffer ind; 
    private float[] mat_ambient; 
    private float[] mat_diffuse; 
    private float[] mat_specular; 
    private float[] mat_emission; 

    public Tetrahedron() { 

     int one = toInt(1.0f); 
     int vertices[] = { 
       //face 1 
       -one, -one, one, //point 1 
        0 , one, 0 , //point 2 
       one, -one, one, //point 3 

       //face 2 
        0 , one, 0 , //point 2 
       one, -one, one, //point 3 
        0 , 0 , -one, //point 4 

       //face 3 
       one, -one, one, //point 3 
        0 , 0 , -one, //point 4 
       -one, -one, one, //point 1 

       //face 4 
        0 , 0 , -one, //point 4 
       -one, -one, one, //point 1 
        0 , one, 0 //point 2 

     }; 
     int normals[] = { 
       -one, -one, one, 
        0 , one, one, 
       one, -one, one, 

       one, one, -one, 
       one, 0 , 0 , 
       one, one, -one, 

       one, -one, one, 
        0 , -one, -one, 
       -one, -one, one, 

       -one, one, -one, 
       -one, 0 , 0 , 
       -one, one, -one 
     }; 

     byte indices[] = { 
       0, 1, 2, 
       3, 4, 5, 
       6, 7, 8, 
       9,10,11 

     }; 

     ver=getIntBuffer(vertices); 
     nor=getIntBuffer(normals); 
     ind=getByteBuffer(indices); 

     mat_ambient = new float[4]; 
     mat_ambient[0] = 0.3f; 
     mat_ambient[1] = 0.3f; 
     mat_ambient[2] = 0.3f; 
     mat_ambient[3] = 1.0f; 

     mat_diffuse = new float[4]; 
     mat_diffuse[0] = 0.9f; 
     mat_diffuse[1] = 0.9f; 
     mat_diffuse[2] = 0.9f; 
     mat_diffuse[3] = 1.0f; 

     mat_specular = new float[4]; 
     mat_specular[0] = 0.1f; 
     mat_specular[1] = 0.1f; 
     mat_specular[2] = 0.1f; 
     mat_specular[3] = 1.0f; 

     mat_emission = new float[4]; 
     mat_emission[0] = 0.0f; 
     mat_emission[1] = 1.0f; 
     mat_emission[2] = 0.0f; 
     mat_emission[3] = 1.0f; 

    } 
    private IntBuffer getIntBuffer(int[] data){ 
     ByteBuffer byteBuf = ByteBuffer.allocateDirect(data.length * 4); 
     byteBuf.order(ByteOrder.nativeOrder()); 
     IntBuffer buf = byteBuf.asIntBuffer(); 
     buf.put(data); 
     buf.position(0); 
     return buf; 
    } 
    private ByteBuffer getByteBuffer(byte[] data){ 
     ByteBuffer buf = ByteBuffer.allocateDirect(data.length); 
     buf.put(data); 
     buf.position(0); 
     return buf; 
    } 
    private int toInt(float num){ 
     return (int)(num*65536); 
    } 

    public void draw(GL10 gl){ 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); 


     gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient, 0); 
     gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse, 0); 
     gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mat_specular, 0); 
     gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, mat_emission, 0); 
     gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, 56); 

     gl.glFrontFace(GL10.GL_CW); 
     gl.glVertexPointer(3, GL10.GL_FIXED, 0, ver); 
     gl.glNormalPointer(GL10.GL_FIXED, 0, nor); 
     gl.glDrawElements(GL10.GL_TRIANGLES, ind.capacity(), GL10.GL_UNSIGNED_BYTE, ind); 

     gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    } 
} 

回答

4

你可以嘗試禁用臉撲殺,只是爲了確保你沒有這些面孔倒退:

gl.glDisable(GL10.GL_CULL_FACE); 
//You should also comment out this: gl.glFrontFace(GL10.GL_CW); 

如果這能解決問題,扭轉問題的三角形的繞組(開關點1和3),然後恢復臉部剔除。

+0

我認爲你可能是正確的面對1和2的纏繞順序彼此相反... – Goz 2012-04-23 07:57:56

+0

謝謝。我不知道三角形都必須朝同一個方向纏繞。 – 2012-04-23 08:55:20

+0

它們本身並不是如此,但是如果啓用了默認剔除程序,則通過查看頂點的繪製順序來確定要剔除哪些三角形以確定它們是面向「後退」還是「前沿」。 – Jared 2012-04-24 03:56:42