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"
31 #define VIEW_ASPECT 1.3
33 /* `Main program' equivalent, creating windows and returning main app frame */
34 bool MyApp::OnInit(void)
37 /* Create the main frame window */
38 MyFrame
*frame
= new MyFrame(NULL
, "wxWindows OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
41 wxMenu
*fileMenu
= new wxMenu
;
43 fileMenu
->Append(wxID_EXIT
, "E&xit");
44 wxMenuBar
*menuBar
= new wxMenuBar
;
45 menuBar
->Append(fileMenu
, "&File");
46 frame
->SetMenuBar(menuBar
);
48 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));
50 /* Load file wiht mesh data */
51 frame
->m_canvas
->LoadLWO( "penguin.lwo" );
61 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
62 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
65 /* My frame constructor */
66 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
67 const wxSize
& size
, long style
):
68 wxFrame(frame
, -1, title
, pos
, size
, style
)
73 /* Intercept menu commands */
74 void MyFrame::OnExit(wxCommandEvent
& event
)
79 bool MyFrame::OnClose(void)
84 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
85 EVT_SIZE(TestGLCanvas::OnSize
)
86 EVT_PAINT(TestGLCanvas::OnPaint
)
87 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
88 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
91 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
92 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
93 wxGLCanvas(parent
, id
, pos
, size
, style
, name
)
98 TestGLCanvas::~TestGLCanvas(void)
101 lw_object_free(info
.lwobject
);
104 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
106 /* must always be here */
109 if (!GetContext()) return;
113 /* initialize OpenGL */
114 if (info
.do_init
== TRUE
)
117 info
.do_init
= FALSE
;
121 glMatrixMode( GL_PROJECTION
);
123 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1, 100 );
124 glMatrixMode( GL_MODELVIEW
);
127 glClearColor( .3, .4, .6, 1 );
128 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
130 /* transformations */
133 glTranslatef( 0, 0, -30 );
134 build_rotmatrix( m
,info
.quat
);
135 glMultMatrixf( &m
[0][0] );
138 lw_object_show( info
.lwobject
);
147 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
150 GetClientSize(& width
, & height
);
155 glViewport(0, 0, width
, height
);
159 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
161 /* Do nothing, to avoid flashing on MSW */
164 void TestGLCanvas::LoadLWO(const wxString
&filename
)
166 /* test if lightwave object */
167 if (!lw_is_lwobject(filename
)) return;
169 /* read lightwave object */
170 lwObject
*lwobject
= lw_object_read(filename
);
173 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
175 /* set up mesh info */
177 info
.lwobject
= lwobject
;
181 trackball( info
.quat
, 0.0, 0.0, 0.0, 0.0 );
184 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
186 wxSize
sz(GetClientSize());
187 if (event
.Dragging())
189 /* drag in progress, simulate trackball */
192 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
193 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
194 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
195 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
197 add_quats( spin_quat
, info
.quat
, info
.quat
);
199 /* orientation has changed, redraw mesh */
203 info
.beginx
= event
.GetX();
204 info
.beginy
= event
.GetY();
207 void TestGLCanvas::InitGL(void)
209 GLfloat light0_pos
[4] = { -50.0, 50.0, 0.0, 0.0 };
210 GLfloat light0_color
[4] = { .6, .6, .6, 1.0 }; /* white light */
211 GLfloat light1_pos
[4] = { 50.0, 50.0, 0.0, 0.0 };
212 GLfloat light1_color
[4] = { .4, .4, 1, 1.0 }; /* cold blue light */
214 /* remove back faces */
215 glDisable(GL_CULL_FACE
);
216 glEnable(GL_DEPTH_TEST
);
220 glShadeModel(GL_SMOOTH
);
221 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
222 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
225 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
226 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
227 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
228 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
231 glEnable(GL_LIGHTING
);
233 glColorMaterial(GL_FRONT_AND_BACK
,GL_AMBIENT_AND_DIFFUSE
);
234 glEnable(GL_COLOR_MATERIAL
);