程式語言 - WxWidgets - v3.0 - OpenGL 1.0 - Draw Triangle



參考資訊:
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;

        wxPaintDC dc(this);
        SetCurrent(*context);
        if (is_inited == 0) {
            is_inited = 1;
            glViewport(0, 0, W, H);
        }

        glBegin(GL_TRIANGLES);
            glVertex3f(0.0f, 0.5f, 0.0f);
            glVertex3f(-0.5f, -0.5f, 0.0f);
            glVertex3f(0.5f, -0.5f, 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