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 #include "../../sample.xpm"
45 #define VIEW_ASPECT 1.3
47 // `Main program' equivalent, creating windows and returning main app frame
50 // Create the main frame window
51 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Penguin Sample"),
52 wxDefaultPosition
, wxDefaultSize
);
55 wxMenu
*fileMenu
= new wxMenu
;
57 fileMenu
->Append(wxID_EXIT
, wxT("E&xit"));
58 wxMenuBar
*menuBar
= new wxMenuBar
;
59 menuBar
->Append(fileMenu
, wxT("&File"));
60 frame
->SetMenuBar(menuBar
);
62 frame
->SetCanvas( new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
63 wxSize(200, 200), wxSUNKEN_BORDER
) );
65 /* Load file wiht mesh data */
66 frame
->GetCanvas()->LoadLWO( wxT("penguin.lwo") );
76 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
77 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
80 /* My frame constructor */
81 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
82 const wxSize
& size
, long style
)
83 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
86 SetIcon(wxIcon(sample_xpm
));
89 /* Intercept menu commands */
90 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
92 // true is to force the frame to close
96 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
97 EVT_SIZE(TestGLCanvas::OnSize
)
98 EVT_PAINT(TestGLCanvas::OnPaint
)
99 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
100 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
103 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
104 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
105 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
)
110 TestGLCanvas::~TestGLCanvas()
113 lw_object_free(info
.lwobject
);
116 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
118 /* must always be here */
122 if (!GetContext()) return;
131 info
.do_init
= false;
135 glMatrixMode( GL_PROJECTION
);
137 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1.0, 100.0 );
138 glMatrixMode( GL_MODELVIEW
);
141 glClearColor( 0.3f
, 0.4f
, 0.6f
, 1.0f
);
142 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
147 glTranslatef( 0.0f
, 0.0f
, -30.0f
);
148 build_rotmatrix( m
,info
.quat
);
149 glMultMatrixf( &m
[0][0] );
152 lw_object_show( info
.lwobject
);
161 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
163 // this is also necessary to update the context on some platforms
164 wxGLCanvas::OnSize(event
);
166 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
168 GetClientSize(&w
, &h
);
174 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
178 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
180 /* Do nothing, to avoid flashing on MSW */
183 void TestGLCanvas::LoadLWO(const wxString
&filename
)
185 /* test if lightwave object */
186 if (!lw_is_lwobject(filename
.mb_str())) return;
188 /* read lightwave object */
189 lwObject
*lwobject
= lw_object_read(filename
.mb_str());
192 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
194 /* set up mesh info */
196 info
.lwobject
= lwobject
;
200 trackball( info
.quat
, 0.0f
, 0.0f
, 0.0f
, 0.0f
);
203 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
206 if ( event
.Dragging() )
208 wxSize
sz( GetClientSize() );
210 /* drag in progress, simulate trackball */
213 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
214 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
215 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
216 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
218 add_quats( spin_quat
, info
.quat
, info
.quat
);
220 /* orientation has changed, redraw mesh */
224 info
.beginx
= event
.GetX();
225 info
.beginy
= event
.GetY();
228 void TestGLCanvas::InitGL()
230 static const GLfloat light0_pos
[4] = { -50.0f
, 50.0f
, 0.0f
, 0.0f
};
233 static const GLfloat light0_color
[4] = { 0.6f
, 0.6f
, 0.6f
, 1.0f
};
235 static const GLfloat light1_pos
[4] = { 50.0f
, 50.0f
, 0.0f
, 0.0f
};
238 static const GLfloat light1_color
[4] = { 0.4f
, 0.4f
, 1.0f
, 1.0f
};
240 /* remove back faces */
241 glDisable(GL_CULL_FACE
);
242 glEnable(GL_DEPTH_TEST
);
246 glShadeModel(GL_SMOOTH
);
247 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
248 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
251 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
252 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
253 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
254 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
257 glEnable(GL_LIGHTING
);
259 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
260 glEnable(GL_COLOR_MATERIAL
);