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

Thread: GLUT problem I think

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    7

    GLUT problem I think

    Hey guys hope you're all having a great day

    so my issue with my code is kinda driving me mad, its one of those things Ive stared at enough trying to find the issue and so I just cant see it. Honestly, Im not sure what could be causing the problem, I can tell you however that it is most likely to do with GLUT if anyone here knows enough to be able to help me. originally my program drew a blank window but then wouldnt do much else so I commented out glutMainLoop() and then the window didnt even draw

    Here is some code anyway, starting with a simple main that will let you compile it if you do help:
    Code :
    int main(int argc, char** argv){
        window = new GlutWindow(argc, argv, "EcoSphere", 800, 600);
     
     
        GameLoop::get()->init(window);
        GameLoop::get()->run();
     
     
        delete window;
     
     
        return 0;
    };

    GlutWindow:
    Code :
    #ifndef GLUTWINDOW_H#define GLUTWINDOW_H
     
     
    #include "glut.h"
    #include "Window.h"
    #include "GameLoop.h"
     
     
    void draw(void);
    void resize(int, int);
    void kybdPressed(unsigned char, int, int);
    void kybdReleased(unsigned char, int, int);
     
     
    class GlutWindow : public Window
    {
    private:
        int window;
        static Input* input;
     
     
    public:
        GlutWindow(int argc, char** argv, char* title, int w, int h)
        {
            glutInit(&argc, argv);
            glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
            glutInitWindowSize(w, h);
            glutInitWindowPosition(w/10, h/10);
            window = glutCreateWindow(title);
     
     
            glutDisplayFunc(draw);
            glutReshapeFunc(resize);
            glutKeyboardFunc(kybdPressed);
            glutKeyboardUpFunc(kybdReleased);
     
     
            //glutMainLoop();
        }
     
     
        static void draw()
        {
            glClearColor(0.0, 0.0, 0.0, 1.0);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
     
            glutSolidTeapot(3.0);
     
     
            glFlush(); // Complete Pending Operations
            glutSwapBuffers(); //Switch Buffer (Double Buffering)
        }
     
     
        static void resize(int w, int h)
        {
            glViewport(0, 0, (GLsizei)w, (GLsizei)h);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(45, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
            glMatrixMode(GL_MODELVIEW);
        }
     
     
        static void kybdPressed(unsigned char key, int x, int y)
        {
            input->keyPressed(key);
        }
     
     
        static void kybdReleased(unsigned char key, int x, int y)
        {
            input->keyReleased(key);
        }
     
     
        void redraw()
        {
            glutPostRedisplay();
        }
     
     
        virtual ~GlutWindow(){}
    };
     
     
    Entities* GlutWindow::entities = nullptr;
    Input* GlutWindow::input = nullptr;
     
     
    #endif

    GameLoop:
    Code :
    #ifndef GAMELOOP_H#define GAMELOOP_H
     
     
    #include "Input.h"
    #include "Window.h"
     
     
    class GameLoop
    {
    private:
        static GameLoop* instance;
        GameLoop(){}
     
     
        Controls* controls;
     
     
        Window* window;
     
     
        static bool running;
     
     
    public:
        static GameLoop* get()
        {
            if(instance == nullptr)
            {
                instance = new GameLoop();
            }
            return instance;
        }
     
     
        void init(Window* w)
        {
            window = w;
        }
     
     
        void run()
        {
            bool running = true;
     
     
            while(running)
            {
                bool* keys = Input::get()->getKybdState();
     
                            //use keyboard input and update all entities
     
     
                window->redraw();
            }
        }
     
     
        static void end()
        {
            running = false;
        }
    };
     
     
    GameLoop* GameLoop::instance = nullptr;
    bool GameLoop::running = true;
     
     
    #endif

    finally the input class:
    Code :
    #ifndef INPUT_H#define INPUT_H
     
     
    class Input
    {
    private:
        static Input* instance;
        Input(){}
     
     
        static bool * kybdState;
     
     
    public:
        static Input* get()
        {
            if(instance == nullptr)
            {
                for(int i = 0; i < 256; i++)
                {
                    kybdState[i] = false;
                }
     
     
                instance = new Input();
            }
            return instance;
        }
     
     
        static bool* getKybdState()
        {
            return kybdState;
        }
     
     
        static void keyPressed(unsigned char key)
        {
            kybdState[key] = true;
        }
     
     
        static void keyReleased(unsigned char key)
        {
            kybdState[key] = false;
        }
     
     
        virtual ~Input()
        {
            delete kybdState;
        }
    };
     
     
    Input* Input::instance = nullptr;
    bool * Input::kybdState = new bool[256];
     
     
    #endif

    Really sorry for the mass of code, there are some things edited out because they would make compiling hard but it should work as is here, if someone is to help me and feel they need any other information, please say and I will get it right away, also, thank you for your time if you read down this far :P

  2. #2
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4
    I guess, at least you must call `glutMainLoop` once.
    hxxp://www.opengl.org/documentation/specs/glut/spec3/node14.html

    Also, you didn't even called `glColor*` (e.g. `glColor3f`) to set the color to draw things, so I guess you'd probably get a black scene. You may also need to enable lighting etc etc.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    7
    oops, yeah forgot to mention I do have the lights and colour set up, just didnt include them, I have an init() function that is called before everything

    and the glutMainLoop 'should be called at most once in a GLUT program', so I thought it isnt always necessary, because when I had it, my program was catching when it called this line and didnt run through the game loop I wrote, instead going through whatever glut wants to do

Posting Permissions

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