Android OpenGL:为什么我错过了2个三角形?

时间:2012-04-23 07:40:00

标签: android opengl-es

我正在学习OpenGL(在Android上),在我的代码中我指定了4个形成四面体的三角形,但由于某种原因,其中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);
    }
}

1 个答案:

答案 0 :(得分:4)

您可以尝试禁用脸部剔除,以确保您没有向后反面:

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

如果这样可以解决问题,请反转有问题三角形的缠绕(切换点1和3),然后恢复面部剔除。