1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
29 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
35 # include <OpenGL/glu.h>
43 #define VIEW_ASPECT 1.3
45 // `Main program' equivalent, creating windows and returning main app frame
49 // Create the main frame window
50 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Penguin Sample"),
51 wxDefaultPosition
, wxDefaultSize
);
54 wxMenu
*fileMenu
= new wxMenu
;
56 fileMenu
->Append(wxID_EXIT
, wxT("E&xit"));
57 wxMenuBar
*menuBar
= new wxMenuBar
;
58 menuBar
->Append(fileMenu
, wxT("&File"));
59 frame
->SetMenuBar(menuBar
);
61 frame
->SetCanvas( new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
62 wxSize(200, 200), wxSUNKEN_BORDER
) );
64 /* Load file wiht mesh data */
65 frame
->GetCanvas()->LoadLWO( wxT("penguin.lwo") );
75 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
76 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
79 /* My frame constructor */
80 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
81 const wxSize
& size
, long style
)
82 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
87 /* Intercept menu commands */
88 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
90 // true is to force the frame to close
94 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
95 EVT_SIZE(TestGLCanvas::OnSize
)
96 EVT_PAINT(TestGLCanvas::OnPaint
)
97 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
98 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
101 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
102 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
103 : wxGLCanvas(parent
, id
, pos
, size
, style
, name
)
108 TestGLCanvas::~TestGLCanvas()
111 lw_object_free(info
.lwobject
);
114 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
116 /* must always be here */
120 if (!GetContext()) return;
129 info
.do_init
= false;
133 glMatrixMode( GL_PROJECTION
);
135 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1.0, 100.0 );
136 glMatrixMode( GL_MODELVIEW
);
139 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
140 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
145 glTranslatef( 0.0f
, 0.0f
, -30.0f
);
146 build_rotmatrix( m
,info
.quat
);
147 glMultMatrixf( &m
[0][0] );
150 lw_object_show( info
.lwobject
);
159 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
161 // this is also necessary to update the context on some platforms
162 wxGLCanvas::OnSize(event
);
164 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
166 GetClientSize(&w
, &h
);
172 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
176 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
178 /* Do nothing, to avoid flashing on MSW */
181 void TestGLCanvas::LoadLWO(const wxString
&filename
)
183 /* test if lightwave object */
184 if (!lw_is_lwobject(filename
.mb_str())) return;
186 /* read lightwave object */
187 lwObject
*lwobject
= lw_object_read(filename
.mb_str());
190 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
192 /* set up mesh info */
194 info
.lwobject
= lwobject
;
198 trackball( info
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
201 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
204 if ( event
.Dragging() )
206 wxSize
sz( GetClientSize() );
208 /* drag in progress, simulate trackball */
211 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
212 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
213 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
214 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
216 add_quats( spin_quat
, info
.quat
, info
.quat
);
218 /* orientation has changed, redraw mesh */
222 info
.beginx
= event
.GetX();
223 info
.beginy
= event
.GetY();
226 void TestGLCanvas::InitGL()
228 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
231 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
233 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
236 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
238 /* remove back faces */
239 glDisable(GL_CULL_FACE
);
240 glEnable(GL_DEPTH_TEST
);
244 glShadeModel(GL_SMOOTH
);
245 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
246 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
249 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
250 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
251 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
252 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
255 glEnable(GL_LIGHTING
);
257 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
258 glEnable(GL_COLOR_MATERIAL
);