OpenGL - 没有获得背景颜色

时间:2014-11-27 15:58:01

标签: c++ opengl fltk

我正在尝试用C ++构建一个简单的OpenGL应用程序。我正在使用FLTK构建一个Window,并且我正在使用OpenGL进行绘制。以下是我的代码:

// 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);
}

根据我关注的教程,我应该得到一个红色背景。但我的屏幕只有一半是彩色的

]

可能出现什么问题?

0 个答案:

没有答案