Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Lighting issues (very basic)

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    6

    Lighting issues (very basic)

    Hello,

    I'm getting started with opengl, and I'm facing issues with the lighting. My problem is that I'm drawing a red shape, but it is rendered white. I'm sure I missing something elementary...

    I'm developing in Java using the JOGL libraries.

    Here is how I setup the lighting in the init method:

    Code :
            gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, new float[]{.5f, .5f, .5f, .5f}, 0);
            gl.glEnable(GL2ES1.GL_LIGHTING);
            gl.glEnable(GL2ES1.GL_LIGHT0);

    and here is how I draw a red triangle:

    Code :
            gl.glBegin(GL.GL_TRIANGLES);
     
            gl.glColor3f(1, 0, 0);
     
            gl.glNormal3f(0f, 0f, 1f);
            gl.glVertex3f(0, 0, -5);
            gl.glVertex3f(1, 0, -5);
            gl.glVertex3f(0, 1, -5);
     
            gl.glEnd();


    Like I said, the triangle is rendered completely white. What am I doing wrong?


    I'm attaching the entire code if it can help.

    Any help would be appreciated.
    Attached Files Attached Files

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    788
    When using lighting the reflection properties of surfaces are described with materials (see glMaterial) and vertex colors are ignored. You can specify that vertex colors be used as a material property (e.g. as diffuse color) with glColorMaterial and the corresponding glEnable(GL_COLOR_MATERIAL).

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    6
    Perfect, thanks!

  4. #4
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    6
    Adding glEnable(GL_COLOR_MATERIAL) has improved things in that the brightness of my triangles now depends on their angle. However, I still have a problem: all triangles appear as if they were drawn at the world's origin. It's a bit difficult to explain, but what I mean is that if two triangles have the same angle compated to the world, but one is much further away from the light source than the other, both triangles are lit exactyl the same, when I would expect the further triangle to be darker, since its angle compared to the light source is smaller.

    Any ideas?

    Here is the relevant code:

    Code :
    public void display(GLAutoDrawable drawable) {
            catchControls();
     
            GL2 gl = drawable.getGL().getGL2();
            gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
            gl.glLoadIdentity();
     
            gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, new float[]{ambientLightIntensity, ambientLightIntensity, ambientLightIntensity, ambientLightIntensity}, 0);
            gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, new float[]{diffuseLightIntensity, diffuseLightIntensity, diffuseLightIntensity, diffuseLightIntensity}, 0);
            gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR, new float[]{0, 0, 0, 0}, 0);
            gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, new float[]{lightPositionX, lightPositionY, lightPositionZ, 0}, 0);
     
            drawWorld(gl);
        }
     
     
        public void init(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();      // get the OpenGL graphics context
            glu = new GLU();                         // get GL Utilities
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
            gl.glClearDepth(1.0f);      // set clear depth value to farthest
            gl.glEnable(GL.GL_DEPTH_TEST); // enables depth testing
            gl.glDepthFunc(GL.GL_LEQUAL);  // the type of depth test to do
            gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // best perspective correction
            gl.glShadeModel(GLLightingFunc.GL_SMOOTH); // blends colors nicely, and smooths out lighting
     
     
            gl.glColorMaterial(GL.GL_FRONT, GL2ES1.GL_DIFFUSE);
            gl.glEnable(GL2ES1.GL_COLOR_MATERIAL);
            gl.glEnable(GL2ES1.GL_LIGHTING);
     
            gl.glEnable(GL2ES1.GL_LIGHT0);
        }

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    788
    You are specifying a directional light source (because last component w==0 for the position). If you want a point light source you need to specify a position (i.e. w!=0) and possibly configure the attenuation factors.

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    6
    Good stuff, thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •