need help with opengl lighting!!

This is a discussion on need help with opengl lighting!! within the Game Programming forums, part of the General Programming Boards category; I just finished the chapter on lighting from the red book. So I figured id put some skillz to the ...

  1. #1
    genghis37
    Guest

    Unhappy need help with opengl lighting!!

    I just finished the chapter on lighting from the red book. So I figured id put some skillz to the test and rip out a program that modeled the solar system fairly accurately.

    Well everything seemed to be going quite well. I got a window on the screen. I got opengl mapped to the window. I rendered a sphere at the origin of the screen. Now, ofcourse the sphere looked rather flat until I enabled the lighting. And so then even the lighting seemed to be working. But then I tried to position the light in various different positions and the way the sphere was being illuminated seemed rather bizarre. For example, with the modelview matrix always set to identity, i would set the light position to (0,1,0,0) and it would illuminate the sphere from the right.

    Maybe im doing something stupid or maybe i really missed out on something, but i dont care cuz I really just wanna get it working. I tried to run all sorts of tests with no good results.

    heres the main portion of the code:
    void init()
    {
    glViewport(0, 0, width, height);

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClearDepth(1.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    float pos0[4] = {0.0, 0.0, 5.0, 0.0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos0);
    glEnable(GL_LIGHT0);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_LIGHTING);
    glEnable(GL_NORMALIZE);
    }

    bool display()
    {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    float pos0[4] = {1.2, 0.0, 0.0, 1.0};
    glRotatef(angle++, 1.0, 0.0, 1.0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos0);
    glEnable(GL_LIGHT0);

    glLoadIdentity();
    glPointSize(10.0);
    glDisable(GL_LIGHTING);
    glBegin(GL_POINTS);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(4.5, 0.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 4.5, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 4.5);
    glEnd();
    glEnable(GL_LIGHTING);

    glLoadIdentity();
    glRotatef(angle+=0.1f, 1.0, 0.0, 0.0);
    DrawSphere(5);

    return true;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    oh nm. solved the problem. very stupid. i didnt even learn anything out of it. just wasted a lot of time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shininess lighting GLUT OpenGL
    By Zishaan in forum Game Programming
    Replies: 1
    Last Post: 04-22-2007, 08:30 PM
  2. OpenGL lighting
    By Zishaan in forum Game Programming
    Replies: 6
    Last Post: 04-21-2007, 03:24 PM
  3. OpenGL lighting
    By BabyG in forum Game Programming
    Replies: 3
    Last Post: 08-29-2004, 09:58 AM
  4. Lighting in OpenGL
    By MathFan in forum Game Programming
    Replies: 5
    Last Post: 06-28-2004, 12:56 PM
  5. OpenGL lighting problems
    By sirSolarius in forum Game Programming
    Replies: 0
    Last Post: 12-01-2003, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21