1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
5 // Modified by: Sandro Sigala
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
29 #include <OpenGL/glu.h>
34 #include "../../sample.xpm"
36 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
40 // `Main program' equivalent, creating windows and returning main app frame
43 if ( !wxApp::OnInit() )
46 // Create the main frame window
47 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets Penguin Sample"),
48 wxDefaultPosition
, wxDefaultSize
);
51 if (wxFileExists(wxT("penguin.dxf.gz")))
52 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
54 if (wxFileExists(wxT("penguin.dxf")))
55 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
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
)
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
)
81 SetIcon(wxIcon(sample_xpm
));
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..."));
92 wxMenuBar
*menuBar
= new wxMenuBar
;
93 menuBar
->Append(fileMenu
, wxT("&File"));
94 menuBar
->Append(helpMenu
, wxT("&Help"));
97 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
98 wxSize(300, 300), wxSUNKEN_BORDER
);
101 // File|Open... command
102 void MyFrame::OnMenuFileOpen( wxCommandEvent
& WXUNUSED(event
) )
104 wxString filename
= wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
106 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
108 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
111 if (!filename
.IsEmpty())
113 m_canvas
->LoadDXF(filename
);
114 m_canvas
->Refresh(false);
119 void MyFrame::OnMenuFileExit( wxCommandEvent
& WXUNUSED(event
) )
121 // true is to force the frame to close
125 // Help|About... command
126 void MyFrame::OnMenuHelpAbout( wxCommandEvent
& WXUNUSED(event
) )
128 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
131 // ---------------------------------------------------------------------------
133 // ---------------------------------------------------------------------------
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
)
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
)
146 m_gldata
.initialized
= false;
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
);
155 TestGLCanvas::~TestGLCanvas()
159 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
161 // must always be here
165 if (!GetContext()) return;
171 if (!m_gldata
.initialized
)
174 ResetProjectionMode();
175 m_gldata
.initialized
= true;
179 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
180 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
184 glTranslatef( 0.0f
, 0.0f
, -20.0f
);
186 build_rotmatrix( m
, m_gldata
.quat
);
187 glMultMatrixf( &m
[0][0] );
198 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
200 // this is also necessary to update the context on some platforms
201 wxGLCanvas::OnSize(event
);
202 // Reset the OpenGL view aspect
203 ResetProjectionMode();
206 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
208 // Do nothing, to avoid flashing on MSW
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
)
215 wxFileInputStream
stream(filename
);
219 if (filename
.Right(3).Lower() == wxT(".gz"))
221 wxZlibInputStream
zstream(stream
);
222 m_renderer
.Load(zstream
);
225 m_renderer
.Load(stream
);
230 m_renderer
.Load(stream
);
235 void TestGLCanvas::OnMouse(wxMouseEvent
& event
)
237 if (event
.Dragging())
239 wxSize
sz(GetClientSize());
241 /* drag in progress, simulate trackball */
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
);
249 add_quats(spin_quat
, m_gldata
.quat
, m_gldata
.quat
);
251 /* orientation has changed, redraw mesh */
255 m_gldata
.beginx
= event
.GetX();
256 m_gldata
.beginy
= event
.GetY();
259 void TestGLCanvas::InitGL()
261 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
264 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
266 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
269 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
271 /* remove back faces */
272 glDisable(GL_CULL_FACE
);
273 glEnable(GL_DEPTH_TEST
);
277 glShadeModel(GL_SMOOTH
);
278 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
279 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
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
);
288 glEnable(GL_LIGHTING
);
290 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
291 glEnable(GL_COLOR_MATERIAL
);
294 void TestGLCanvas::ResetProjectionMode()
297 GetClientSize(&w
, &h
);
303 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
304 glMatrixMode(GL_PROJECTION
);
306 gluPerspective(45.0f
, (GLfloat
)w
/h
, 1.0, 100.0);
307 glMatrixMode(GL_MODELVIEW
);