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"
30 #include "wx/gtk/win_gtk.h"
32 #define VIEW_ASPECT 1.3
34 /* `Main program' equivalent, creating windows and returning main app frame */
35 bool MyApp::OnInit(void)
38 /* Create the main frame window */
39 MyFrame
*frame
= new MyFrame(NULL
, "wxWindows OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
42 wxMenu
*fileMenu
= new wxMenu
;
44 fileMenu
->Append(wxID_EXIT
, "E&xit");
45 wxMenuBar
*menuBar
= new wxMenuBar
;
46 menuBar
->Append(fileMenu
, "&File");
47 frame
->SetMenuBar(menuBar
);
49 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));
51 /* Load file wiht mesh data */
52 frame
->m_canvas
->LoadLWO( "penguin.lwo" );
62 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
63 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
66 /* My frame constructor */
67 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
68 const wxSize
& size
, long style
):
69 wxFrame(frame
, -1, title
, pos
, size
, style
)
74 /* Intercept menu commands */
75 void MyFrame::OnExit(wxCommandEvent
& event
)
80 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
81 EVT_SIZE(TestGLCanvas::OnSize
)
82 EVT_PAINT(TestGLCanvas::OnPaint
)
83 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
84 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
)
87 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
88 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
89 wxGLCanvas(parent
, id
, pos
, size
, style
, name
)
94 TestGLCanvas::~TestGLCanvas(void)
97 lw_object_free(info
.lwobject
);
100 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
102 /* must always be here */
106 if (!GetContext()) return;
109 printf( "on refresh.\n" );
111 GtkMyFixed
*fixed
= GTK_MYFIXED(m_wxwindow
);
112 gdk_window_get_size( m_wxwindow
->window
, &x
, &y
);
113 printf( "-> window %d %d.\n", x
, y
);
114 gdk_window_get_size( fixed
->bin_window
, &x
, &y
);
115 printf( "-> bin_window %d %d.\n", x
, y
);
119 /* initialize OpenGL */
120 if (info
.do_init
== TRUE
)
123 info
.do_init
= FALSE
;
127 glMatrixMode( GL_PROJECTION
);
129 gluPerspective( info
.zoom
, VIEW_ASPECT
, 1, 100 );
130 glMatrixMode( GL_MODELVIEW
);
133 glClearColor( .3, .4, .6, 1 );
134 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
136 /* transformations */
139 glTranslatef( 0, 0, -30 );
140 build_rotmatrix( m
,info
.quat
);
141 glMultMatrixf( &m
[0][0] );
144 lw_object_show( info
.lwobject
);
153 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
156 GetClientSize(& width
, & height
);
158 printf( "onsize %d %d.\n", width
, height
);
165 glViewport(0, 0, width
, height
);
169 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
171 /* Do nothing, to avoid flashing on MSW */
174 void TestGLCanvas::LoadLWO(const wxString
&filename
)
176 /* test if lightwave object */
177 if (!lw_is_lwobject(filename
)) return;
179 /* read lightwave object */
180 lwObject
*lwobject
= lw_object_read(filename
);
183 lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
));
185 /* set up mesh info */
187 info
.lwobject
= lwobject
;
191 trackball( info
.quat
, 0.0, 0.0, 0.0, 0.0 );
194 void TestGLCanvas::OnMouse( wxMouseEvent
& event
)
196 wxSize
sz(GetClientSize());
197 if (event
.Dragging())
199 /* drag in progress, simulate trackball */
202 (2.0*info
.beginx
- sz
.x
) / sz
.x
,
203 ( sz
.y
- 2.0*info
.beginy
) / sz
.y
,
204 ( 2.0*event
.GetX() - sz
.x
) / sz
.x
,
205 ( sz
.y
- 2.0*event
.GetY()) / sz
.y
);
207 add_quats( spin_quat
, info
.quat
, info
.quat
);
209 /* orientation has changed, redraw mesh */
213 info
.beginx
= event
.GetX();
214 info
.beginy
= event
.GetY();
217 void TestGLCanvas::InitGL(void)
219 GLfloat light0_pos
[4] = { -50.0, 50.0, 0.0, 0.0 };
220 GLfloat light0_color
[4] = { .6, .6, .6, 1.0 }; /* white light */
221 GLfloat light1_pos
[4] = { 50.0, 50.0, 0.0, 0.0 };
222 GLfloat light1_color
[4] = { .4, .4, 1, 1.0 }; /* cold blue light */
224 /* remove back faces */
225 glDisable(GL_CULL_FACE
);
226 glEnable(GL_DEPTH_TEST
);
230 glShadeModel(GL_SMOOTH
);
231 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
232 glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
);
235 glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
);
236 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light0_color
);
237 glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
);
238 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, light1_color
);
241 glEnable(GL_LIGHTING
);
243 glColorMaterial(GL_FRONT_AND_BACK
,GL_AMBIENT_AND_DIFFUSE
);
244 glEnable(GL_COLOR_MATERIAL
);