Thread: Having a problem with glut rendering from an object.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Having a problem with glut rendering from an object.

    Basically I revised my experiment programs to use a class, and rather than use a specific system api I'm using GLUT. The thing is when I converted all the rendering and such to an object I must have missed something and now my scene doesn't show anything. It captures the keyboard properly so I know it's in the proper loop, but other than that it's just a black screen. Here's my main source, and the class header and source

    Code:
    //main.cpp
    #define GLUT_NO_LIB_PRAGMA
    #define GLUT_NO_WARNING_DISABLE
    
    #include <GL/glut.h>
    #include "cgfxopengl.h"
    //=========================================================================
    int width = 800, height = 600;
    CGfxOpenGL* glRender = new CGfxOpenGL;
    //=========================================================================
    
    void renderScene(void)
    {
        glRender->prepare();
        glRender->render();
    
        glutSwapBuffers();
    }
    
    void preserveRatio(int width, int height)
    {   glRender->setupProjection(width, height);  }
    
    void specialKeys(int key, int x, int y)
    {
        switch(key)
        {
            case GLUT_KEY_F1:
                glRender->toggleFlashLight();
                break;
            default:
                break;
        }
    }
    
    void normalKeys(unsigned char key, int x, int y)
    {
        if(key == 27)
            exit(0);
    }
    
    //main
    int main(int argc, char **argv)
    {
        //Initializes and Creates Window
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutInitWindowPosition(100,100);
        glutInitWindowSize(width , height);
        glutCreateWindow("My GLUT Window");
    
        //displayfunctions
        glutDisplayFunc(renderScene);
        glutIdleFunc(renderScene);
        glutReshapeFunc(preserveRatio);
    
        //keyboard function
        glutSpecialFunc(specialKeys);
        glutKeyboardFunc(normalKeys);
    
        //rendering object
        glRender->intialize();
    
        glutMainLoop();
    
        return 0;
    }

    Code:
    //cgfxopengl.h
    #ifndef CGFXOPENGL_H
    #define CGFXOPENGL_H
    #include "includes.h"
    
    class CGfxOpenGL
    {
        public:
            CGfxOpenGL() : flashLightOn(true) {}
            virtual ~CGfxOpenGL() {}
    
            //sets up all variables and necessary OpenGL functions
            bool intialize();
    
            //projection function
            void setupProjection(int width, int height);
    
            //cleans up all OpenGL Internal states
            bool shutDown();
    
            //prepares the scene for rendering (if animation needed)
            void prepare();
    
            //renders the scene
            void render();
    
        //special functions
        void toggleFlashLight() { flashLightOn = !flashLightOn; }
    
        private:
            int windowWidth;
            int windowHeight;
    
            bool flashLightOn;
    };
    #endif // CGFXOPENGL_H
    Code:
    //cgfxopengl.cpp
    #include "cgfxopengl.h"
    #include "shapes.h"
    //=========================================================================
    GLfloat lightAngle;
    GLfloat flashLightPos[] = {0.0, 0.0, 0.0, 1.0};
    GLfloat flashLightDir[] = {0.0, 0.0, -1.0};
    GLfloat flashLightCol[] = {0.2, 0.2, 0.2, 1.0};
    
    GLfloat bigLightPos[] = {0.0, 1.0, 1.0, 0.0};
    GLfloat bigLightCol[] = {0.3, 0.3, 0.3, 1.0};
    
    //albient color for the scene, bluish Green
    GLfloat globalAmbience[] = {0.0, 0.2, 0.3, 1.0};
    //=========================================================================
    
    
    bool CGfxOpenGL::intialize()
    {
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbience);
    
        //set up the flashLight
        glEnable(GL_LIGHT0);
            glLightfv(GL_LIGHT0, GL_POSITION, flashLightPos);
            glLightfv(GL_LIGHT0, GL_AMBIENT, flashLightCol);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, flashLightCol);
            glLightfv(GL_LIGHT0, GL_SPECULAR, flashLightCol);
            glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, flashLightDir);
            glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0);
    
        //set up the large light
    
    
        return true;
    }
    
    void CGfxOpenGL::setupProjection(int width, int height)
    {
        //prevent division by 0
        if(height = 0)
            height = 1;
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, width, height);
        gluPerspective(60.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0.0, 0.0, -1.0,
                  0.0, 0.0, 0.0,
                  0.0, 1.0, 0.0);
    
        windowWidth = width;
        windowHeight = height;
    }
    
    void CGfxOpenGL::render()
    {
        glPushMatrix();
            GLfloat coneColor[] = {0.5, 0.8, 1.0, 1.0};
            GLfloat coneSpecular[] = { 1.0, 1.0, 1.0, 1.0};
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, coneColor);
            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, coneSpecular);
            glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 15.0);
    
            glTranslatef(0, 0, 12);
            DrawCube(2, 12);
        glPopMatrix();
    
    }
    
    bool CGfxOpenGL::shutDown()
    {
        return true;
    }
    
    void CGfxOpenGL::prepare()
    {
        if(flashLightOn)
            glEnable(GL_LIGHT0);
        else
            glDisable(GL_LIGHT0);
    }
    At first I thought it was the position of the camera, but I changed it and relocated the cube (which DrawCube is defined elsewhere and works properly), but it doesn't show it. It worked find normally, but after I put it in a class it just stopped.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    okay I corrected some mistakes in the class but I'm still having problems, I know it's not the main source because I replaced the class with the one in the book and still nothing

    Code:
    #include "cgfxopengl.h"
    #include "shapes.h"
    //=========================================================================
    const GLfloat flashLightPos[] = { 0.0, 0.0, 0.0, 1.0 };
    const GLfloat flashLightDir[] = { 0.0, 0.0, -1.0 };
    const GLfloat flashLightCol[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    
    GLfloat redLightPos[] = {3.0, 2.0, 0.0, 1.0};
    GLfloat redLightCol[] = {1.0f, 0.0f, 0.0f, 1.0f};
    
    //albient color for the scene, bluish Green
    GLfloat globalAmbience[] = {0.0, 0.2, 0.3, 1.0};
    //=========================================================================
    
    CGfxOpenGL::CGfxOpenGL()
    {
        flashLightOn = GL_TRUE;
    }
    
    CGfxOpenGL::~CGfxOpenGL()
    {
    }
    
    bool CGfxOpenGL::intialize()
    {
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
    
        //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbience);
    
        //set up the flashLight
        glEnable(GL_LIGHT0);
            glLightfv(GL_LIGHT0, GL_POSITION, flashLightPos);
            glLightfv(GL_LIGHT0, GL_AMBIENT, flashLightCol);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, flashLightCol);
            glLightfv(GL_LIGHT0, GL_SPECULAR, flashLightCol);
            glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, flashLightDir);
            glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20.0);
    
        glEnable(GL_LIGHT1);
            glLightfv(GL_LIGHT1, GL_POSITION, redLightPos);
            glLightfv(GL_LIGHT1, GL_AMBIENT, redLightCol);
            glLightfv(GL_LIGHT1, GL_DIFFUSE, redLightCol);
            glLightfv(GL_LIGHT1, GL_SPECULAR, redLightCol);
    
        return true;
    }
    
    void CGfxOpenGL::setupProjection(int width, int height)
    {
        //prevent division by 0
        if(height = 0)
        {
            height = 1;
        }
    
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    
        gluPerspective(60.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        windowWidth = width;
        windowHeight = height;
    }
    
    void CGfxOpenGL::render()
    {
        glClear(GL_COLOR_BUFFER_BIT |
                GL_DEPTH_BUFFER_BIT |
                GL_STENCIL_BUFFER_BIT);
    
        glLoadIdentity();
        gluLookAt(0.0, 0.0, 20.0,
                  0.0, 0.0, 0.0,
                  0.0, 1.0, 0.0);
    
        if(flashLightOn)
            glEnable(GL_LIGHT0);
        else
            glDisable(GL_LIGHT0);
    
            GLfloat cubeColor[] = { 0.6f, 0.7f, 1.0f, 1.0f };
            GLfloat cubeSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cubeColor);
            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cubeSpecular);
            glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 15.0);
    
            DrawCone(3, 3, 10);
    }
    
    bool CGfxOpenGL::shutDown()
    {
        return true;
    }
    
    void CGfxOpenGL::prepare()
    {
    }
    The draw cone functions correctly and is defined elsewhere.
    What is wrong with my code.

    Edit: Could a mod clear the code in the first post and replace it with this one.
    Last edited by indigo0086; 04-04-2007 at 06:49 PM.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    I don't see where you call CGfxOpenGL::setupProjection().

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Yes I know That is just the class, all the calls are in the main method. The call to setupProjection is in the main method's reshape function here
    Code:
    void preserveRatio(int width, int height)
    {   glRender->setupProjection(width, height);  }
    I've tried calling it before I set it up and it doesn't work.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Ugh, I just trashed it and am starting from scratch, the new one I'm working on works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  4. deleting dynamic object problem
    By eth0 in forum C++ Programming
    Replies: 17
    Last Post: 05-19-2004, 01:17 PM
  5. C++ Programming (Problem One)
    By Nicknameguy in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2002, 06:38 PM