1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Brian Paul (original gltk version), Wolfram Gloger
5 // Modified by: Julian Smart
8 // Copyright: (c) Julian Smart
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"
28 #include "wx/glcanvas.h"
31 #include "wx/cmdline.h"
32 #include "wx/archive.h"
33 #include "wx/wfstream.h"
34 #include "wx/zstream.h"
37 #include "../../sample.xpm"
40 // global options which can be set through command-line options
41 GLboolean g_use_vertex_arrays
= GL_FALSE
;
42 GLboolean g_doubleBuffer
= GL_TRUE
;
43 GLboolean g_smooth
= GL_TRUE
;
44 GLboolean g_lighting
= GL_TRUE
;
48 //---------------------------------------------------------------------------
50 //---------------------------------------------------------------------------
54 // `Main program' equivalent, creating windows and returning main app frame
57 if ( !wxApp::OnInit() )
60 // Create the main frame window
61 SetTopWindow(new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
62 wxDefaultPosition
, wxDefaultSize
));
67 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
69 parser
.AddSwitch("", "sb", "Do not use double buffering");
70 parser
.AddSwitch("", "db", "Use double buffering");
71 parser
.AddSwitch("", "va", "Use vertex arrays");
73 wxApp::OnInitCmdLine(parser
);
76 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
78 if (parser
.Found("sb"))
79 g_doubleBuffer
= GL_FALSE
;
80 else if (parser
.Found("db"))
81 g_doubleBuffer
= GL_TRUE
;
83 if (parser
.Found("va"))
84 g_use_vertex_arrays
= GL_TRUE
;
86 return wxApp::OnCmdLineParsed(parser
);
89 //---------------------------------------------------------------------------
91 //---------------------------------------------------------------------------
93 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
94 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
97 // My frame constructor
98 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
99 const wxSize
& size
, long style
)
100 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
),
103 SetIcon(wxICON(sample
));
107 wxMenu
*fileMenu
= new wxMenu
;
109 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
110 wxMenuBar
*menuBar
= new wxMenuBar
;
111 menuBar
->Append(fileMenu
, _T("&File"));
115 // Make a TestGLCanvas
119 int *gl_attrib
= NULL
;
122 { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
123 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
125 # if defined(__WXMAC__) || defined(__WXCOCOA__)
134 wxLogWarning("don't have double buffer, disabling\n");
139 g_doubleBuffer
= GL_FALSE
;
145 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
146 GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib
);
154 // Intercept menu commands
155 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
157 // true is to force the frame to close
162 //---------------------------------------------------------------------------
164 //---------------------------------------------------------------------------
166 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
167 EVT_SIZE(TestGLCanvas::OnSize
)
168 EVT_PAINT(TestGLCanvas::OnPaint
)
169 EVT_CHAR(TestGLCanvas::OnChar
)
170 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
173 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
178 const wxString
& name
,
180 : wxGLCanvas(parent
, id
, gl_attrib
, pos
, size
,
181 style
| wxFULL_REPAINT_ON_RESIZE
, name
)
183 // Explicitly create a new rendering context instance for this canvas.
184 m_glRC
= new wxGLContext(this);
186 // Make the new context current (activate it for use) with this canvas.
191 LoadSurface("isosurf.dat.gz");
194 TestGLCanvas::~TestGLCanvas()
199 void TestGLCanvas::LoadSurface(const wxString
& filename
)
201 wxZlibInputStream
* stream
=
202 new wxZlibInputStream(new wxFFileInputStream(filename
));
203 if (!stream
|| !stream
->IsOk())
205 wxLogError("Cannot load '%s' type of files!", filename
.c_str());
212 const size_t sz
= sizeof(GLfloat
);
213 while (!stream
->Eof() && m_numverts
< MAXVERTS
)
216 for (int i
=0; i
<3; i
++)
217 if (stream
->Read(&m_verts
[m_numverts
][i
], sz
).LastRead() != sz
)
219 wxLogError("Cannot read the %d-th vertex in '%s'!",
220 m_numverts
, filename
.c_str());
226 for (int i
=0; i
<3; i
++)
227 if (stream
->Read(&m_norms
[m_numverts
][i
], sz
).LastRead() != sz
)
229 wxLogError("Cannot read the %d-th vertex in '%s'!",
230 m_numverts
, filename
.c_str());
240 wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"),
241 m_numverts
, m_numverts
-2, filename
.c_str());
244 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
246 // This is a dummy, to avoid an endless succession of paint messages.
247 // OnPaint handlers must always create a wxPaintDC.
250 // This is normally only necessary if there is more than one wxGLCanvas
251 // or more than one wxGLContext in the application.
254 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
256 glRotatef( m_yrot
, 0.0f
, 1.0f
, 0.0f
);
257 glRotatef( m_xrot
, 1.0f
, 0.0f
, 0.0f
);
260 /* if (g_use_vertex_arrays)
262 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
266 glBegin( GL_TRIANGLE_STRIP
);
268 for (int i
=0;i
<m_numverts
;i
++)
270 glNormal3fv( m_norms
[i
] );
271 glVertex3fv( m_verts
[i
] );
278 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
283 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
285 // This is normally only necessary if there is more than one wxGLCanvas
286 // or more than one wxGLContext in the application.
289 // It's up to the application code to update the OpenGL viewport settings.
290 // This is OK here only because there is only one canvas that uses the
291 // context. See the cube sample for that case that multiple canvases are
292 // made current with one context.
293 glViewport(0, 0, event
.GetSize().x
, event
.GetSize().y
);
296 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
298 switch( event
.GetKeyCode() )
301 wxTheApp
->ExitMainLoop();
321 g_smooth
= !g_smooth
;
324 glShadeModel(GL_SMOOTH
);
328 glShadeModel(GL_FLAT
);
333 g_lighting
= !g_lighting
;
336 glEnable(GL_LIGHTING
);
340 glDisable(GL_LIGHTING
);
352 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
354 static int dragging
= 0;
355 static float last_x
, last_y
;
357 // Allow default processing to happen, or else the canvas cannot gain focus
361 if(event
.LeftIsDown())
369 m_yrot
+= (event
.GetX() - last_x
)*1.0;
370 m_xrot
+= (event
.GetY() - last_y
)*1.0;
373 last_x
= event
.GetX();
374 last_y
= event
.GetY();
382 void TestGLCanvas::InitMaterials()
384 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
385 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
386 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
387 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
388 static const GLfloat front_mat_shininess
[1] = {60.0f
};
389 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
390 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
392 static const GLfloat back_mat_shininess[1] = {60.0f};
393 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
394 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
396 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
397 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
399 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
400 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
401 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
404 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
405 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
406 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
409 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
410 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
411 glEnable(GL_LIGHTING
);
413 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
414 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
415 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
418 void TestGLCanvas::InitGL()
420 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
422 glShadeModel(GL_SMOOTH
);
423 glEnable(GL_DEPTH_TEST
);
427 glMatrixMode(GL_PROJECTION
);
429 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
431 glMatrixMode(GL_MODELVIEW
);
433 glTranslatef( 0.0, 0.0, -6.0 );
435 if (g_use_vertex_arrays
)
437 glVertexPointer( 3, GL_FLOAT
, 0, m_verts
);
438 glNormalPointer( GL_FLOAT
, 0, m_norms
);
439 glEnable( GL_VERTEX_ARRAY_EXT
);
440 glEnable( GL_NORMAL_ARRAY_EXT
);