OpenGL coloring AND lighting at the same time?

Problems with Canvas, OpenGL, etc...

OpenGL coloring AND lighting at the same time?

Postby kazasrinivas3 » Wed Apr 13, 2011 1:38 am

I'd like to have lighting on a cube, but have some faces have different colors. The following code results in a cube with no lighting, but one black face:

Code: Select all
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;
    }

Uncommenting glEnable(GL10.GL_COLOR_MATERIAL) results in a lighted cube with no black face. How would I get a lighted cube with one black face?
kazasrinivas3
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 13, 2011 1:32 am

Top

Re: OpenGL coloring AND lighting at the same time?

Postby zorro » Thu May 05, 2011 9:52 am

Using glEnable(GL10.GL_COLOR_MATERIAL) disables the lighting effects by default. You cannot have them both, because the lighting calculations are still colors in the end. I think the easiest way is to use textures on you cube faces. Use small textures (2x2 pixels) and you will have the same effect as using the colors. The overhead for texturing will be minimal. Textures and lighting can be mixed up and you will obtain the desired effect.
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests