3 #include "wx/glcanvas.h"
5 #include "basic_gl_pane.h"
11 #include "OpenGL/glu.h"
12 #include "OpenGL/gl.h"
18 class MyApp: public wxApp
20 virtual bool OnInit();
31 DrawableThing* sprite;
35 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
36 frame = new wxFrame((wxFrame *)NULL, -1, wxT("Hello GL World"), wxPoint(50,50), wxSize(400,200));
38 int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0};
40 glPane = new BasicGLPane( (wxFrame*) frame, args);
41 sizer->Add(glPane, 1, wxEXPAND);
43 frame->SetSizer(sizer);
44 frame->SetAutoLayout(true);
50 BEGIN_EVENT_TABLE(BasicGLPane, wxGLCanvas)
51 EVT_MOTION(BasicGLPane::mouseMoved)
52 EVT_LEFT_DOWN(BasicGLPane::mouseDown)
53 EVT_LEFT_UP(BasicGLPane::mouseReleased)
54 EVT_RIGHT_DOWN(BasicGLPane::rightClick)
55 EVT_LEAVE_WINDOW(BasicGLPane::mouseLeftWindow)
56 EVT_SIZE(BasicGLPane::resized)
57 EVT_KEY_DOWN(BasicGLPane::keyPressed)
58 EVT_KEY_UP(BasicGLPane::keyReleased)
59 EVT_MOUSEWHEEL(BasicGLPane::mouseWheelMoved)
60 EVT_PAINT(BasicGLPane::render)
64 // some useful events to use
65 void BasicGLPane::mouseMoved(wxMouseEvent& event) {}
66 void BasicGLPane::mouseDown(wxMouseEvent& event) {}
67 void BasicGLPane::mouseWheelMoved(wxMouseEvent& event) {}
68 void BasicGLPane::mouseReleased(wxMouseEvent& event) {}
69 void BasicGLPane::rightClick(wxMouseEvent& event) {}
70 void BasicGLPane::mouseLeftWindow(wxMouseEvent& event) {}
71 void BasicGLPane::keyPressed(wxKeyEvent& event) {}
72 void BasicGLPane::keyReleased(wxKeyEvent& event) {}
74 BasicGLPane::BasicGLPane(wxFrame* parent, int* args) :
75 wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"), args)
79 void BasicGLPane::resized(wxSizeEvent& evt)
81 wxGLCanvas::OnSize(evt);
86 void BasicGLPane::prepare3DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
89 * Inits the OpenGL viewport for drawing in 3D.
92 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
93 glClearDepth(1.0f); // Depth Buffer Setup
94 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
95 glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
96 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
98 glEnable(GL_COLOR_MATERIAL);
100 glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
101 glMatrixMode(GL_PROJECTION);
104 float ratio_w_h = (float)(bottomrigth_x-topleft_x)/(float)(bottomrigth_y-topleft_y);
105 gluPerspective(45 /*view angle*/, ratio_w_h, 0.1 /*clip close*/, 200 /*clip far*/);
106 glMatrixMode(GL_MODELVIEW);
111 void BasicGLPane::prepare2DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
115 * Inits the OpenGL viewport for drawing in 2D
118 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
119 glEnable(GL_TEXTURE_2D); // textures
120 glEnable(GL_COLOR_MATERIAL);
122 glDisable(GL_DEPTH_TEST);
123 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
125 glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
126 glMatrixMode(GL_PROJECTION);
129 gluOrtho2D(topleft_x, bottomrigth_x, bottomrigth_y, topleft_y);
130 glMatrixMode(GL_MODELVIEW);
134 int BasicGLPane::getWidth()
139 int BasicGLPane::getHeight()
144 void BasicGLPane::render( wxPaintEvent& evt )
146 if(!IsShown()) return;
148 wxGLCanvas::SetCurrent();
153 image = new Image( wxT("myfile.png") );
154 sprite = new DrawableThing(image);
157 wxPaintDC(this); // only to be used in paint events. use wxClientDC to paint outside the paint event
159 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161 // render loaded image
162 prepare2DViewport(0,0,getWidth(), getHeight());