#include "wx/wx.h" #include "wx/sizer.h" #include "wx/glcanvas.h" #include "basic_gl_pane.h" #include "image.h" #include "drawable.h" // include OpenGL #ifdef __WXMAC__ #include "OpenGL/glu.h" #include "OpenGL/gl.h" #else #include #include #endif class MyApp: public wxApp { virtual bool OnInit(); wxFrame *frame; BasicGLPane * glPane; public: }; IMPLEMENT_APP(MyApp) Image* image = NULL; DrawableThing* sprite; bool MyApp::OnInit() { wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); frame = new wxFrame((wxFrame *)NULL, -1, char_to_wx("Hello GL World"), wxPoint(50,50), wxSize(400,200)); int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0}; glPane = new BasicGLPane( (wxFrame*) frame, args); sizer->Add(glPane, 1, wxEXPAND); frame->SetSizer(sizer); frame->SetAutoLayout(true); frame->Show(); return true; } BEGIN_EVENT_TABLE(BasicGLPane, wxGLCanvas) EVT_MOTION(BasicGLPane::mouseMoved) EVT_LEFT_DOWN(BasicGLPane::mouseDown) EVT_LEFT_UP(BasicGLPane::mouseReleased) EVT_RIGHT_DOWN(BasicGLPane::rightClick) EVT_LEAVE_WINDOW(BasicGLPane::mouseLeftWindow) EVT_SIZE(BasicGLPane::resized) EVT_KEY_DOWN(BasicGLPane::keyPressed) EVT_KEY_UP(BasicGLPane::keyReleased) EVT_MOUSEWHEEL(BasicGLPane::mouseWheelMoved) EVT_PAINT(BasicGLPane::render) END_EVENT_TABLE() // some useful events to use void BasicGLPane::mouseMoved(wxMouseEvent& event) {} void BasicGLPane::mouseDown(wxMouseEvent& event) {} void BasicGLPane::mouseWheelMoved(wxMouseEvent& event) {} void BasicGLPane::mouseReleased(wxMouseEvent& event) {} void BasicGLPane::rightClick(wxMouseEvent& event) {} void BasicGLPane::mouseLeftWindow(wxMouseEvent& event) {} void BasicGLPane::keyPressed(wxKeyEvent& event) {} void BasicGLPane::keyReleased(wxKeyEvent& event) {} BasicGLPane::BasicGLPane(wxFrame* parent, int* args) : wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, char_to_wx("GLCanvas"), args) { } void BasicGLPane::resized(wxSizeEvent& evt) { wxGLCanvas::OnSize(evt); Refresh(); } void BasicGLPane::prepare3DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y) { /* * Inits the OpenGL viewport for drawing in 3D. */ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_COLOR_MATERIAL); glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float ratio_w_h = (float)(bottomrigth_x-topleft_x)/(float)(bottomrigth_y-topleft_y); gluPerspective(45 /*view angle*/, ratio_w_h, 0.1 /*clip close*/, 200 /*clip far*/); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void BasicGLPane::prepare2DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y) { /* * Inits the OpenGL viewport for drawing in 2D */ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background glEnable(GL_TEXTURE_2D); // textures glEnable(GL_COLOR_MATERIAL); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(topleft_x, bottomrigth_x, bottomrigth_y, topleft_y); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int BasicGLPane::getWidth() { return GetSize().x; } int BasicGLPane::getHeight() { return GetSize().y; } void BasicGLPane::render( wxPaintEvent& evt ) { if(!IsShown()) return; wxGLCanvas::SetCurrent(); if(image == NULL) { image = new Image(char_to_wx("myfile.png")); sprite = new DrawableThing(image); } wxPaintDC(this); // only to be used in paint events. use wxClientDC to paint outside the paint event glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // render loaded image prepare2DViewport(0,0,getWidth(), getHeight()); sprite->render(); glFlush(); SwapBuffers(); }