2012-04-23 7 views
0

私はAndroidでOpenGLを学んでいますが、私のコードでは四面体を形成する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

+0

ありがとう。私は三角形がすべて同じ方向に巻かなければならないことを知らなかった。 –

+0

それ自体ではありませんが、既定のカリングプログラムを有効にしている場合は、頂点の描画順を見て「後ろ」か「前」かを判断することによって、どの三角形を削除するかを決定します。 – Jared

関連する問題