-1

I am having trouble getting the lighting to work on just the decline portion of my ground. Below is my code for the ground and the decline (making it a ditch):

static void ground(double x, double y, double z, double dx, double dy, 
double dz){
 float white[] = {1,1,1,1};
 float Emission[]  = {0.0,0.0,0.01*emission,1.0};
 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
 glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,Emission);
 //  Save transformation
 glPushMatrix();
 //  Offset, scale and rotate
 glTranslated(x,y,z);
 glScaled(dx, dy, dz);

 glEnable(GL_TEXTURE_2D);
 glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(0.5, 1.0, 0.5);
 glNormal3f(0,0,0);
 glTexCoord2f(0.0,  0.0); glVertex3f(-100, 0, -300);
 glTexCoord2f(300.0,0.0); glVertex3f(-100,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(-3,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(-3,0,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(1.0, 1.0, 0.5);
 glNormal3f(0,0,-1);
 glTexCoord2f(0.0,  0.0); glVertex3f(-2.99,0,-300);
 glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
 glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(1.0, 1.0, 0.5);
 glNormal3f(0,0,1);
 glTexCoord2f(0.0,  0.0); glVertex3f(0.99,-1,-300);
 glTexCoord2f(300.0,0.0); glVertex3f(0.99,-1,300);
 glTexCoord2f(300.0,300.0); glVertex3f(2.99,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(2.99,0,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(0.5, 1.0, 0.5);
 glNormal3f(0,0,0);
 glTexCoord2f(0.0,  0.0); glVertex3f(2.99, 0, -300);
 glTexCoord2f(300.0,0.0); glVertex3f(2.99,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(100,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(100,0,-300);
 glEnd();

 glPopMatrix();

 glDisable(GL_TEXTURE_2D);

}

So the code the doesn't have lighting working correctly is the middle two snippets of "glBegin(GL_QUADS)"

Ryan Fasching
  • 449
  • 2
  • 11
  • 21
  • 1
    @BenSteffan It is for a class project so I have been using what I was told to use. – Ryan Fasching Jun 26 '17 at 17:52
  • Edit in a [mcve]. – genpfault Jun 26 '17 at 18:15
  • 1
    @RyanFasching Were you specifically told to use this version of OpenGL? Ben isn't saying that OpenGL is outdated, he's saying that this code, specifically, is for an old version of OpenGL. Use the most recent version, unless you've been explicitly told to use a certain version. Also, please [edit] your question to include a [mcve], as well as an actual problem description -- "I can't make it work" is useless. What do you want it to do? What is it doing instead? In this case, screenshots might be helpful. – Nic Jun 26 '17 at 19:01

1 Answers1

1

This is just an instance of GIGO (garbage in, garbage out):

glNormal3f(0,0,0);

Nope. That is not a valid normal vector, and will totally break any lighting calculation.

The next one

glNormal3f(0,0,-1);
glTexCoord2f(0.0,  0.0); glVertex3f(-2.99,0,-300);
glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);

is at least some non-null vector, but it isn't normal to the face you are describing, so the lighting will be just wrong.

derhass
  • 43,833
  • 2
  • 57
  • 78