OpenGL, Spotlight not Visible in program

71 Views Asked by At

Trying to model a specific scene in OpenGL, and placing a spot light in a lamp in the scene. I am unable to get lighting to work with the spotlight. I know the issue is with the myinit function, but I am unsure as to which.

While I am aware that there are better methods for what I am attempting to achieve, for this class I need to use this form of lighting.

The Current issue is the scene is completely back whenever lighting is enabled. The only light in the scene that I want is the spot light, but I can not get it to be visible at all. I am not sure if it's an issue with my Material Properties, Normals, or how I define the light itself


#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>

static GLdouble viewer[]= {50.0, 50.0, 20.0}; // initial viewer location
static GLint on = 1;                    //Stores if the light is currently on, 1 is yes, 0 is no
void myinit(void)                           
{   
    
  GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };                
  GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat position[] = {40,57,-10,1};                   
  GLfloat lmodel_ambient[] = { .1, .1, .1, 1.0 };
  GLfloat spotdir[] = {0,-1,0};
  GLfloat local_view[] = { 0.0 };
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);
  GLfloat att_constant = 0.0;
  GLfloat att_linear = 1.0;
  GLfloat att_quadratic = 1.0;

  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, position);
  glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotdir );
  glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 1);
  glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, att_constant);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glClearColor(0.2, 0.2, 0.2, 0.3);
  glEnable(GL_NORMALIZE);
} 




void keys(unsigned char key, int x, int y)
{
  /* Use x, X, y, Y, z, and Z keys to move viewer */            
  if(key == 'x') viewer[0]-= 1.0;
  if(key == 'X') viewer[0]+= 1.0;
  if(key == 'y') viewer[1]-= 1.0;
  if(key == 'Y') viewer[1]+= 1.0;
  if(key == 'z') viewer[2]-= 1.0;
  if(key == 'Z') viewer[2]+= 1.0;
  if(key == 'q' || key == 'Q') exit(0);                     //Quits the Program
  if(key == 'o' || key == 'O') on *= -1;                    //Toggles the Light on or Off
  display();
}
void myReshape(int w, int h)
{
  glViewport(0, 0, w, h);
  /* Use a perspective view */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
             2.0* (GLfloat) h / (GLfloat) w, 2.0, 200.0);
  else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
         2.0* (GLfloat) w / (GLfloat) h, 2.0, 200.0);
  glMatrixMode(GL_MODELVIEW);
}

      

