OpenGL ES(Android)多维数据集在添加glEnable(GL_COLOR_MATERIAL)时禁用照明?

时间:2011-04-10 14:19:00

标签: android opengl-es opengl-es-lighting

这可能是一个非常简单的问题,我只是没有得到它。我想在立方体上照明,但有些面孔有不同的颜色。以下代码生成一个没有光照但没有一个黑色面的立方体:

private float[] lightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };
    private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
    private float[] lightPosition = { 0.0f, 0.0f, 3.0f, 1.0f};

    float matAmbient[] = new float[] { 0.3f, 0.3f, 0.3f, 1.0f };
    float matDiffuse[] = new float[] { 0.6f, 0.6f, 0.6f, 1.0f };

    private FloatBuffer vertexBuffer;

    private FloatBuffer normalBuffer;

    private FloatBuffer colorBuffer;

    float vertices[] = new float[] {
            // FRONT
            -0.5f, -0.5f,  0.5f,
             0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
            // BACK
            -0.5f, -0.5f, -0.5f,
            -0.5f,  0.5f, -0.5f,
             0.5f, -0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
            // LEFT
            -0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f,  0.5f, -0.5f,
            // RIGHT
             0.5f, -0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
             0.5f, -0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
            // TOP
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
             -0.5f,  0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
            // BOTTOM
            -0.5f, -0.5f,  0.5f,
            -0.5f, -0.5f, -0.5f,
             0.5f, -0.5f,  0.5f,
             0.5f, -0.5f, -0.5f,
        };

    float colors[] = new float[] {
            // FRONT
            0.0f, 0.0f,  0.0f, 0.0f,
             0.0f, 0.0f,  0.0f, 0.0f,
            0.0f,  0.0f,  0.0f, 0.0f,
             0.0f,  0.0f,  0.0f, 0.0f,
            // BACK
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f,  1.0f, 1.0f, 1.0f,
             1.0f, 1.0f, 1.0f, 1.0f,
             1.0f,  1.0f, 1.0f, 1.0f,
            // LEFT
            1.0f, 1.0f,  1.0f, 1.0f,
            1.0f,  1.0f,  1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f,  1.0f, 1.0f, 1.0f,
            // RIGHT
             1.0f, 1.0f, 1.0f, 1.0f,
             1.0f,  1.0f, 1.0f, 1.0f,
             1.0f, 1.0f,  1.0f, 1.0f,
             1.0f,  1.0f,  1.0f, 1.0f,
            // TOP
            1.0f,  1.0f,  1.0f, 1.0f,
             1.0f,  1.0f,  1.0f, 1.0f,
             1.0f,  1.0f, 1.0f, 1.0f,
             1.0f,  1.0f, 1.0f, 1.0f,
            // BOTTOM
            1.0f, 1.0f,  1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f,
             1.0f, 1.0f,  1.0f, 1.0f,
             1.0f, 1.0f, 1.0f, 1.0f,
        };

    float normals[] = new float[] {
            // FRONT
            0f,  0f,  1f,
            0f,  0f,  1f,
            0f,  0f,  1f,
            0f,  0f,  1f,
            // BACK
            0f,  0f,  -1f,
            0f,  0f,  -1f,
            0f,  0f,  -1f,
            0f,  0f,  -1f,
            // LEFT
            -1f,  0f,  0f,
            -1f,  0f,  0f,
            -1f,  0f,  0f,
            -1f,  0f,  0f,
            // RIGHT
            1f, 0f, 0f,
            1f, 0f, 0f,
            1f, 0f, 0f,
            1f, 0f, 0f,
            // TOP
            0f,  1f, 0f,
            0f,  1f, 0f,
            0f,  1f, 0f,
            0f,  1f, 0f,
            // BOTTOM
            0f,  -1f, 0f,
            0f,  -1f, 0f,
            0f,  -1f, 0f,
            0f,  -1f, 0f
        };
...
            gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_LIGHT0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);

        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);

        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);


        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);

        gl.glEnable(GL10.GL_COLOR_MATERIAL);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClearDepthf(1.0f);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    }

    public GLScreen(Context context) {
        super(context);
        this.setRenderer(this);
        this.requestFocus();

        vertexBuffer = makeFloatBuffer(vertices);
        normalBuffer = makeFloatBuffer(normals);
        colorBuffer = makeFloatBuffer(colors);
    }

    float xrot = 0.0f;
    float yrot = 0.0f;

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0);

        gl.glRotatef(xrot, 1, 0, 0);
        gl.glRotatef(yrot, 0, 1, 0);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);

        xrot += 1.0f;
        yrot += 0.5f;
    }

取消注释glEnable(GL10.GL_COLOR_MATERIAL)会产生一个没有黑色面的发光立方体。我怎么能得到一个黑色脸的灯光立方体?

1 个答案:

答案 0 :(得分:0)

我想here你会找到答案。 Android模拟器和HTC手机(我不知道其他人,我还没有在他们身上测试过应用程序)都有照明漏洞。