程式語言 - WxWidgets - v3.0 - OpenGL 1.0 - Interpolate Color



參考資訊:
https://docs.wxwidgets.org/3.0/overview_helloworld.html

main.cpp

#include <math.h>
#include <wx/wx.h>
#include <wx/glcanvas.h>
#include <GL/gl.h>

#define W 320
#define H 240

class MyGL : public wxGLCanvas {
    wxGLContext* context;

public:
    MyGL(wxWindow* parent) : wxGLCanvas(parent, wxID_ANY, nullptr), context(new wxGLContext(this)) {
        Bind(wxEVT_PAINT, &MyGL::OnPaint, this);
    }

    ~MyGL() {
        delete context;
    }

    void OnPaint(wxPaintEvent& event) {
        static int is_inited = 0;
        GLdouble nearPlane = 0.1;
        GLdouble farPlane = 100.0;
        GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
        GLdouble right = top * ((GLfloat)W / (GLfloat)H);

        wxPaintDC dc(this);
        SetCurrent(*context);
        if (is_inited == 0) {
            is_inited = 1;

            glViewport(0, 0, W, H);
            glMatrixMode(GL_PROJECTION);
            glFrustum(-right, right, -top, top, nearPlane, farPlane);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
        }

        glLoadIdentity();
        glTranslatef(-1.0f, 0.0f, -5.0f);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex3f(0.0f, 0.5f, 0.0f);
       
            glColor3f(0.0f, 1.0f, 0.0f);
            glVertex3f(-0.5f, -0.5f, 0.0f);
       
            glColor3f(0.0f, 0.0f, 1.0f);
            glVertex3f(0.5f, -0.5f, 0.0f);
        glEnd();
       
        glLoadIdentity();
        glTranslatef(1.0f, 0.0f, -5.0f);
        glBegin(GL_QUADS);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex3f(1.0f, 1.0f, 0.0f);
       
            glColor3f(0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f, 1.0f, 0.0f);
       
            glColor3f(0.0f, 0.0f, 1.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
       
            glColor3f(1.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
        glEnd();

        SwapBuffers();
    }
};

class MyFrame : public wxFrame {
public:
    MyFrame() : wxFrame(nullptr, wxID_ANY, "main", wxDefaultPosition, wxSize(W, H)) {
        new MyGL(this);
    }
};

class MyApp : public wxApp {
public:
    bool OnInit() override {
        MyFrame* frame = new MyFrame();
        frame->Show();
        return true;
    }
};

wxIMPLEMENT_APP(MyApp);

編譯、執行

$ g++ main.cpp -o main $(wx-config --cxxflags) $(wx-config --libs gl,core,base) -lGL
$ ./main