void tabledraw()    //Method for drawing the table
{           //It will have a high shininess propertery, 
  //and will need to experiment with other values to determine others
  glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 70.0); //Sets High Shininess
  glMaterialf(GL_FRONT_AND_BACK,GL_DIFFUSE, 30);
  glColor3f(.75,.38,.1);        //Sets the color to Brown
                    //Sets the normal Vector
  //Legs will be 5x30x5

  glBegin(GL_QUADS);
  //Table Top - A 80x3x40 Rectangle 
  glNormal3f(0,-1,0);
  //Bottom of Top
  glVertex3f(10,35,0);
  glVertex3f(90,35,0);
  glVertex3f(90,35,-40);
  glVertex3f(10,35,-40);
  //Top of Top
  glNormal3f(0,1,0);
  glVertex3f(10,40,0);
  glVertex3f(90,40,0);
  glVertex3f(90,40,-40);
  glVertex3f(10,40,-40);
  //Left of Top
  glNormal3f(-1,0,0);
  glVertex3f(10,40,0);
  glVertex3f(10,35,0);
  glVertex3f(10,35,-40);
  glVertex3f(10,40,-40);
  //Right of Top
  glNormal3f(1,0,0);
  glVertex3f(90,40,0);
  glVertex3f(90,35,0);
  glVertex3f(90,35,-40);
  glVertex3f(90,40,-40);
  //Back of top
  glVertex3f(90,40,-40);
  glVertex3f(90,35,-40);
  glVertex3f(10,35,-40);
  glVertex3f(10,40,-40);
  //Front of Top
        
  glVertex3f(90,40,0);
  glVertex3f(90,35,0);
  glVertex3f(10,35,0);
  glVertex3f(90,40,0);
  //Front Left Leg 5x30x5 Rectangle
  //Bottom
  glVertex3f(10,7,0);
  glVertex3f(15,7,0);
  glVertex3f(15,7,-5);
  glVertex3f(10,7,-5);
        
  //Don't Need a top
  //Left
  glVertex3f(10,7,0);
  glVertex3f(10,37,0);
  glVertex3f(10,37,-5);
  glVertex3f(10,7,-5);

  //Right
  glVertex3f(15,7,0);
  glVertex3f(15,37,0);
  glVertex3f(15,37,-5);
  glVertex3f(15,7,-5);

  //Back
  glVertex3f(15,7,-5);
  glVertex3f(15,37,-5);
  glVertex3f(10,37,-5);
  glVertex3f(10,7,-5);
  //Front
  glVertex3f(15,7,0);
  glVertex3f(15,37,0);
  glVertex3f(10,37,0);
  glVertex3f(10,7,0);

  //Front Right Leg
  //Bottom
  glVertex3f(85,7,0);
  glVertex3f(90,7,0);
  glVertex3f(90,7,-5);
  glVertex3f(85,7,-5);

  //Don't Need a top
  //Left
  glVertex3f(85,7,0);
  glVertex3f(85,37,0);
  glVertex3f(85,37,-5);
  glVertex3f(85,7,-5);

  //Right
  glVertex3f(90,7,0);
  glVertex3f(90,37,0);
  glVertex3f(90,37,-5);
  glVertex3f(90,7,-5);

  //Back
  glVertex3f(90,7,-5);
  glVertex3f(90,37,-5);
  glVertex3f(85,37,-5);
  glVertex3f(85,7,-5);
  //Front
  glVertex3f(90,7,0);
  glVertex3f(90,37,0);
  glVertex3f(85,37,0);
  glVertex3f(85,7,0);

  //Back Left Leg
  //Bottom
  glVertex3f(10,7,-35);
  glVertex3f(15,7,-35);
  glVertex3f(15,7,-40);
  glVertex3f(10,7,-40);

  //Don't Need a top
  //Left
  glVertex3f(10,7,-35);
  glVertex3f(10,37,-35);
  glVertex3f(10,37,-40);
  glVertex3f(10,7,-40);

  //Right
  glVertex3f(15,7,-35);
  glVertex3f(15,37,-35);
  glVertex3f(15,37,-40);
  glVertex3f(15,7,-40);

  //Back
  glVertex3f(15,7,-40);
  glVertex3f(15,37,-40);
  glVertex3f(10,37,-40);
  glVertex3f(10,7,-40);
  //Front
  glVertex3f(15,7,-35);
  glVertex3f(15,37,-35);
  glVertex3f(10,37,-35);
  glVertex3f(10,7,-35);


  //Back Right Leg
  //Bottom
  glVertex3f(85,7,-35);
  glVertex3f(90,7,-35);
  glVertex3f(90,7,-40);
  glVertex3f(85,7,-40);

  //Don't Need a top
  //Left
  glVertex3f(85,7,-35);
  glVertex3f(85,37,-35);
  glVertex3f(85,37,-40);
  glVertex3f(85,7,-40);

  //Right
  glVertex3f(90,7,-35);
  glVertex3f(90,37,-35);
  glVertex3f(90,37,-40);
  glVertex3f(90,7,-40);

  //Back
  glVertex3f(90,7,-40);
  glVertex3f(90,37,-40);
  glVertex3f(85,37,-40);
  glVertex3f(85,7,-40);
  //Front
  glVertex3f(90,7,-35);
  glVertex3f(90,37,-35);
  glVertex3f(85,37,-35);
  glVertex3f(85,7,-35);

  glEnd();

}

