| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: penguin.cpp |
| 3 | // Purpose: wxGLCanvas demo program |
| 4 | // Author: Robert Roebling |
| 5 | // Modified by: Sandro Sigala |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Robert Roebling |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifdef __BORLANDC__ |
| 16 | #pragma hdrstop |
| 17 | #endif |
| 18 | |
| 19 | #ifndef WX_PRECOMP |
| 20 | #include "wx/wx.h" |
| 21 | #endif |
| 22 | |
| 23 | #if !wxUSE_GLCANVAS |
| 24 | #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library" |
| 25 | #endif |
| 26 | |
| 27 | #include "penguin.h" |
| 28 | #ifdef __DARWIN__ |
| 29 | #include <OpenGL/glu.h> |
| 30 | #else |
| 31 | #include <GL/glu.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "../../sample.xpm" |
| 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | // MyApp |
| 38 | // --------------------------------------------------------------------------- |
| 39 | |
| 40 | // `Main program' equivalent, creating windows and returning main app frame |
| 41 | bool MyApp::OnInit() |
| 42 | { |
| 43 | if ( !wxApp::OnInit() ) |
| 44 | return false; |
| 45 | |
| 46 | // Create the main frame window |
| 47 | MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"), |
| 48 | wxDefaultPosition, wxDefaultSize); |
| 49 | |
| 50 | #if wxUSE_ZLIB |
| 51 | if (wxFileExists(wxT("penguin.dxf.gz"))) |
| 52 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz")); |
| 53 | #else |
| 54 | if (wxFileExists(wxT("penguin.dxf"))) |
| 55 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf")); |
| 56 | #endif |
| 57 | |
| 58 | /* Show the frame */ |
| 59 | frame->Show(true); |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | IMPLEMENT_APP(MyApp) |
| 65 | |
| 66 | // --------------------------------------------------------------------------- |
| 67 | // MyFrame |
| 68 | // --------------------------------------------------------------------------- |
| 69 | |
| 70 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 71 | EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen) |
| 72 | EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit) |
| 73 | EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout) |
| 74 | END_EVENT_TABLE() |
| 75 | |
| 76 | // MyFrame constructor |
| 77 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, |
| 78 | const wxSize& size, long style) |
| 79 | : wxFrame(frame, wxID_ANY, title, pos, size, style) |
| 80 | { |
| 81 | SetIcon(wxIcon(sample_xpm)); |
| 82 | |
| 83 | // Make the "File" menu |
| 84 | wxMenu *fileMenu = new wxMenu; |
| 85 | fileMenu->Append(wxID_OPEN, wxT("&Open...")); |
| 86 | fileMenu->AppendSeparator(); |
| 87 | fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X")); |
| 88 | // Make the "Help" menu |
| 89 | wxMenu *helpMenu = new wxMenu; |
| 90 | helpMenu->Append(wxID_HELP, wxT("&About...")); |
| 91 | |
| 92 | wxMenuBar *menuBar = new wxMenuBar; |
| 93 | menuBar->Append(fileMenu, wxT("&File")); |
| 94 | menuBar->Append(helpMenu, wxT("&Help")); |
| 95 | SetMenuBar(menuBar); |
| 96 | |
| 97 | m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition, |
| 98 | wxSize(300, 300), wxSUNKEN_BORDER); |
| 99 | } |
| 100 | |
| 101 | // File|Open... command |
| 102 | void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) ) |
| 103 | { |
| 104 | wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""), |
| 105 | #if wxUSE_ZLIB |
| 106 | wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"), |
| 107 | #else |
| 108 | wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"), |
| 109 | #endif |
| 110 | wxFD_OPEN); |
| 111 | if (!filename.IsEmpty()) |
| 112 | { |
| 113 | m_canvas->LoadDXF(filename); |
| 114 | m_canvas->Refresh(false); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // File|Exit command |
| 119 | void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) ) |
| 120 | { |
| 121 | // true is to force the frame to close |
| 122 | Close(true); |
| 123 | } |
| 124 | |
| 125 | // Help|About... command |
| 126 | void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) ) |
| 127 | { |
| 128 | wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al")); |
| 129 | } |
| 130 | |
| 131 | // --------------------------------------------------------------------------- |
| 132 | // TestGLCanvas |
| 133 | // --------------------------------------------------------------------------- |
| 134 | |
| 135 | BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) |
| 136 | EVT_SIZE(TestGLCanvas::OnSize) |
| 137 | EVT_PAINT(TestGLCanvas::OnPaint) |
| 138 | EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground) |
| 139 | EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse) |
| 140 | END_EVENT_TABLE() |
| 141 | |
| 142 | TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id, |
| 143 | const wxPoint& pos, const wxSize& size, long style, const wxString& name) |
| 144 | : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name) |
| 145 | { |
| 146 | m_gldata.initialized = false; |
| 147 | |
| 148 | // initialize view matrix |
| 149 | m_gldata.beginx = 0.0f; |
| 150 | m_gldata.beginy = 0.0f; |
| 151 | m_gldata.zoom = 45.0f; |
| 152 | trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f); |
| 153 | } |
| 154 | |
| 155 | TestGLCanvas::~TestGLCanvas() |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
| 160 | { |
| 161 | // must always be here |
| 162 | wxPaintDC dc(this); |
| 163 | |
| 164 | #ifndef __WXMOTIF__ |
| 165 | if (!GetContext()) return; |
| 166 | #endif |
| 167 | |
| 168 | SetCurrent(); |
| 169 | |
| 170 | // Initialize OpenGL |
| 171 | if (!m_gldata.initialized) |
| 172 | { |
| 173 | InitGL(); |
| 174 | ResetProjectionMode(); |
| 175 | m_gldata.initialized = true; |
| 176 | } |
| 177 | |
| 178 | // Clear |
| 179 | glClearColor( 0.3f, 0.4f, 0.6f, 1.0f ); |
| 180 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 181 | |
| 182 | // Transformations |
| 183 | glLoadIdentity(); |
| 184 | glTranslatef( 0.0f, 0.0f, -20.0f ); |
| 185 | GLfloat m[4][4]; |
| 186 | build_rotmatrix( m, m_gldata.quat ); |
| 187 | glMultMatrixf( &m[0][0] ); |
| 188 | |
| 189 | m_renderer.Render(); |
| 190 | |
| 191 | // Flush |
| 192 | glFlush(); |
| 193 | |
| 194 | // Swap |
| 195 | SwapBuffers(); |
| 196 | } |
| 197 | |
| 198 | void TestGLCanvas::OnSize(wxSizeEvent& event) |
| 199 | { |
| 200 | // this is also necessary to update the context on some platforms |
| 201 | wxGLCanvas::OnSize(event); |
| 202 | // Reset the OpenGL view aspect |
| 203 | ResetProjectionMode(); |
| 204 | } |
| 205 | |
| 206 | void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
| 207 | { |
| 208 | // Do nothing, to avoid flashing on MSW |
| 209 | } |
| 210 | |
| 211 | // Load the DXF file. If the zlib support is compiled in wxWidgets, |
| 212 | // supports also the ".dxf.gz" gzip compressed files. |
| 213 | void TestGLCanvas::LoadDXF(const wxString& filename) |
| 214 | { |
| 215 | wxFileInputStream stream(filename); |
| 216 | if (stream.Ok()) |
| 217 | #if wxUSE_ZLIB |
| 218 | { |
| 219 | if (filename.Right(3).Lower() == wxT(".gz")) |
| 220 | { |
| 221 | wxZlibInputStream zstream(stream); |
| 222 | m_renderer.Load(zstream); |
| 223 | } else |
| 224 | { |
| 225 | m_renderer.Load(stream); |
| 226 | } |
| 227 | } |
| 228 | #else |
| 229 | { |
| 230 | m_renderer.Load(stream); |
| 231 | } |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | void TestGLCanvas::OnMouse(wxMouseEvent& event) |
| 236 | { |
| 237 | if (event.Dragging()) |
| 238 | { |
| 239 | wxSize sz(GetClientSize()); |
| 240 | |
| 241 | /* drag in progress, simulate trackball */ |
| 242 | float spin_quat[4]; |
| 243 | trackball(spin_quat, |
| 244 | (2.0*m_gldata.beginx - sz.x) / sz.x, |
| 245 | (sz.y - 2.0*m_gldata.beginy) / sz.y, |
| 246 | (2.0*event.GetX() - sz.x) / sz.x, |
| 247 | (sz.y - 2.0*event.GetY()) / sz.y); |
| 248 | |
| 249 | add_quats(spin_quat, m_gldata.quat, m_gldata.quat); |
| 250 | |
| 251 | /* orientation has changed, redraw mesh */ |
| 252 | Refresh(false); |
| 253 | } |
| 254 | |
| 255 | m_gldata.beginx = event.GetX(); |
| 256 | m_gldata.beginy = event.GetY(); |
| 257 | } |
| 258 | |
| 259 | void TestGLCanvas::InitGL() |
| 260 | { |
| 261 | static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f }; |
| 262 | |
| 263 | // white light |
| 264 | static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f }; |
| 265 | |
| 266 | static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f }; |
| 267 | |
| 268 | // cold blue light |
| 269 | static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f }; |
| 270 | |
| 271 | /* remove back faces */ |
| 272 | glDisable(GL_CULL_FACE); |
| 273 | glEnable(GL_DEPTH_TEST); |
| 274 | |
| 275 | /* speedups */ |
| 276 | glEnable(GL_DITHER); |
| 277 | glShadeModel(GL_SMOOTH); |
| 278 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); |
| 279 | glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); |
| 280 | |
| 281 | /* light */ |
| 282 | glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); |
| 283 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); |
| 284 | glLightfv(GL_LIGHT1, GL_POSITION, light1_pos); |
| 285 | glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color); |
| 286 | glEnable(GL_LIGHT0); |
| 287 | glEnable(GL_LIGHT1); |
| 288 | glEnable(GL_LIGHTING); |
| 289 | |
| 290 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); |
| 291 | glEnable(GL_COLOR_MATERIAL); |
| 292 | } |
| 293 | |
| 294 | void TestGLCanvas::ResetProjectionMode() |
| 295 | { |
| 296 | int w, h; |
| 297 | GetClientSize(&w, &h); |
| 298 | #ifndef __WXMOTIF__ |
| 299 | if ( GetContext() ) |
| 300 | #endif |
| 301 | { |
| 302 | SetCurrent(); |
| 303 | glViewport(0, 0, (GLint) w, (GLint) h); |
| 304 | glMatrixMode(GL_PROJECTION); |
| 305 | glLoadIdentity(); |
| 306 | gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0); |
| 307 | glMatrixMode(GL_MODELVIEW); |
| 308 | glLoadIdentity(); |
| 309 | } |
| 310 | } |