1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Brian Paul (original gltk version), Wolfram Gloger
5 // Modified by: Julian Smart, Francesco Montorsi
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/wfstream.h"
33 #include "wx/zstream.h"
34 #include "wx/txtstrm.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")));
66 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
68 parser
.AddSwitch("", "sb", "Do not use double buffering");
69 parser
.AddSwitch("", "db", "Use double buffering");
70 parser
.AddSwitch("", "va", "Use vertex arrays");
72 wxApp::OnInitCmdLine(parser
);
75 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
77 if (parser
.Found("sb"))
78 g_doubleBuffer
= GL_FALSE
;
79 else if (parser
.Found("db"))
80 g_doubleBuffer
= GL_TRUE
;
82 if (parser
.Found("va"))
83 g_use_vertex_arrays
= GL_TRUE
;
85 return wxApp::OnCmdLineParsed(parser
);
88 //---------------------------------------------------------------------------
90 //---------------------------------------------------------------------------
92 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
93 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
96 // My frame constructor
97 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
98 const wxSize
& size
, long style
)
99 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
),
102 SetIcon(wxICON(sample
));
106 wxMenu
*fileMenu
= new wxMenu
;
108 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
109 wxMenuBar
*menuBar
= new wxMenuBar
;
110 menuBar
->Append(fileMenu
, _T("&File"));
114 // Make a TestGLCanvas
118 int *gl_attrib
= NULL
;
121 { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
122 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
124 # if defined(__WXMAC__) || defined(__WXCOCOA__)
133 wxLogWarning("Disabling double buffering");
138 g_doubleBuffer
= GL_FALSE
;
144 m_canvas
= new TestGLCanvas(this, wxID_ANY
, gl_attrib
);
152 // Intercept menu commands
153 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
155 // true is to force the frame to close
160 //---------------------------------------------------------------------------
162 //---------------------------------------------------------------------------
164 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
165 EVT_SIZE(TestGLCanvas::OnSize
)
166 EVT_PAINT(TestGLCanvas::OnPaint
)
167 EVT_CHAR(TestGLCanvas::OnChar
)
168 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
171 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
174 : wxGLCanvas(parent
, id
, gl_attrib
)
180 // Explicitly create a new rendering context instance for this canvas.
181 m_glRC
= new wxGLContext(this);
183 // Make the new context current (activate it for use) with this canvas.
188 LoadSurface("isosurf.dat.gz");
191 TestGLCanvas::~TestGLCanvas()
196 void TestGLCanvas::LoadSurface(const wxString
& filename
)
199 // we need to set english locale to force wxTextInputStream's calls to
200 // wxStrtod to use the point and not the comma as decimal separator...
201 // (the isosurf.dat contains points and not commas)...
202 wxLocale
l(wxLANGUAGE_ENGLISH
);
204 wxZlibInputStream
* stream
=
205 new wxZlibInputStream(new wxFFileInputStream(filename
));
206 if (!stream
|| !stream
->IsOk())
208 wxLogError("Cannot load '%s' type of files!", filename
.c_str());
214 // we suppose to have in input a text file containing floating numbers
215 // space/newline-separed... first 3 numbers are the coordinates of a
216 // vertex and the following 3 are the relative vertex normal and so on...
218 wxTextInputStream
inFile(*stream
);
221 while (!stream
->Eof() && m_numverts
< MAXVERTS
)// && m_numverts<MAXVERTS)
223 inFile
>> m_verts
[m_numverts
][0] >> m_verts
[m_numverts
][1] >> m_verts
[m_numverts
][2];
224 inFile
>> m_norms
[m_numverts
][0] >> m_norms
[m_numverts
][1] >> m_norms
[m_numverts
][2];
229 // discard last vertex; it is a zero caused by the EOF
235 wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"),
236 m_numverts
, m_numverts
-2, filename
.c_str());
239 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
241 // This is a dummy, to avoid an endless succession of paint messages.
242 // OnPaint handlers must always create a wxPaintDC.
245 // This is normally only necessary if there is more than one wxGLCanvas
246 // or more than one wxGLContext in the application.
249 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
251 glRotatef( m_yrot
, 0.0f
, 1.0f
, 0.0f
);
252 glRotatef( m_xrot
, 1.0f
, 0.0f
, 0.0f
);
255 if (g_use_vertex_arrays
)
257 glDrawArrays( GL_TRIANGLE_STRIP
, 0, m_numverts
);
261 glBegin( GL_TRIANGLE_STRIP
);
263 for (int i
=0;i
<m_numverts
;i
++)
265 glNormal3fv( m_norms
[i
] );
266 glVertex3fv( m_verts
[i
] );
273 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
278 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
280 // This is normally only necessary if there is more than one wxGLCanvas
281 // or more than one wxGLContext in the application.
284 // It's up to the application code to update the OpenGL viewport settings.
285 // This is OK here only because there is only one canvas that uses the
286 // context. See the cube sample for that case that multiple canvases are
287 // made current with one context.
288 glViewport(0, 0, event
.GetSize().x
, event
.GetSize().y
);
291 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
293 switch( event
.GetKeyCode() )
296 wxTheApp
->ExitMainLoop();
316 g_smooth
= !g_smooth
;
318 glShadeModel(GL_SMOOTH
);
320 glShadeModel(GL_FLAT
);
324 g_lighting
= !g_lighting
;
326 glEnable(GL_LIGHTING
);
328 glDisable(GL_LIGHTING
);
339 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
341 static int dragging
= 0;
342 static float last_x
, last_y
;
344 // Allow default processing to happen, or else the canvas cannot gain focus
348 if(event
.LeftIsDown())
356 m_yrot
+= (event
.GetX() - last_x
)*1.0;
357 m_xrot
+= (event
.GetY() - last_y
)*1.0;
360 last_x
= event
.GetX();
361 last_y
= event
.GetY();
369 void TestGLCanvas::InitMaterials()
371 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
372 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
373 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
374 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
375 static const GLfloat front_mat_shininess
[1] = {60.0f
};
376 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
377 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
379 static const GLfloat back_mat_shininess[1] = {60.0f};
380 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
381 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
383 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
384 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
386 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
387 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
388 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
391 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
392 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
393 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
396 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
397 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
398 glEnable(GL_LIGHTING
);
400 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
401 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
402 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
405 void TestGLCanvas::InitGL()
407 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
409 glShadeModel(GL_SMOOTH
);
410 glEnable(GL_DEPTH_TEST
);
414 glMatrixMode(GL_PROJECTION
);
416 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
418 glMatrixMode(GL_MODELVIEW
);
420 glTranslatef( 0.0, 0.0, -6.0 );
422 if (g_use_vertex_arrays
)
424 glVertexPointer( 3, GL_FLOAT
, 0, m_verts
);
425 glNormalPointer( GL_FLOAT
, 0, m_norms
);
426 glEnable( GL_VERTEX_ARRAY
);
427 glEnable( GL_NORMAL_ARRAY
);