void kbmdraw()
{
  //Needs to be a rectangle, silver and dull
  //Bottom will be on the table top (y = 40), and will be 2 points tall. 
      
  glMaterialf(GL_FRONT,GL_SHININESS, .2);
  glColor4f(.5,.5,.5,1); //Sets Color to silver
  glBegin(GL_QUADS);
  //Bottom of Keyboard

  glVertex3f(60,40, -10 );
  glVertex3f(60, 40, -20);
  glVertex3f(40,40,-20);
  glVertex3f(40,40,-10);

  //Top of Keyboard 
  glVertex3f(60,42, -10 );
  glVertex3f(60, 42, -20);
  glVertex3f(40,42,-20);
  glVertex3f(40,42,-10);

  //Left of Keyboard
  glVertex3f(40,42,-10);
  glVertex3f(40,42,-20);
  glVertex3f(40,40,-20);
  glVertex3f(40,40,-10);
      
  //Right of Keyboard
  glVertex3f(60,42,-10);
  glVertex3f(60,42,-20);
  glVertex3f(60,40,-20);
  glVertex3f(60,40,-10);

  //Back of Keyboard
  glVertex3f(60,42,-20);
  glVertex3f(60,40,-20);
  glVertex3f(40,40,-20);
  glVertex3f(40,42,-20);

  //Front of Keyboard
  glVertex3f(60,42,-10);
  glVertex3f(60,40,-10);
  glVertex3f(40,40,-10);
  glVertex3f(40,42,-10);

  //Mouse will be the same, just shiny instead of Dull and white
  //Will be a 5x2x10 square
  glColor4f(1,1,1,1);
  glMaterialf(GL_FRONT, GL_SHININESS,.8); //Sets the Shininess to a high value
  //Bottom of Mouse
  glVertex3f(65,40,-10);
  glVertex3f(65,40,-20);
  glVertex3f(70,40,-20);
  glVertex3f(70,40,-10);
  //Top of Mouse
  glVertex3f(65,42,-10);
  glVertex3f(65,42,-20);
  glVertex3f(70,42,-20);
  glVertex3f(70,42,-10);

  //Left of Mouse
  glVertex3f(65,40,-10);
  glVertex3f(65,40,-20);
  glVertex3f(65,42,-20);
  glVertex3f(65,42,-10);
  //Right of Mouse
  glVertex3f(70,40,-10);
  glVertex3f(70,40,-20);
  glVertex3f(70,42,-20);
  glVertex3f(70,42,-10);
  //Back of Mouse
  glVertex3f(65,40,-20);
  glVertex3f(70,40,-20);
  glVertex3f(70,42,-20);
  glVertex3f(65,42,-20);

  //Front of Mouse
  glVertex3f(65,40,-10);
  glVertex3f(70,40,-10);
  glVertex3f(70,42,-10);
  glVertex3f(65,42,-10);
  glEnd();
}
void aiodraw()
{
  /*Basic shape will be
    __________
    | ______  |
    | |     | |3
    | |__4__| |
    ----------
    |  | 2
    ------
    ------ 1
  */
  //The center Square will be black and shiny, the rest of it will be silver and dull
  //The AIO computer will have four main shapes
  //1. The base > a 10 x 3 x10 rectangle, Dull Silver
  //2. The Stem > a 5 x 20 x 3 Rectangle placed partially inside the base
  //3. Then core system > a 22 x 18 x 3 Silver Dull Rectange part of the stem might go inside it
  //4. The Viewing port > a 20 x 15 x 1 Square placed slightly above center on the AIO system

  glColor3f(.5,.5,.5); //Sets Color to Silver
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,.3); //Low Shininess Value
  glBegin(GL_QUADS);
      
  //AIO BASE #1
  //Bottom of Base
  glVertex3f(65,40,-25);
  glVertex3f(65,40,-35);
  glVertex3f(55,40,-35);
  glVertex3f(55,40,-25);

  //Top of Base
  glVertex3f(65,43,-25);
  glVertex3f(65,43,-35);
  glVertex3f(55,43,-35);
  glVertex3f(55,43,-25);

  //Left of Base
  glVertex3f(55,40,-25);
  glVertex3f(55,43,-25);
  glVertex3f(55,43,-35);
  glVertex3f(55,40,-35);

  //Right of Base
  glVertex3f(65,40,-25);
  glVertex3f(65,43,-25);
  glVertex3f(65,43,-35);
  glVertex3f(65,40,-35);


  //Back of Base
  glVertex3f(55,40,-35);
  glVertex3f(55,43,-35);
  glVertex3f(65,43,-35);
  glVertex3f(65,40,-35);

  //Front of Base 
  glVertex3f(55,40,-25);
  glVertex3f(55,43,-25);
  glVertex3f(65,43,-25);
  glVertex3f(65,40,-25);

  //AIO Stem - a 4 x 10 x 3 Rectangle
  //Bottom of Stem
  glVertex3f(58,43,-30);
  glVertex3f(58,43,-32);
  glVertex3f(62,43,-32);
  glVertex3f(62,43,-30);
  //Top of Stem
  glVertex3f(58,53,-30);
  glVertex3f(58,53,-32);
  glVertex3f(62,53,-32);
  glVertex3f(62,53,-30);
  //Left of Stem
  glVertex3f(58,43,-30);
  glVertex3f(58,53,-30);
  glVertex3f(58,53,-32);
  glVertex3f(58,43,-32);
  //Right of Stem
  glVertex3f(62,43,-30);
  glVertex3f(62,53,-30);
  glVertex3f(62,53,-32);
  glVertex3f(62,43,-32);
  //Back of Stem
  glVertex3f(58,43,-32);
  glVertex3f(58,53,-32);
  glVertex3f(62,53,-32);
  glVertex3f(62,43,-32);
  //Front of Stem
  glVertex3f(58,43,-30);
  glVertex3f(58,53,-30);
  glVertex3f(62,53,-30);
  glVertex3f(62,43,-30);

  //AIO Core -  22 x 18 x 3 Rectangle. WIll look like it is hanging off the stem 
  //Bottom of the Core
  glNormal3f(0,0,1);
  glVertex3f(49,48, -30);
  glVertex3f(49,48,-27);
  glVertex3f(71,48,-27);
  glVertex3f(71,48,-30);
      
  //Top of the core
  glVertex3f(49,66, -30);
  glVertex3f(49,66,-27);
  glVertex3f(71,66,-27);
  glVertex3f(71,66,-30);

  //Left of the Core
  glVertex3f(49,66,-30);
  glVertex3f(49,48,-30);
  glVertex3f(49,48,-27);
  glVertex3f(49,66,-27);
  //Right of the core
  glVertex3f(71,66,-30);
  glVertex3f(71,48,-30);
  glVertex3f(71,48,-27);
  glVertex3f(71,66,-27);
  //Back of the Core
  glVertex3f(49,66,-30);
  glVertex3f(49,48,-30);
  glVertex3f(71,48,-30);
  glVertex3f(71,66,-30);
  //Front of the Core
  glVertex3f(49,66,-27);
  glVertex3f(49,48,-27);
  glVertex3f(71,48,-27);
  glVertex3f(71,66,-27);
  //AIO Screen- A shiny black Square 
  glColor3f(0,0,0);
  glMaterialf(GL_FRONT,GL_SHININESS,.9); //Very high Shininess
  glMaterialf(GL_FRONT,GL_DIFFUSE,.1); //Low Diffuse value
  glVertex3f(50,65,-26.9);
  glVertex3f(50,50,-26.9);
  glVertex3f(70,50,-26.9);
  glVertex3f(70,65,-26.9);
  glEnd();  
}
void lampdraw()
{

  //Made up of three Sections
  //1. Base: a 3x1x3 Box
  //2. Stem: a 1x10x1 Rectangle
  //3. Lamp: a 3x3x3 box, with an open section, angled towards the Keyboard
  glColor3f(.5,.5,.5); //Sets Color to Silver
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,.3); //Low Shininess Value
  glBegin(GL_QUADS);
  //Base 
  //Don't need bottom
  //Top
  glVertex3f(23,41,-15);
  glVertex3f(23,41,-18);
  glVertex3f(28,41,-18);
  glVertex3f(28,41,-15);
  //Left
  glVertex3f(23,41,-15);
  glVertex3f(23,40,-15);
  glVertex3f(23,40,-18);
  glVertex3f(23,41,-18);
  //Right
  glVertex3f(28,41,-15);
  glVertex3f(28,40,-15);
  glVertex3f(28,40,-18);
  glVertex3f(28,41,-18);
  //Back
  glVertex3f(23,40,-18);
  glVertex3f(23,41,-18);
  glVertex3f(28,41,-18);
  glVertex3f(28,40,-18);
  //Front
    
  glVertex3f(23,40,-15);
  glVertex3f(23,41,-15);
  glVertex3f(28,41,-15);
  glVertex3f(28,40,-15);
  //Stem: 1 x 10 x1 Rectangle   
  //Left
  glVertex3f(25,41,-16);
  glVertex3f(25,57,-16);
  glVertex3f(25,57,-17);
  glVertex3f(25,41,-17);    
  //Right
  glVertex3f(26,41,-16);
  glVertex3f(26,57,-16);
  glVertex3f(26,57,-17);
  glVertex3f(26,41,-17);    

  //Back
  glVertex3f(25,41,-17);
  glVertex3f(25,57,-17);
  glVertex3f(26,57,-17);
  glVertex3f(26,41,-17);
  //Front
  glVertex3f(25,41,-16);
  glVertex3f(25,57,-16);
  glVertex3f(26,57,-16);
  glVertex3f(26,41,-16);

  //Lamp Head- an open 3x3x3 Rectangle
  //Bottom
  glVertex3f(27,56,-15);
  glVertex3f(24,57,-15);
  glVertex3f(24,57,-18);
  glVertex3f(27,56,-18);
  //Top
  glVertex3f(27,59,-15);
  glVertex3f(24,60,-15);
  glVertex3f(24,60,-18);
  glVertex3f(27,59,-18);
  //Left
  glVertex3f(24,57,-15);
  glVertex3f(24,60,-15);
  glVertex3f(24,60,-18);
  glVertex3f(24,57,-18);
    
  //Back 
  glVertex3f(27,56,-18);
  glVertex3f(24,57,-18);
  glVertex3f(24,60,-18); 
  glVertex3f(27,59,-18);
  //Front
    
  glVertex3f(27,56,-15);
  glVertex3f(24,57,-15);
  glVertex3f(24,60,-15);
  glVertex3f(27,59,-15);
  //Right will be slightly Emissive
  static GLfloat em[] = {1, 1, .1, 1};
  static GLfloat nem[] = {0, 0, 0, 1};
  glColor4f(1,1,0,1);
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, em);
  glVertex3f(27,57,-15);
  glVertex3f(27,59,-15);
  glVertex3f(27,59,-18);
  glVertex3f(27,57,-18);
  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, nem );
  glEnd();
}
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  /* Update viewer position in modelview matrix */
  glLoadIdentity();
  gluLookAt(viewer[0],viewer[1],viewer[2], 50.0, 45.0, -10.0, 0.0,   //X,Y,Z COP | XYZ of Target | XYZ of Tilt
        1.0, 0.0); 
  tabledraw();
  kbmdraw();
  aiodraw();
  lampdraw();
  glFlush();
  //if(on == 1) glEnable(GL_LIGHT0);
  //    else glDisable(GL_LIGHT0);
  glutSwapBuffers();
}

int main(int argc, char **argv)
{
        
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(500, 500);
  glutCreateWindow("Table Lighting");       
        
  glutReshapeFunc(myReshape);
  glutDisplayFunc(display);
  glutKeyboardFunc(keys);
  myinit();
  glEnable(GL_DEPTH_TEST);
  glutMainLoop();
  return 0;
}

Currently the light is outside of the lamp, and pointing straight down for simplicity sake. I've tried modifying values. Spot direction cutoff causes a segmentation fault.

0

There are 0 best solutions below