1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
5 // Modified by: Sandro Sigala
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
23 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
28 #include <OpenGL/glu.h>
33 #include "../../sample.xpm"
35 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
39 // `Main program' equivalent, creating windows and returning main app frame
42 if ( !wxApp::OnInit() )
45 // Create the main frame window
46 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets Penguin Sample"),
47 wxDefaultPosition
, wxDefaultSize
);
50 if (wxFileExists(wxT("penguin.dxf.gz")))
51 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
53 if (wxFileExists(wxT("penguin.dxf")))
54 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
65 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
70 EVT_MENU(wxID_OPEN
, MyFrame::OnMenuFileOpen
)
71 EVT_MENU(wxID_EXIT
, MyFrame::OnMenuFileExit
)
72 EVT_MENU(wxID_HELP
, MyFrame::OnMenuHelpAbout
)
75 // MyFrame constructor
76 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
77 const wxSize
& size
, long style
)
78 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
80 SetIcon(wxICON(sample
));
82 // Make the "File" menu
83 wxMenu
*fileMenu
= new wxMenu
;
84 fileMenu
->Append(wxID_OPEN
, wxT("&Open..."));
85 fileMenu
->AppendSeparator();
86 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tALT-X"));
87 // Make the "Help" menu
88 wxMenu
*helpMenu
= new wxMenu
;
89 helpMenu
->Append(wxID_HELP
, wxT("&About"));
91 wxMenuBar
*menuBar
= new wxMenuBar
;
92 menuBar
->Append(fileMenu
, wxT("&File"));
93 menuBar
->Append(helpMenu
, wxT("&Help"));
98 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
99 GetClientSize(), wxSUNKEN_BORDER
);
102 // File|Open... command
103 void MyFrame::OnMenuFileOpen( wxCommandEvent
& WXUNUSED(event
) )
105 wxString filename
= wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
107 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
109 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
112 if (!filename
.IsEmpty())
114 m_canvas
->LoadDXF(filename
);
115 m_canvas
->Refresh(false);
120 void MyFrame::OnMenuFileExit( wxCommandEvent
& WXUNUSED(event
) )
122 // true is to force the frame to close
126 // Help|About command
127 void MyFrame::OnMenuHelpAbout( wxCommandEvent
& WXUNUSED(event
) )
129 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
132 // ---------------------------------------------------------------------------
134 // ---------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
137 EVT_SIZE(TestGLCanvas::OnSize
)
138 EVT_PAINT(TestGLCanvas::OnPaint
)
139 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
140 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
143 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
148 const wxString
& name
)
149 : wxGLCanvas(parent
, id
, NULL
, pos
, size
,
150 style
| wxFULL_REPAINT_ON_RESIZE
, name
)
152 // Explicitly create a new rendering context instance for this canvas.
153 m_glRC
= new wxGLContext(this);
155 m_gldata
.initialized
= false;
157 // initialize view matrix
158 m_gldata
.beginx
= 0.0f
;
159 m_gldata
.beginy
= 0.0f
;
160 m_gldata
.zoom
= 45.0f
;
161 trackball(m_gldata
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
164 TestGLCanvas::~TestGLCanvas()
169 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
171 // must always be here
177 if (!m_gldata
.initialized
)
180 ResetProjectionMode();
181 m_gldata
.initialized
= true;
185 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
186 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
190 glTranslatef( 0.0f
, 0.0f
, -20.0f
);
192 build_rotmatrix( m
, m_gldata
.quat
);
193 glMultMatrixf( &m
[0][0] );
204 void TestGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
206 // Reset the OpenGL view aspect.
207 // This is OK only because there is only one canvas that uses the context.
208 // See the cube sample for that case that multiple canvases are made current with one context.
209 ResetProjectionMode();
212 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
214 // Do nothing, to avoid flashing on MSW
217 // Load the DXF file. If the zlib support is compiled in wxWidgets,
218 // supports also the ".dxf.gz" gzip compressed files.
219 void TestGLCanvas::LoadDXF(const wxString
& filename
)
221 wxFileInputStream
stream(filename
);
225 if (filename
.Right(3).Lower() == wxT(".gz"))
227 wxZlibInputStream
zstream(stream
);
228 m_renderer
.Load(zstream
);
231 m_renderer
.Load(stream
);
236 m_renderer
.Load(stream
);
241 void TestGLCanvas::OnMouse(wxMouseEvent
& event
)
243 if (event
.Dragging())
245 wxSize
sz(GetClientSize());
247 /* drag in progress, simulate trackball */
250 (2.0*m_gldata
.beginx
- sz
.x
) / sz
.x
,
251 (sz
.y
- 2.0*m_gldata
.beginy
) / sz
.y
,
252 (2.0*event
.GetX() - sz
.x
) / sz
.x
,
253 (sz
.y
- 2.0*event
.GetY()) / sz
.y
);
255 add_quats(spin_quat
, m_gldata
.quat
, m_gldata
.quat
);
257 /* orientation has changed, redraw mesh */
261 m_gldata
.beginx
= event
.GetX();
262 m_gldata
.beginy
= event
.GetY();
265 void TestGLCanvas::InitGL()
267 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
270 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
272 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
275 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
277 /* remove back faces */
278 glEnable(GL_CULL_FACE
);
279 glEnable(GL_DEPTH_TEST
);
283 glShadeModel(GL_SMOOTH
);
284 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
285 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
288 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
289 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
290 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
291 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
294 glEnable(GL_LIGHTING
);
296 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
297 glEnable(GL_COLOR_MATERIAL
);
300 void TestGLCanvas::ResetProjectionMode()
302 if ( !IsShownOnScreen() )
305 // This is normally only necessary if there is more than one wxGLCanvas
306 // or more than one wxGLContext in the application.
310 GetClientSize(&w
, &h
);
312 // It's up to the application code to update the OpenGL viewport settings.
313 // In order to avoid extensive context switching, consider doing this in
314 // OnPaint() rather than here, though.
315 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
317 glMatrixMode(GL_PROJECTION
);
319 gluPerspective(45.0f
, (GLfloat
)w
/h
, 1.0, 100.0);
320 glMatrixMode(GL_MODELVIEW
);