0

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

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) 会导致没有黑色面的发光立方体。我怎样才能得到一个黑色的发光立方体?

4

1 回答 1

0

我想在这里你会找到答案。Android Emulator 和 HTC 手机(我不了解其他手机,我没有在它们上测试过应用程序)都有照明错误。

于 2011-05-09T13:10:31.847 回答