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"
30 # include <OpenGL/glu.h>
38 #include "../../sample.xpm"
40 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 // `Main program' equivalent, creating windows and returning main app frame
47 // Create the main frame window
48 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets Penguin Sample"),
49 wxDefaultPosition
, wxDefaultSize
);
52 if (wxFileExists(wxT("penguin.dxf.gz")))
53 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
55 if (wxFileExists(wxT("penguin.dxf")))
56 frame
->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
67 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
72 EVT_MENU(wxID_OPEN
, MyFrame::OnMenuFileOpen
)
73 EVT_MENU(wxID_EXIT
, MyFrame::OnMenuFileExit
)
74 EVT_MENU(wxID_HELP
, MyFrame::OnMenuHelpAbout
)
77 // MyFrame constructor
78 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
79 const wxSize
& size
, long style
)
80 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
82 SetIcon(wxIcon(sample_xpm
));
84 // Make the "File" menu
85 wxMenu
*fileMenu
= new wxMenu
;
86 fileMenu
->Append(wxID_OPEN
, wxT("&Open..."));
87 fileMenu
->AppendSeparator();
88 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tALT-X"));
89 // Make the "Help" menu
90 wxMenu
*helpMenu
= new wxMenu
;
91 helpMenu
->Append(wxID_HELP
, wxT("&About..."));
93 wxMenuBar
*menuBar
= new wxMenuBar
;
94 menuBar
->Append(fileMenu
, wxT("&File"));
95 menuBar
->Append(helpMenu
, wxT("&Help"));
98 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
99 wxSize(300, 300), 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
, wxWindowID id
,
144 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
145 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
)
147 m_gldata
.initialized
= false;
149 // initialize view matrix
150 m_gldata
.beginx
= 0.0f
;
151 m_gldata
.beginy
= 0.0f
;
152 m_gldata
.zoom
= 45.0f
;
153 trackball(m_gldata
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
156 TestGLCanvas::~TestGLCanvas()
160 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
162 // must always be here
166 if (!GetContext()) return;
172 if (!m_gldata
.initialized
)
175 ResetProjectionMode();
176 m_gldata
.initialized
= true;
180 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
181 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
185 glTranslatef( 0.0f
, 0.0f
, -20.0f
);
187 build_rotmatrix( m
, m_gldata
.quat
);
188 glMultMatrixf( &m
[0][0] );
199 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
201 // this is also necessary to update the context on some platforms
202 wxGLCanvas::OnSize(event
);
203 // Reset the OpenGL view aspect
204 ResetProjectionMode();
207 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
209 // Do nothing, to avoid flashing on MSW
212 // Load the DXF file. If the zlib support is compiled in wxWidgets,
213 // supports also the ".dxf.gz" gzip compressed files.
214 void TestGLCanvas::LoadDXF(const wxString
& filename
)
216 wxFileInputStream
stream(filename
);
220 if (filename
.Right(3).Lower() == wxT(".gz"))
222 wxZlibInputStream
zstream(stream
);
223 m_renderer
.Load(zstream
);
226 m_renderer
.Load(stream
);
231 m_renderer
.Load(stream
);
236 void TestGLCanvas::OnMouse(wxMouseEvent
& event
)
238 if (event
.Dragging())
240 wxSize
sz(GetClientSize());
242 /* drag in progress, simulate trackball */
245 (2.0*m_gldata
.beginx
- sz
.x
) / sz
.x
,
246 (sz
.y
- 2.0*m_gldata
.beginy
) / sz
.y
,
247 (2.0*event
.GetX() - sz
.x
) / sz
.x
,
248 (sz
.y
- 2.0*event
.GetY()) / sz
.y
);
250 add_quats(spin_quat
, m_gldata
.quat
, m_gldata
.quat
);
252 /* orientation has changed, redraw mesh */
256 m_gldata
.beginx
= event
.GetX();
257 m_gldata
.beginy
= event
.GetY();
260 void TestGLCanvas::InitGL()
262 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
265 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
267 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
270 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
272 /* remove back faces */
273 glDisable(GL_CULL_FACE
);
274 glEnable(GL_DEPTH_TEST
);
278 glShadeModel(GL_SMOOTH
);
279 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
280 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
283 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
284 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
285 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
286 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
289 glEnable(GL_LIGHTING
);
291 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
292 glEnable(GL_COLOR_MATERIAL
);
295 void TestGLCanvas::ResetProjectionMode()
298 GetClientSize(&w
, &h
);
304 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
305 glMatrixMode(GL_PROJECTION
);
307 gluPerspective(45.0f
, (GLfloat
)w
/h
, 1.0, 100.0);
308 glMatrixMode(GL_MODELVIEW
);