1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
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 #define VIEW_ASPECT 1.3
42 // `Main program' equivalent, creating windows and returning main app frame
45 // Create the main frame window
46 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets 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
);
57 frame
->SetCanvas( new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
58 wxSize(200, 200), wxSUNKEN_BORDER
) );
60 /* Load file wiht mesh data */
61 frame
->GetCanvas()->LoadLWO( wxT("penguin.lwo") );
71 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
72 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
75 /* My frame constructor */
76 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
77 const wxSize
& size
, long style
)
78 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
81 SetIcon(wxIcon(sample_xpm
));
84 /* Intercept menu commands */
85 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
87 // true is to force the frame to close
91 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
92 EVT_SIZE(TestGLCanvas::OnSize
)
93 EVT_PAINT(TestGLCanvas::OnPaint
)
94 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
95 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
98 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
99 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
100 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
)
105 TestGLCanvas::~TestGLCanvas()
108 lw_object_free(info
.lwobject
);
111 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
113 /* must always be here */
117 if (!GetContext()) return;
126 info
.do_init
= false;
130 glMatrixMode( GL_PROJECTION
);
132 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1.0, 100.0 );
133 glMatrixMode( GL_MODELVIEW
);
136 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
137 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
142 glTranslatef( 0.0f
, 0.0f
, -30.0f
);
143 build_rotmatrix( m
,info
.quat
);
144 glMultMatrixf( &m
[0][0] );
147 lw_object_show( info
.lwobject
);
156 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
158 // this is also necessary to update the context on some platforms
159 wxGLCanvas::OnSize(event
);
161 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
163 GetClientSize(&w
, &h
);
169 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
173 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
175 /* Do nothing, to avoid flashing on MSW */
178 void TestGLCanvas::LoadLWO(const wxString
&filename
)
180 /* test if lightwave object */
181 if (!lw_is_lwobject(filename
.mb_str())) return;
183 /* read lightwave object */
184 lwObject
*lwobject
= lw_object_read(filename
.mb_str());
187 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
189 /* set up mesh info */
191 info
.lwobject
= lwobject
;
195 trackball( info
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
198 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
201 if ( event
.Dragging() )
203 wxSize
sz( GetClientSize() );
205 /* drag in progress, simulate trackball */
208 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
209 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
210 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
211 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
213 add_quats( spin_quat
, info
.quat
, info
.quat
);
215 /* orientation has changed, redraw mesh */
219 info
.beginx
= event
.GetX();
220 info
.beginy
= event
.GetY();
223 void TestGLCanvas::InitGL()
225 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
228 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
230 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
233 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
235 /* remove back faces */
236 glDisable(GL_CULL_FACE
);
237 glEnable(GL_DEPTH_TEST
);
241 glShadeModel(GL_SMOOTH
);
242 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
243 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
246 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
247 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
248 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
249 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
252 glEnable(GL_LIGHTING
);
254 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
255 glEnable(GL_COLOR_MATERIAL
);