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 // Create the main frame window
44 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets Penguin Sample"),
45 wxDefaultPosition
, wxDefaultSize
);
48 if (wxFileExists(wxT("penguin.dxf.gz")))
49 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
51 if (wxFileExists(wxT("penguin.dxf")))
52 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
63 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
68 EVT_MENU(wxID_OPEN
, MyFrame::OnMenuFileOpen
)
69 EVT_MENU(wxID_EXIT
, MyFrame::OnMenuFileExit
)
70 EVT_MENU(wxID_HELP
, MyFrame::OnMenuHelpAbout
)
73 // MyFrame constructor
74 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
75 const wxSize
& size
, long style
)
76 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
78 SetIcon(wxIcon(sample_xpm
));
80 // Make the "File" menu
81 wxMenu
*fileMenu
= new wxMenu
;
82 fileMenu
->Append(wxID_OPEN
, wxT("&Open..."));
83 fileMenu
->AppendSeparator();
84 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tALT-X"));
85 // Make the "Help" menu
86 wxMenu
*helpMenu
= new wxMenu
;
87 helpMenu
->Append(wxID_HELP
, wxT("&About..."));
89 wxMenuBar
*menuBar
= new wxMenuBar
;
90 menuBar
->Append(fileMenu
, wxT("&File"));
91 menuBar
->Append(helpMenu
, wxT("&Help"));
94 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
95 wxSize(300, 300), wxSUNKEN_BORDER
);
98 // File|Open... command
99 void MyFrame::OnMenuFileOpen( wxCommandEvent
& WXUNUSED(event
) )
101 wxString filename
= wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
103 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
105 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
108 if (!filename
.IsEmpty())
110 m_canvas
->LoadDXF(filename
);
111 m_canvas
->Refresh(false);
116 void MyFrame::OnMenuFileExit( wxCommandEvent
& WXUNUSED(event
) )
118 // true is to force the frame to close
122 // Help|About... command
123 void MyFrame::OnMenuHelpAbout( wxCommandEvent
& WXUNUSED(event
) )
125 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
128 // ---------------------------------------------------------------------------
130 // ---------------------------------------------------------------------------
132 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
133 EVT_SIZE(TestGLCanvas::OnSize
)
134 EVT_PAINT(TestGLCanvas::OnPaint
)
135 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
136 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
139 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
140 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
141 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
)
143 m_gldata
.initialized
= false;
145 // initialize view matrix
146 m_gldata
.beginx
= 0.0f
;
147 m_gldata
.beginy
= 0.0f
;
148 m_gldata
.zoom
= 45.0f
;
149 trackball(m_gldata
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
152 TestGLCanvas::~TestGLCanvas()
156 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
158 // must always be here
162 if (!GetContext()) return;
168 if (!m_gldata
.initialized
)
171 ResetProjectionMode();
172 m_gldata
.initialized
= true;
176 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
177 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
181 glTranslatef( 0.0f
, 0.0f
, -20.0f
);
183 build_rotmatrix( m
, m_gldata
.quat
);
184 glMultMatrixf( &m
[0][0] );
195 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
197 // this is also necessary to update the context on some platforms
198 wxGLCanvas::OnSize(event
);
199 // Reset the OpenGL view aspect
200 ResetProjectionMode();
203 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
205 // Do nothing, to avoid flashing on MSW
208 // Load the DXF file. If the zlib support is compiled in wxWidgets,
209 // supports also the ".dxf.gz" gzip compressed files.
210 void TestGLCanvas::LoadDXF(const wxString
& filename
)
212 wxFileInputStream
stream(filename
);
216 if (filename
.Right(3).Lower() == wxT(".gz"))
218 wxZlibInputStream
zstream(stream
);
219 m_renderer
.Load(zstream
);
222 m_renderer
.Load(stream
);
227 m_renderer
.Load(stream
);
232 void TestGLCanvas::OnMouse(wxMouseEvent
& event
)
234 if (event
.Dragging())
236 wxSize
sz(GetClientSize());
238 /* drag in progress, simulate trackball */
241 (2.0*m_gldata
.beginx
- sz
.x
) / sz
.x
,
242 (sz
.y
- 2.0*m_gldata
.beginy
) / sz
.y
,
243 (2.0*event
.GetX() - sz
.x
) / sz
.x
,
244 (sz
.y
- 2.0*event
.GetY()) / sz
.y
);
246 add_quats(spin_quat
, m_gldata
.quat
, m_gldata
.quat
);
248 /* orientation has changed, redraw mesh */
252 m_gldata
.beginx
= event
.GetX();
253 m_gldata
.beginy
= event
.GetY();
256 void TestGLCanvas::InitGL()
258 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
261 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
263 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
266 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
268 /* remove back faces */
269 glDisable(GL_CULL_FACE
);
270 glEnable(GL_DEPTH_TEST
);
274 glShadeModel(GL_SMOOTH
);
275 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
276 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
279 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
280 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
281 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
282 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
285 glEnable(GL_LIGHTING
);
287 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
288 glEnable(GL_COLOR_MATERIAL
);
291 void TestGLCanvas::ResetProjectionMode()
294 GetClientSize(&w
, &h
);
300 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
301 glMatrixMode(GL_PROJECTION
);
303 gluPerspective(45.0f
, (GLfloat
)w
/h
, 1.0, 100.0);
304 glMatrixMode(GL_MODELVIEW
);