Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My ground is not lighting up

Tags:

opengl

Im a bit stuck with my lighting. My light can light up meshes but it does not light up my ground.

Here is some of my code

The light:

glEnable(GL_LIGHT0);

glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
glEnable(GL_COLOR_MATERIAL);


GLfloat ambient_light0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position_light0[] = { 0.0f, 8.0f, 0.0f, 0.9f };


GLfloat spotDirection_light0[] = {0.0, -1.0, 0.0 };
GLfloat specularLightcolor[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, position_light0 );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection_light0 );
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);

glLightfv( GL_LIGHT0, GL_AMBIENT, ambient_light0 );
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLightcolor);

glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f );
glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 50.0f );

The ground:

void drawGround(int id) {

glEnable(GL_TEXTURE_2D);

glEnable(GL_COLOR_MATERIAL);



getTexture(id);

glBindTexture(GL_TEXTURE_2D, id); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



GLfloat material_diffuse[] = {0.9, 0.9, 0.9, 1.0 };

    GLfloat material_specular[] = { 1, 1, 1, 1 };



glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);

glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);


glBegin(GL_QUADS); 



for(int i = 0; i<3; i++){

    for(int j= 0; j<3; j++){

    glNormal3f(0.0f, 1.0f, 0.0f);

    glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);

        glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);

        glMaterialf(GL_FRONT, GL_SHININESS, 80.0);



    glTexCoord2f(1, 1);

    glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));

    glTexCoord2f(1, 0);     

    glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));

    glTexCoord2f(0,0);  

    glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));

    glTexCoord2f(0, 1);     

    glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));


    }

}


glDepthMask( 1.0 );

glDisable(GL_TEXTURE_2D);

glEnd();

}

Drawing of the ground happens after the light is called. Has anyone any idea why my ground is not lighting up?

here is a screen shot of my problem ---> Image

like image 452
ettan Avatar asked Nov 01 '11 07:11

ettan


1 Answers

The problem is, that default fixed function OpenGL lighting does the illumination at the vertices only, and then just interpolates the resulting color. Since all the 4 vertices of the ground are dimly lit, the whole quad gets dim.

Solutions:

  • further subdivide your ground, so that there are more light sampling points. So far you've got only 3×3 points, which is not nearly sufficient. Think 100×100 or even more, to get a nice illumination effect.

or

  • use a fragment shader to implement per pixel lighting.

You're running into a typical OpenGL newbie pitfall there, BTW.

Update:

On a side note: You also did not supply normals for the plane. Therefore lighting will not work at all.

like image 200
datenwolf Avatar answered Oct 31 '22 04:10

datenwolf