wxWidgets builded from latest trunk.
Minimal example:
Code: Select all
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if !wxUSE_GLCANVAS
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif
#include "wx/glcanvas.h"
class MyApp : public wxApp {
public:
virtual bool OnInit() wxOVERRIDE;
};
wxIMPLEMENT_APP(MyApp);
class MyFrame : public wxFrame {
public:
MyFrame();
private:
~MyFrame();
void SetCurrent();
wxGLCanvas *glCanvas;
wxGLContext *glContext;
GLuint texture;
};
bool MyApp::OnInit() {
if (!wxApp::OnInit())
return false;
wxInitAllImageHandlers();
new MyFrame();
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "wxWidgets OpenGL Sample") {
wxGLAttributes dispAttrs;
dispAttrs.PlatformDefaults().MinRGBA(8, 8, 8, 8).DoubleBuffer().Depth(32).EndList();
glCanvas = new wxGLCanvas(this, dispAttrs, wxID_ANY,
wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE);
glContext = new wxGLContext(glCanvas);
SetCurrent();
glGenTextures(1, &texture);
wxString imagePath = wxString("/Users/winnydows/Dev/projects/OpenGL/1080p.png");
if (wxFileExists(imagePath)) {
wxImage img = wxImage(imagePath);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
}
glCanvas->Bind(wxEVT_PAINT, [=](wxPaintEvent& event) {
// This is required even though dc is not used otherwise.
wxPaintDC dc(glCanvas);
SetCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Matrix
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS); // Start Drawing
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, 1.0f); // Top Left Of The Texture and Quad
glEnd();
glCanvas->SwapBuffers();
});
glCanvas->Bind(wxEVT_SIZE, [=](wxSizeEvent& event) {
event.Skip();
SetCurrent();
int w, h;
glCanvas->GetClientSize(&w, &h);
glViewport(0, 0, w, h);
glCanvas->Refresh();
});
glCanvas->Bind(wxEVT_ERASE_BACKGROUND, [=](wxEraseEvent& event) {
// Do nothing, to avoid flashing on MSW
});
SetClientSize(640, 480);
Show();
}
MyFrame::~MyFrame() {
delete glContext;
}
void MyFrame::SetCurrent() {
glCanvas->SetCurrent(*glContext);
}