0

我正在学习 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);
    }
}
4

1 回答 1

4

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

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

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

于 2012-04-23T07:48:15.147 回答