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_speed_test
= GL_FALSE
;
42 GLboolean g_use_vertex_arrays
= GL_FALSE
;
43 GLboolean g_doubleBuffer
= GL_TRUE
;
44 GLboolean g_smooth
= GL_TRUE
;
45 GLboolean g_lighting
= GL_TRUE
;
49 //---------------------------------------------------------------------------
51 //---------------------------------------------------------------------------
55 // `Main program' equivalent, creating windows and returning main app frame
58 if ( !wxApp::OnInit() )
61 // Create the main frame window
62 SetTopWindow(new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
63 wxDefaultPosition
, wxDefaultSize
));
68 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
70 parser
.AddSwitch("", "-sb");
71 parser
.AddSwitch("", "-db");
72 parser
.AddSwitch("", "-speed");
73 parser
.AddSwitch("", "-va");
75 wxApp::OnInitCmdLine(parser
);
78 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
80 if (parser
.Found("-sb"))
81 g_doubleBuffer
= GL_FALSE
;
82 else if (parser
.Found("-db"))
83 g_doubleBuffer
= GL_TRUE
;
85 if (parser
.Found("-speed"))
87 g_speed_test
= GL_TRUE
;
88 g_doubleBuffer
= GL_TRUE
;
91 if (parser
.Found("-va"))
92 g_use_vertex_arrays
= GL_TRUE
;
94 return wxApp::OnCmdLineParsed(parser
);
97 //---------------------------------------------------------------------------
99 //---------------------------------------------------------------------------
101 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
102 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
105 // My frame constructor
106 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
107 const wxSize
& size
, long style
)
108 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
),
111 SetIcon(wxICON(sample
));
115 wxMenu
*fileMenu
= new wxMenu
;
117 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
118 wxMenuBar
*menuBar
= new wxMenuBar
;
119 menuBar
->Append(fileMenu
, _T("&File"));
123 // Make a TestGLCanvas
127 int *gl_attrib
= NULL
;
130 { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
131 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
133 # if defined(__WXMAC__) || defined(__WXCOCOA__)
142 wxLogWarning("don't have double buffer, disabling\n");
147 g_doubleBuffer
= GL_FALSE
;
153 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
154 GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib
);
162 // Intercept menu commands
163 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
165 // true is to force the frame to close
170 //---------------------------------------------------------------------------
172 //---------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
175 EVT_SIZE(TestGLCanvas::OnSize
)
176 EVT_PAINT(TestGLCanvas::OnPaint
)
177 EVT_CHAR(TestGLCanvas::OnChar
)
178 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
181 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
186 const wxString
& name
,
188 : wxGLCanvas(parent
, id
, gl_attrib
, pos
, size
,
189 style
| wxFULL_REPAINT_ON_RESIZE
, name
)
191 // Explicitly create a new rendering context instance for this canvas.
192 m_glRC
= new wxGLContext(this);
194 // Make the new context current (activate it for use) with this canvas.
199 LoadSurface("isosurf.dat.gz");
202 TestGLCanvas::~TestGLCanvas()
207 void TestGLCanvas::LoadSurface(const wxString
& filename
)
209 wxZlibInputStream
* stream
=
210 new wxZlibInputStream(new wxFFileInputStream(filename
));
211 if (!stream
|| !stream
->IsOk())
213 wxLogError("Cannot load '%s' type of files!", filename
.c_str());
220 const size_t sz
= sizeof(GLfloat
);
221 while (!stream
->Eof() && m_numverts
< MAXVERTS
)
224 for (int i
=0; i
<3; i
++)
225 if (stream
->Read(&m_verts
[m_numverts
][i
], sz
).LastRead() != sz
)
227 wxLogError("Cannot read the %d-th vertex in '%s'!",
228 m_numverts
, filename
.c_str());
234 for (int i
=0; i
<3; i
++)
235 if (stream
->Read(&m_norms
[m_numverts
][i
], sz
).LastRead() != sz
)
237 wxLogError("Cannot read the %d-th vertex in '%s'!",
238 m_numverts
, filename
.c_str());
248 wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"),
249 m_numverts
, m_numverts
-2, filename
.c_str());
252 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
254 // This is a dummy, to avoid an endless succession of paint messages.
255 // OnPaint handlers must always create a wxPaintDC.
258 // This is normally only necessary if there is more than one wxGLCanvas
259 // or more than one wxGLContext in the application.
262 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
264 glRotatef( m_yrot
, 0.0f
, 1.0f
, 0.0f
);
265 glRotatef( m_xrot
, 1.0f
, 0.0f
, 0.0f
);
268 /* if (g_use_vertex_arrays)
270 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
274 glBegin( GL_TRIANGLE_STRIP
);
276 for (int i
=0;i
<m_numverts
;i
++)
278 glNormal3fv( m_norms
[i
] );
279 glVertex3fv( m_verts
[i
] );
286 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
291 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
293 // This is normally only necessary if there is more than one wxGLCanvas
294 // or more than one wxGLContext in the application.
297 // It's up to the application code to update the OpenGL viewport settings.
298 // This is OK here only because there is only one canvas that uses the
299 // context. See the cube sample for that case that multiple canvases are
300 // made current with one context.
301 glViewport(0, 0, event
.GetSize().x
, event
.GetSize().y
);
304 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
306 switch( event
.GetKeyCode() )
309 wxTheApp
->ExitMainLoop();
329 g_smooth
= !g_smooth
;
332 glShadeModel(GL_SMOOTH
);
336 glShadeModel(GL_FLAT
);
341 g_lighting
= !g_lighting
;
344 glEnable(GL_LIGHTING
);
348 glDisable(GL_LIGHTING
);
360 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
362 static int dragging
= 0;
363 static float last_x
, last_y
;
365 // Allow default processing to happen, or else the canvas cannot gain focus
369 if(event
.LeftIsDown())
377 m_yrot
+= (event
.GetX() - last_x
)*1.0;
378 m_xrot
+= (event
.GetY() - last_y
)*1.0;
381 last_x
= event
.GetX();
382 last_y
= event
.GetY();
390 void TestGLCanvas::InitMaterials()
392 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
393 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
394 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
395 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
396 static const GLfloat front_mat_shininess
[1] = {60.0f
};
397 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
398 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
400 static const GLfloat back_mat_shininess[1] = {60.0f};
401 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
402 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
404 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
405 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
407 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
408 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
409 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
412 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
413 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
414 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
417 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
418 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
419 glEnable(GL_LIGHTING
);
421 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
422 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
423 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
426 void TestGLCanvas::InitGL()
428 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
430 glShadeModel(GL_SMOOTH
);
431 glEnable(GL_DEPTH_TEST
);
435 glMatrixMode(GL_PROJECTION
);
437 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
439 glMatrixMode(GL_MODELVIEW
);
441 glTranslatef( 0.0, 0.0, -6.0 );
443 if (g_use_vertex_arrays
)
445 glVertexPointer( 3, GL_FLOAT
, 0, m_verts
);
446 glNormalPointer( GL_FLOAT
, 0, m_norms
);
447 glEnable( GL_VERTEX_ARRAY_EXT
);
448 glEnable( GL_NORMAL_ARRAY_EXT
);