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
));
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"));
99 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
100 GetClientSize(), wxSUNKEN_BORDER
);
103 // File|Open... command
104 void MyFrame::OnMenuFileOpen( wxCommandEvent
& WXUNUSED(event
) )
106 wxString filename
= wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
108 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
110 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
113 if (!filename
.IsEmpty())
115 m_canvas
->LoadDXF(filename
);
116 m_canvas
->Refresh(false);
121 void MyFrame::OnMenuFileExit( wxCommandEvent
& WXUNUSED(event
) )
123 // true is to force the frame to close
127 // Help|About... command
128 void MyFrame::OnMenuHelpAbout( wxCommandEvent
& WXUNUSED(event
) )
130 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
133 // ---------------------------------------------------------------------------
135 // ---------------------------------------------------------------------------
137 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
138 EVT_SIZE(TestGLCanvas::OnSize
)
139 EVT_PAINT(TestGLCanvas::OnPaint
)
140 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
141 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
144 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
149 const wxString
& name
)
150 : wxGLCanvas(parent
, id
, NULL
, pos
, size
,
151 style
| wxFULL_REPAINT_ON_RESIZE
, name
)
153 // Explicitly create a new rendering context instance for this canvas.
154 m_glRC
= new wxGLContext(this);
156 // Make the new context current (activate it for use) with this canvas.
159 m_gldata
.initialized
= false;
161 // initialize view matrix
162 m_gldata
.beginx
= 0.0f
;
163 m_gldata
.beginy
= 0.0f
;
164 m_gldata
.zoom
= 45.0f
;
165 trackball(m_gldata
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
168 TestGLCanvas::~TestGLCanvas()
173 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
175 // must always be here
181 if (!m_gldata
.initialized
)
184 ResetProjectionMode();
185 m_gldata
.initialized
= true;
189 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
190 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
194 glTranslatef( 0.0f
, 0.0f
, -20.0f
);
196 build_rotmatrix( m
, m_gldata
.quat
);
197 glMultMatrixf( &m
[0][0] );
208 void TestGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
210 // Reset the OpenGL view aspect.
211 // This is OK only because there is only one canvas that uses the context.
212 // See the cube sample for that case that multiple canvases are made current with one context.
213 ResetProjectionMode();
216 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
218 // Do nothing, to avoid flashing on MSW
221 // Load the DXF file. If the zlib support is compiled in wxWidgets,
222 // supports also the ".dxf.gz" gzip compressed files.
223 void TestGLCanvas::LoadDXF(const wxString
& filename
)
225 wxFileInputStream
stream(filename
);
229 if (filename
.Right(3).Lower() == wxT(".gz"))
231 wxZlibInputStream
zstream(stream
);
232 m_renderer
.Load(zstream
);
235 m_renderer
.Load(stream
);
240 m_renderer
.Load(stream
);
245 void TestGLCanvas::OnMouse(wxMouseEvent
& event
)
247 if (event
.Dragging())
249 wxSize
sz(GetClientSize());
251 /* drag in progress, simulate trackball */
254 (2.0*m_gldata
.beginx
- sz
.x
) / sz
.x
,
255 (sz
.y
- 2.0*m_gldata
.beginy
) / sz
.y
,
256 (2.0*event
.GetX() - sz
.x
) / sz
.x
,
257 (sz
.y
- 2.0*event
.GetY()) / sz
.y
);
259 add_quats(spin_quat
, m_gldata
.quat
, m_gldata
.quat
);
261 /* orientation has changed, redraw mesh */
265 m_gldata
.beginx
= event
.GetX();
266 m_gldata
.beginy
= event
.GetY();
269 void TestGLCanvas::InitGL()
271 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
274 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
276 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
279 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
281 /* remove back faces */
282 glDisable(GL_CULL_FACE
);
283 glEnable(GL_DEPTH_TEST
);
287 glShadeModel(GL_SMOOTH
);
288 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
289 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
292 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
293 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
294 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
295 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
298 glEnable(GL_LIGHTING
);
300 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
301 glEnable(GL_COLOR_MATERIAL
);
304 void TestGLCanvas::ResetProjectionMode()
306 // This is normally only necessary if there is more than one wxGLCanvas
307 // or more than one wxGLContext in the application.
311 GetClientSize(&w
, &h
);
313 // It's up to the application code to update the OpenGL viewport settings.
314 // In order to avoid extensive context switching, consider doing this in
315 // OnPaint() rather than here, though.
316 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
318 glMatrixMode(GL_PROJECTION
);
320 gluPerspective(45.0f
, (GLfloat
)w
/h
, 1.0, 100.0);
321 glMatrixMode(GL_MODELVIEW
);