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 1 of 1

Thread: Help! I can't get my lighting or textures to work

  1. #1
    Newbie Newbie
    Join Date
    Oct 2012
    Posts
    1

    Help! I can't get my lighting or textures to work

    I started learning openGL a few months ago for a class and I'm in way over my head. I think I have most of the basics down but I just can't get my lighting or textures to work. It complies fine, but none of the lighting on my cube is showing up and neither is the texture. I think that I need to link the texture bmp to the file so my computer can find it but I'm not sure exactly how to do that. Here is my source code:

    Code :
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <math.h>
    #define GL_GLEXT_PROTOTYPES
    #ifdef __APPLE__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
     
    int axes=1;       //  Display axes
    int mode=1;       //  Projection mode
    int move=1;       //  Move light
    int th=0;         //  Azimuth of view angle
    int ph=0;         //  Elevation of view angle
    int fov=55;       //  Field of view (for perspective)
    int light=1;      //  Lighting
    double asp=1;     //  Aspect ratio
    double dim=3.0;   //  Size of world
    // Light values
    int emission  =   0;  // Emission intensity (%)
    int ambient   =  30;  // Ambient intensity (%)
    int diffuse   = 100;  // Diffuse intensity (%)
    int specular  =   0;  // Specular intensity (%)
    int shininess =   0;  // Shininess (power of two)
    float shinyvec[1];    // Shininess (value)
    int zh        =  90;  // Light azimuth
    float ylight  =   0;  // Elevation of light
    int ntex=0;       //  Cube faces
    int rep=1;        //  Repitition
    unsigned int texture[1]; // Texture names
    int distance  =   5;  // Light distance
    int local     =   0;  // Local Viewer Model
     
     
     
    //  Cosine and Sine in degrees
    #define Cos(x) (cos((x)*3.1415927/180))
    #define Sin(x) (sin((x)*3.1415927/180))
     
     
     
     
    /*
     *  Draw a cube
     *     at (x,y,z)
     *     dimentions (dx,dy,dz)
     *     rotated th about the y axis
     */
    static void cube(double x,double y,double z,
                     double dx,double dy,double dz,
                     double th)
     
    {
       //  Set specular color to white
       float white[] = {1,1,1,1};
       float Emission[] = {0.0,0.0,0.1*emission,1.0};
       glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shinyvec);
       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);
       glRotated(th,0,1,0);
       glScaled(dx,dy,dz);
       //  Enable textures
       glEnable(GL_TEXTURE_2D);
       glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,mode?GL_REPLACE:GL_MODULATE);
       glColor3f(1,1,1);
       glBindTexture(GL_TEXTURE_2D,texture[0]);
       //  Front
       glColor3f(1,0,0);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f( 0, 0, 1);
       glTexCoord2f(0,0); glVertex3f(-1,-1, 1);
       glTexCoord2f(1,0); glVertex3f(+1,-1, 1);
       glTexCoord2f(1,1); glVertex3f(+1,+1, 1);
       glTexCoord2f(0,1); glVertex3f(-1,+1, 1);
       glEnd();
       //  Back
       glColor3f(0,0,1);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f( 0, 0,-1);
       glTexCoord2f(0,0); glVertex3f(+1,-1,-1);
       glTexCoord2f(1,0); glVertex3f(-1,-1,-1);
       glTexCoord2f(1,1); glVertex3f(-1,+1,-1);
       glTexCoord2f(0,1); glVertex3f(+1,+1,-1);
       glEnd();
       //  Right
       glColor3f(1,1,0);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f(+1, 0, 0);
       glTexCoord2f(0,0); glVertex3f(+1,-1,+1);
       glTexCoord2f(1,0); glVertex3f(+1,-1,-1);
       glTexCoord2f(1,1); glVertex3f(+1,+1,-1);
       glTexCoord2f(0,1); glVertex3f(+1,+1,+1);
       glEnd();
       //  Left
       glColor3f(0,1,0);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f(-1, 0, 0);
       glTexCoord2f(0,0); glVertex3f(-1,-1,-1);
       glTexCoord2f(1,0); glVertex3f(-1,-1,+1);
       glTexCoord2f(1,1); glVertex3f(-1,+1,+1);
       glTexCoord2f(0,1); glVertex3f(-1,+1,-1);
       glEnd();
       //  Top
       glColor3f(0,1,1);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f( 0,+1, 0);
       glTexCoord2f(0,0); glVertex3f(-1,+1,+1);
       glTexCoord2f(1,0); glVertex3f(+1,+1,+1);
       glTexCoord2f(1,1); glVertex3f(+1,+1,-1);
       glTexCoord2f(0,1); glVertex3f(-1,+1,-1);
       glEnd();
       //  Bottom
       glColor3f(1,0,1);
       if (ntex) glBindTexture(GL_TEXTURE_2D,texture[1]);
       glBegin(GL_QUADS);
       glNormal3f( 0,-1, 0);
       glTexCoord2f(0,0); glVertex3f(-1,-1,-1);
       glTexCoord2f(1,0); glVertex3f(+1,-1,-1);
       glTexCoord2f(1,1); glVertex3f(+1,-1,+1);
       glTexCoord2f(0,1); glVertex3f(-1,-1,+1);
       glEnd();
       //  Undo transformations and textures
       glPopMatrix();
       glDisable(GL_TEXTURE_2D);
    }
     
    static void ball(double x,double y,double z,double r)
    {
       //  Save transformation
       glPushMatrix();
       //  Offset, scale and rotate
       glTranslated(x,y,z);
       glScaled(r,r,r);
       //  White ball
       glColor3f(1,1,1);
       glutSolidSphere(1.0,16,16);
       //  Undo transofrmations
       glPopMatrix();
    }
     
    // Projection
    static void Project()
    {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      if (mode)
         gluPerspective(fov,asp,dim/3,3*dim);
      else
         glOrtho(-asp*dim,+asp*dim,-dim,+dim,-dim,+dim);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }
     
    void display()
    {
       const double len=2.0;  //  Length of axes
       //  Erase the window and the depth buffer
       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
       //  Enable Z-buffering in OpenGL
       glEnable(GL_DEPTH_TEST);
     
       //  Undo previous transformations
       glLoadIdentity();
       //  Perspective - set eye position
       if (mode)
       {
          double Ex = -2*dim*Sin(th)*Cos(ph);
          double Ey = +2*dim        *Sin(ph);
          double Ez = +2*dim*Cos(th)*Cos(ph);
          gluLookAt(Ex,Ey,Ez,0,0,0,0,Cos(ph),0);
       }
       //  Orthogonal - set world orientation
       else
       {
          glRotatef(ph,1,0,0);
          glRotatef(th,0,1,0);
       }
       //  Light switch
       if (light)
       {
          //  Translate intensity to color vectors
          float Ambient[]   = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
          float Diffuse[]   = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
          float Specular[]  = {0.01*specular,0.01*specular,0.01*specular,1.0};
          //  Light position
          float Position[]  = {distance*Cos(zh),ylight,distance*Sin(zh),1.0};
          //  Draw light position as ball (still no lighting here)
          glColor3f(1,1,1);
          ball(Position[0],Position[1],Position[2] , 0.1);
          //  OpenGL should normalize normal vectors
          glEnable(GL_NORMALIZE);
          //  Enable lighting
          glEnable(GL_LIGHTING);
          //  Location of viewer for specular calculations
          glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,local);
          //  glColor sets ambient and diffuse color materials
          glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
          glEnable(GL_COLOR_MATERIAL);
          //  Enable light 0
          glEnable(GL_LIGHT0);
          //  Set ambient, diffuse, specular components and position of light 0
          glLightfv(GL_LIGHT0,GL_AMBIENT ,Ambient);
          glLightfv(GL_LIGHT0,GL_DIFFUSE ,Diffuse);
          glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
          glLightfv(GL_LIGHT0,GL_POSITION,Position);
       }
     
       //  Draw scene
       cube(+1,0,0,0.5,0.5,0.5,0);
       ball(-1,0,0,0.5);
     
       //  Draw axes - no lighting from here on
       glDisable(GL_LIGHTING);
       glColor3f(1,1,1);
       if (axes)
       {
          glBegin(GL_LINES);
          glVertex3d(0.0,0.0,0.0);
          glVertex3d(len,0.0,0.0);
          glVertex3d(0.0,0.0,0.0);
          glVertex3d(0.0,len,0.0);
          glVertex3d(0.0,0.0,0.0);
          glVertex3d(0.0,0.0,len);
          glEnd();
       }
     
       //  Render the scene and make it visible
       glFlush();
       glutSwapBuffers();
    }
     
    void special(int key,int x,int y)
    {
       if (key == GLUT_KEY_RIGHT)
          th += 5;
       else if (key == GLUT_KEY_LEFT)
          th -= 5;
       else if (key == GLUT_KEY_UP)
          ph += 5;
       else if (key == GLUT_KEY_DOWN)
          ph -= 5;
       th %= 360;
       ph %= 360;
       Project();
       glutPostRedisplay();
    }
     
    void key(unsigned char ch,int x, int y)
    {
       // Exit on ESC
       if (ch == 27)
          exit(0);
       // Switch display mode
       else if (ch == 'p' || ch == 'P')
          mode = 1-mode;
       // Changle fov
       else if (ch == '-' && ch>1)
          fov--;
       else if (ch == '+' && ch<179)
          fov++;
       //  Toggle lighting
       else if (ch == 'l' || ch == 'L')
          light = 1-light;
       //  Toggle light movement
       else if (ch == 'm' || ch == 'M')
          move = 1-move;
       //  Move light
       else if (ch == '<')
          zh += 1;
       else if (ch == '>')
          zh -= 1;
       //  Light elevation
       else if (ch=='[')
          ylight -= 0.1;
       else if (ch==']')
          ylight += 0.1;
       //  Ambient level
       else if (ch=='a' && ambient>0)
          ambient -= 5;
       else if (ch=='A' && ambient<100)
          ambient += 5;
       //  Diffuse level
       else if (ch=='d' && diffuse>0)
          diffuse -= 5;
       else if (ch=='D' && diffuse<100)
          diffuse += 5;
       //  Specular level
       else if (ch=='s' && specular>0)
          specular -= 5;
       else if (ch=='S' && specular<100)
          specular += 5;
       //  Emission level
       else if (ch=='e' && emission>0)
          emission -= 5;
       else if (ch=='E' && emission<100)
          emission += 5;
       //  Shininess level
       else if (ch=='n' && shininess>-1)
          shininess -= 1;
       else if (ch=='N' && shininess<7)
          shininess += 1;
       //  Translate shininess power to value (-1 => 0)
       shinyvec[0] = shininess<0 ? 0 : pow(2.0,shininess);
       //  Reproject
       Project(mode?fov:0,asp,dim);
       glutPostRedisplay();
    } 
     
    void reshape(int width,int height)
    {
       asp = (height>0) ? (double)width/height : 1;
       glViewport(0,0,width,height);
       Project();
    }
     
    /*
     *  GLUT calls this routine when there is nothing else to do
     */
    void idle()
    {
       double t = glutGet(GLUT_ELAPSED_TIME)/1000.0;
       glutPostRedisplay();
    }
     
    /*
     *  Start up GLUT and tell it what to do
     */
    int main(int argc,char* argv[])
    {
       //  Initialize GLUT and process user parameters
       glutInit(&argc,argv);
       //  Request double buffered, true color window with Z buffering at 600x600
       glutInitWindowSize(600,600);
       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
       //  Create the window
       glutCreateWindow("hw7");
       //  Tell GLUT to call "idle" when there is nothing else to do
       glutIdleFunc(idle);
       //  Tell GLUT to call "display" when the scene should be drawn
       glutDisplayFunc(display);
       //  Tell GLUT to call "reshape" when the window is resized
       glutReshapeFunc(reshape);
       //  Tell GLUT to call "special" when an arrow key is pressed
       glutSpecialFunc(special);
       //  Tell GLUT to call "key" when a key is pressed
       glutKeyboardFunc(key);
       // Texture
       texture[1] = LoadTexBMP("texture1.bmp");
       //  Pass control to GLUT so it can interact with the user
       glutMainLoop();
       return 0;
    }

    Any help would be greatly appreciated!
    Last edited by marbles; 10-31-2012 at 09:15 AM.

Posting Permissions

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