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 # include <OpenGL/glu.h>
39 #define VIEW_ASPECT 1.3
41 // `Main program' equivalent, creating windows and returning main app frame
45 // Create the main frame window
46 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWindows OpenGL Penguin Sample"),
47 wxDefaultPosition
, wxDefaultSize
);
50 wxMenu
*fileMenu
= new wxMenu
;
52 fileMenu
->Append(wxID_EXIT
, wxT("E&xit"));
53 wxMenuBar
*menuBar
= new wxMenuBar
;
54 menuBar
->Append(fileMenu
, wxT("&File"));
55 frame
->SetMenuBar(menuBar
);
58 frame
->SetCanvas( new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
59 wxSize(200, 200), wxSUNKEN_BORDER
) );
61 /* Load file wiht mesh data */
62 frame
->GetCanvas()->LoadLWO( wxT("penguin.lwo") );
70 wxMessageBox( _T("This sample has to be compiled with wxUSE_GLCANVAS"),
71 _T("Building error"), wxOK
);
80 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
81 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
84 /* My frame constructor */
85 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
86 const wxSize
& size
, long style
)
87 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
94 /* Intercept menu commands */
95 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
97 // true is to force the frame to close
103 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
104 EVT_SIZE(TestGLCanvas::OnSize
)
105 EVT_PAINT(TestGLCanvas::OnPaint
)
106 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
107 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
110 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
111 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
112 : wxGLCanvas(parent
, id
, pos
, size
, style
, name
)
117 TestGLCanvas::~TestGLCanvas()
120 lw_object_free(info
.lwobject
);
123 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
125 /* must always be here */
129 if (!GetContext()) return;
138 info
.do_init
= false;
142 glMatrixMode( GL_PROJECTION
);
144 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1.0, 100.0 );
145 glMatrixMode( GL_MODELVIEW
);
148 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
149 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
154 glTranslatef( 0.0f
, 0.0f
, -30.0f
);
155 build_rotmatrix( m
,info
.quat
);
156 glMultMatrixf( &m
[0][0] );
159 lw_object_show( info
.lwobject
);
168 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
170 // this is also necessary to update the context on some platforms
171 wxGLCanvas::OnSize(event
);
173 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
175 GetClientSize(&w
, &h
);
181 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
185 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
187 /* Do nothing, to avoid flashing on MSW */
190 void TestGLCanvas::LoadLWO(const wxString
&filename
)
192 /* test if lightwave object */
193 if (!lw_is_lwobject(filename
.mb_str())) return;
195 /* read lightwave object */
196 lwObject
*lwobject
= lw_object_read(filename
.mb_str());
199 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
201 /* set up mesh info */
203 info
.lwobject
= lwobject
;
207 trackball( info
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
210 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
213 if ( event
.Dragging() )
215 wxSize
sz( GetClientSize() );
217 /* drag in progress, simulate trackball */
220 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
221 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
222 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
223 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
225 add_quats( spin_quat
, info
.quat
, info
.quat
);
227 /* orientation has changed, redraw mesh */
231 info
.beginx
= event
.GetX();
232 info
.beginy
= event
.GetY();
235 void TestGLCanvas::InitGL()
237 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
240 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
242 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
245 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
247 /* remove back faces */
248 glDisable(GL_CULL_FACE
);
249 glEnable(GL_DEPTH_TEST
);
253 glShadeModel(GL_SMOOTH
);
254 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
255 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
258 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
259 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
260 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
261 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
264 glEnable(GL_LIGHTING
);
266 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
267 glEnable(GL_COLOR_MATERIAL
);
271 #endif // #if wxUSE_GLCANVAS