Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

[C++] [OpenGL + SDL] Can't get lights to work

This topic is 3311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm working through NeHe's tutorials, they are really great. I'm using SDL for window management because I'm used to it and because I use Linux and it seemed like the most easy way to do it. However, I can't get my code to work... Could someone please look if they can find any big flaws? The textured cube and the coloured tetrahedron show up and rotate as planned, but aren't lit at all, they are just fully lit as if I never wrote the code for the lights.
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

int main(int argc, char** argv)
{
    // Initialization
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);

    SDL_WM_SetCaption("OpenGL Lights", NULL);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat height = (GLfloat) SDL_GetVideoSurface()->h;
    GLfloat width  = (GLfloat) SDL_GetVideoSurface()->w;
    gluPerspective(45.0f, width / height, 0.1f, 100.0f);

    // Lights
    GLfloat ambient[]  = { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat lightpos[] = { 0.0f, 0.0f, 2.0f, 1.0f };

    glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, lightpos);

    glEnable(GL_LIGHT1);

    // Vars
    SDL_Event event;
    bool done = false;
    GLfloat xrot = 0.0f;
    GLfloat yrot = 0.0f;
    GLfloat zrot = 0.0f;
    GLuint texture;

    {
        SDL_Surface* sfc = SDL_LoadBMP("nehe.bmp");

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, sfc->w, sfc->h, 0,
                     GL_BGR, GL_UNSIGNED_BYTE, sfc->pixels);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        SDL_FreeSurface(sfc);
    }

    // Main loop
    while (!done)
    {
        // Event polling
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) done = true;

            if (event.type == SDL_KEYUP) {
                done = true;
            }
        }

        // OpenGL magix
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity();
        glTranslatef(-2.0f, 0.0f, -8.0f);

        glRotatef(xrot, 1.0f, 0.0f, 0.0f);
        glRotatef(yrot, 0.0f, 1.0f, 0.0f);
        glRotatef(zrot, 0.0f, 0.0f, 1.0f);

        glEnable(GL_TEXTURE_2D);
        glColor3f(1, 1, 1);
        glBindTexture(GL_TEXTURE_2D, texture);

        glBegin(GL_QUADS);
            // Bottom
            glNormal3f(0, -1, 0);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1, -1,  1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1, -1, -1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1, -1, -1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1, -1,  1);

            // Front
            glNormal3f(0, 0, 1);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1,  1,  1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1, -1,  1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1, -1,  1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1,  1,  1);

            // Top
            glNormal3f(0, 1, 0);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1,  1, -1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1,  1,  1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1,  1,  1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1,  1, -1);

            // Back
            glNormal3f(0, 0, -1);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1, -1, -1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1,  1, -1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1,  1, -1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1, -1, -1);

            // Left
            glNormal3f(-1, 0, 0);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1,  1, -1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(-1, -1, -1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f(-1, -1,  1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1,  1,  1);

            // Right
            glNormal3f(1, 0, 0);
            glTexCoord2f(0.0f, 1.0f); glVertex3f( 1, -1, -1);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1,  1, -1);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1,  1,  1);
            glTexCoord2f(0.0f, 0.0f); glVertex3f( 1, -1,  1);
        glEnd();

        glDisable(GL_TEXTURE_2D);

        glLoadIdentity();
        glTranslatef(2.0f, 0.0f, -8.0f);

        glRotatef(xrot, 1.0f, 0.0f, 0.0f);
        glRotatef(yrot, 0.0f, 1.0f, 0.0f);
        glRotatef(zrot, 0.0f, 0.0f, 1.0f);

        glBegin(GL_TRIANGLES);
            glNormal3f(-1, 1, 1);
            glColor3f(1, 0, 0); glVertex3f(-1, -1,  1);
            glColor3f(0, 1, 0); glVertex3f( 1,  1,  1);
            glColor3f(0, 0, 1); glVertex3f(-1,  1, -1);

            glNormal3f(1, -1, 1);
            glColor3f(1, 0, 0); glVertex3f( 1,  1,  1);
            glColor3f(0, 1, 0); glVertex3f(-1, -1,  1);
            glColor3f(0, 0, 1); glVertex3f( 1, -1, -1);

            glNormal3f(-1, -1, -1);
            glColor3f(1, 0, 0); glVertex3f(-1,  1, -1);
            glColor3f(0, 1, 0); glVertex3f( 1, -1, -1);
            glColor3f(0, 0, 1); glVertex3f(-1, -1,  1);

            glNormal3f(1, 1, -1);
            glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
            glColor3f(0, 1, 0); glVertex3f(-1,  1, -1);
            glColor3f(0, 0, 1); glVertex3f( 1,  1,  1);
        glEnd();

        SDL_GL_SwapBuffers();
        glFlush();

        // Rotate variables increments
        xrot += 1.0f;
        yrot += 0.7f;
        zrot += 0.6f;

        // Sleep
        SDL_Delay(10);
    }

    return 0;
}
Thanks

Share this post


Link to post
Share on other sites
Advertisement
I second that,

Add a glEnable(GL_LIGHTING) under glEnable(GL_LIGHT0), or anywhere in that area.

Share this post


Link to post
Share on other sites
Hmm alright, now that seems to enable lighning, but now the colours on my tetrahedron are gone! It just appears white (with correct light effects), however it used to be nicely coloured as it was taught in one of the first tutorials... How can I regain this colour?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!