OpenGL - Not getting background color

574 views Asked by At

I am trying to build a simple OpenGL application in C++. I am using FLTK to build a Window, and within that I am drawing using OpenGL. The following is my code:

// Make the GUI here
#define WIN32
#include <GL/glew.h>
#include <GL/glut.h>
#include <FL/gl.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Color_Chooser.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Button.H>

class Block
{
public:
    Block(int x, int y, int z)
    {
        xyz[0] = x;
        xyz[1] = y;
        xyz[2] = z;
    }

    int getX(){ return xyz[0];}
    int getY(){ return xyz[1];}
    int getZ(){ return xyz[2];}

    void setX(int x){ xyz[0]=x;}
    void setY(int y){ xyz[1]=y;}
    void setZ(int z){ xyz[2]=z;}

private:
    int xyz[3];
};

// Snake is an array blocks.
Block* snake;
// Mouse is a single block
Block* mouse;

void renderEverything();

class GlWindow : public Fl_Gl_Window
{
public:
    // Call back for draw
    void draw()
    {
        glTranslatef(0.0f,0.0f, -10.0f);
        // Get the current state of the universe and draw it
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red  
        glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)  
        glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations  

        // Render everything
        renderEverything();

        //glutSolidCube(5);
        glFlush(); // Flush the OpenGL buffers to the window  
    }

    // To handle key-events
    void handle()
    {
    }

    // Constructor
    GlWindow(int X, int Y, int W, int H, const char* L=0): Fl_Gl_Window(X,Y,W,H,L)
    {
    }

};

void renderEverything()
{
    glBegin(GL_QUADS);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 0.0f);
    glVertex3f( 1.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glEnd();
}

void buildGUI(int argc, char** argv)
{
    // Main Window  
    Fl_Double_Window *window = new Fl_Double_Window(800,500);
    window->label("Snake3D");
    window->color(FL_LIGHT3);
    window->begin();

    // Menu Bar
    Fl_Menu_Bar* menubar = new Fl_Menu_Bar(0,0,800,30);
    menubar->color(FL_LIGHT3);
    menubar->down_color(fl_rgb_color(0,145,255));
    menubar->box(FL_THIN_UP_BOX);
    menubar->add("File/Exit");
    menubar->add("Help/About");

    // OpenGL Window
    GlWindow* gameWindow = new GlWindow(200,30,600,470);

    // Start Button
    Fl_Button* startButton = new Fl_Button(50,200,100,30,"Start");
    Fl_Button* pauseButton = new Fl_Button(50,300,100,30,"Pause");
    window->show();
    window->end();
    Fl::run();
}

int main(int argc, char ** argv)
{   
    // Build GUI
    buildGUI(argc,argv);
}

According to the tutorial I am following, I should get a red background. But only half of my screen is colored

]

What might be going wrong?

0

There are 0 answers