]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/penguin/penguin.cpp
Tried to make wxGLCanvas work again. WIP.
[wxWidgets.git] / utils / glcanvas / samples / penguin / penguin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: penguin.cpp
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "penguin.h"
29 #include <GL/glu.h>
30 #include "wx/gtk/win_gtk.h"
31
32 #define VIEW_ASPECT 1.3
33
34 /* `Main program' equivalent, creating windows and returning main app frame */
35 bool MyApp::OnInit(void)
36 {
37
38 /* Create the main frame window */
39 MyFrame *frame = new MyFrame(NULL, "wxWindows OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
40
41 /* Make a menubar */
42 wxMenu *fileMenu = new wxMenu;
43
44 fileMenu->Append(wxID_EXIT, "E&xit");
45 wxMenuBar *menuBar = new wxMenuBar;
46 menuBar->Append(fileMenu, "&File");
47 frame->SetMenuBar(menuBar);
48
49 frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
50
51 /* Load file wiht mesh data */
52 frame->m_canvas->LoadLWO( "penguin.lwo" );
53
54 /* Show the frame */
55 frame->Show(TRUE);
56
57 return TRUE;
58 }
59
60 IMPLEMENT_APP(MyApp)
61
62 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
63 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
64 END_EVENT_TABLE()
65
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)
70 {
71 m_canvas = NULL;
72 }
73
74 /* Intercept menu commands */
75 void MyFrame::OnExit(wxCommandEvent& event)
76 {
77 Destroy();
78 }
79
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)
85 END_EVENT_TABLE()
86
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)
90 {
91 block = FALSE;
92 }
93
94 TestGLCanvas::~TestGLCanvas(void)
95 {
96 /* destroy mesh */
97 lw_object_free(info.lwobject);
98 }
99
100 void TestGLCanvas::OnPaint( wxPaintEvent& event )
101 {
102 /* must always be here */
103 wxPaintDC dc(this);
104
105 #ifndef __WXMOTIF__
106 if (!GetContext()) return;
107 #endif
108
109 printf( "on refresh.\n" );
110 int x,y;
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 );
116
117 SetCurrent();
118
119 /* initialize OpenGL */
120 if (info.do_init == TRUE)
121 {
122 InitGL();
123 info.do_init = FALSE;
124 }
125
126 /* view */
127 glMatrixMode( GL_PROJECTION );
128 glLoadIdentity();
129 gluPerspective( info.zoom, VIEW_ASPECT, 1, 100 );
130 glMatrixMode( GL_MODELVIEW );
131
132 /* clear */
133 glClearColor( .3, .4, .6, 1 );
134 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
135
136 /* transformations */
137 GLfloat m[4][4];
138 glLoadIdentity();
139 glTranslatef( 0, 0, -30 );
140 build_rotmatrix( m,info.quat );
141 glMultMatrixf( &m[0][0] );
142
143 /* draw object */
144 lw_object_show( info.lwobject );
145
146 /* flush */
147 glFlush();
148
149 /* swap */
150 SwapBuffers();
151 }
152
153 void TestGLCanvas::OnSize(wxSizeEvent& event)
154 {
155 int width, height;
156 GetClientSize(& width, & height);
157
158 printf( "onsize %d %d.\n", width, height );
159
160 #ifndef __WXMOTIF__
161 if (GetContext())
162 #endif
163 {
164 SetCurrent();
165 glViewport(0, 0, width, height);
166 }
167 }
168
169 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
170 {
171 /* Do nothing, to avoid flashing on MSW */
172 }
173
174 void TestGLCanvas::LoadLWO(const wxString &filename)
175 {
176 /* test if lightwave object */
177 if (!lw_is_lwobject(filename)) return;
178
179 /* read lightwave object */
180 lwObject *lwobject = lw_object_read(filename);
181
182 /* scale */
183 lw_object_scale(lwobject, 10.0 / lw_object_radius(lwobject));
184
185 /* set up mesh info */
186 info.do_init = TRUE;
187 info.lwobject = lwobject;
188 info.beginx = 0;
189 info.beginy = 0;
190 info.zoom = 45;
191 trackball( info.quat, 0.0, 0.0, 0.0, 0.0 );
192 }
193
194 void TestGLCanvas::OnMouse( wxMouseEvent& event )
195 {
196 wxSize sz(GetClientSize());
197 if (event.Dragging())
198 {
199 /* drag in progress, simulate trackball */
200 float spin_quat[4];
201 trackball(spin_quat,
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);
206
207 add_quats( spin_quat, info.quat, info.quat );
208
209 /* orientation has changed, redraw mesh */
210 Refresh(FALSE);
211 }
212
213 info.beginx = event.GetX();
214 info.beginy = event.GetY();
215 }
216
217 void TestGLCanvas::InitGL(void)
218 {
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 */
223
224 /* remove back faces */
225 glDisable(GL_CULL_FACE);
226 glEnable(GL_DEPTH_TEST);
227
228 /* speedups */
229 glEnable(GL_DITHER);
230 glShadeModel(GL_SMOOTH);
231 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
232 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
233
234 /* light */
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);
239 glEnable(GL_LIGHT0);
240 glEnable(GL_LIGHT1);
241 glEnable(GL_LIGHTING);
242
243 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
244 glEnable(GL_COLOR_MATERIAL);
245 }
246
247