スポンサーリンク

wxWidgetsでOpenGLを使う

// インクルードディレクトリ
// /wxWidgets/include
// /wxWidgets/release/lib/vc_x64_dll/mswu

// ライブラリディレクトリ
// /wxWidgets/release/lib/vc_x64_dll

// プリプロセッサ
// __WXMSW__
// WXUSINGDLL

#if defined(_DEBUG)

#pragma comment(lib,"wxbase32ud.lib")
#pragma comment(lib,"wxbase32ud_net.lib")
#pragma comment(lib,"wxbase32ud_xml.lib")
#pragma comment(lib,"wxmsw32ud_adv.lib")
#pragma comment(lib,"wxmsw32ud_aui.lib")
#pragma comment(lib,"wxmsw32ud_core.lib")
#pragma comment(lib,"wxmsw32ud_gl.lib")
#pragma comment(lib,"wxmsw32ud_html.lib")
#pragma comment(lib,"wxmsw32ud_media.lib")
#pragma comment(lib,"wxmsw32ud_propgrid.lib")
#pragma comment(lib,"wxmsw32ud_qa.lib")
#pragma comment(lib,"wxmsw32ud_ribbon.lib")
#pragma comment(lib,"wxmsw32ud_richtext.lib")
#pragma comment(lib,"wxmsw32ud_stc.lib")
#pragma comment(lib,"wxmsw32ud_webview.lib")
#pragma comment(lib,"wxmsw32ud_xrc.lib")

#else

#pragma comment(lib,"wxbase32u.lib")
#pragma comment(lib,"wxbase32u_net.lib")
#pragma comment(lib,"wxbase32u_xml.lib")
#pragma comment(lib,"wxmsw32u_adv.lib")
#pragma comment(lib,"wxmsw32u_aui.lib")
#pragma comment(lib,"wxmsw32u_core.lib")
#pragma comment(lib,"wxmsw32u_gl.lib")
#pragma comment(lib,"wxmsw32u_html.lib")
#pragma comment(lib,"wxmsw32u_media.lib")
#pragma comment(lib,"wxmsw32u_propgrid.lib")
#pragma comment(lib,"wxmsw32u_qa.lib")
#pragma comment(lib,"wxmsw32u_ribbon.lib")
#pragma comment(lib,"wxmsw32u_richtext.lib")
#pragma comment(lib,"wxmsw32u_stc.lib")
#pragma comment(lib,"wxmsw32u_webview.lib")
#pragma comment(lib,"wxmsw32u_xrc.lib")

#endif


#include <wx/wxprec.h>
#include <wx/glcanvas.h>
#include <wx/frame.h>

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <GL/gl.h>

#pragma comment(lib, "opengl32.lib")


class MyGLCanvas : public wxGLCanvas
{
public:
    MyGLCanvas(wxFrame* parent,
        int* attribList = NULL);

    void Render(wxPaintEvent& evt);
private:

    wxGLContext* m_context; // OpenGL context
};

MyGLCanvas::MyGLCanvas(wxFrame* parent, int* attribList)
    : wxGLCanvas(parent, wxID_ANY, attribList)
{
    m_context = new wxGLContext(this);

    // windowsでちらつき防止
    SetBackgroundStyle(wxBG_STYLE_CUSTOM);
}

void MyGLCanvas::Render(wxPaintEvent& evt)
{
    if (!IsShown()) return;

    int w, h;
    GetClientSize(&w, &h);
    glViewport(0, 0, w, h);


    wxGLCanvas::SetCurrent(*m_context);
    wxPaintDC(this);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor3d(1.0, 0.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    
    glColor3d(0.0, 1.0, 0.0);
    glVertex3f(-1.0, -1.0, 0.0);

    glColor3d(0.0, 0.0, 1.0);
    glVertex3f(1.0, -1.0, 0.0);
    glEnd();

    glFlush();
    SwapBuffers();
}
      
      
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, int xpos, int ypos, int width, int height);
    ~MyFrame();

    void OnSize(wxSizeEvent& event);

    MyGLCanvas* m_canvas = NULL;
};


MyFrame::MyFrame(const wxString& title, int xpos, int ypos, int width, int height)
    : wxFrame((wxFrame*)NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height))
{
    int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0 };

    m_canvas = new MyGLCanvas(this, args);
    m_canvas->Bind(wxEVT_PAINT, &MyGLCanvas::Render, m_canvas);

    Bind(wxEVT_SIZE, &MyFrame::OnSize, this);

}
void MyFrame::OnSize(wxSizeEvent& event) {
    m_canvas->SetSize(event.GetSize());
}

MyFrame::~MyFrame()
{
	delete m_canvas;
}
      
class MyApp : public wxApp
{
public:
    bool OnInit();
};


bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame(wxT("wxWidgets OpenGL"), 50, 50, 400, 400);
    frame->Show();

    return true;
}

IMPLEMENT_APP(MyApp)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: