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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
29 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
33 #include "wx/glcanvas.h"
36 #if defined(__WXMAC__) || defined(__WXCOCOA__)
38 # include <OpenGL/gl.h>
39 # include <OpenGL/glu.h>
49 // disabled because this has apparently changed in OpenGL 1.2, so doesn't link
50 // correctly if this is on...
51 #ifdef GL_EXT_vertex_array
52 #undef GL_EXT_vertex_array
57 #include "../../sample.xpm"
59 // The following part is taken largely unchanged from the original C Version
61 GLboolean speed_test
= GL_FALSE
;
62 GLboolean use_vertex_arrays
= GL_FALSE
;
64 GLboolean doubleBuffer
= GL_TRUE
;
66 GLboolean smooth
= GL_TRUE
;
67 GLboolean lighting
= GL_TRUE
;
70 #define MAXVERTS 10000
72 static GLfloat verts
[MAXVERTS
][3];
73 static GLfloat norms
[MAXVERTS
][3];
74 static GLint numverts
;
80 static void read_surface( const wxChar
*filename
)
82 FILE *f
= wxFopen(filename
,_T("r"));
85 wxString msg
= _T("Couldn't read ");
92 while (!feof(f
) && numverts
<MAXVERTS
)
94 fscanf( f
, "%f %f %f %f %f %f",
95 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
96 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
102 wxPrintf(_T("%d vertices, %d triangles\n"), numverts
, numverts
-2);
108 static void draw_surface()
112 #ifdef GL_EXT_vertex_array
113 if (use_vertex_arrays
)
115 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
120 glBegin( GL_TRIANGLE_STRIP
);
121 for (i
=0;i
<numverts
;i
++)
123 glNormal3fv( norms
[i
] );
124 glVertex3fv( verts
[i
] );
133 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
135 glRotatef( yrot
, 0.0f
, 1.0f
, 0.0f
);
136 glRotatef( xrot
, 1.0f
, 0.0f
, 0.0f
);
146 static void InitMaterials()
148 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
149 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
150 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
151 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
152 static const GLfloat front_mat_shininess
[1] = {60.0f
};
153 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
154 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
156 static const GLfloat back_mat_shininess[1] = {60.0f};
157 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
158 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
160 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
161 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
163 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
164 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
165 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
168 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
169 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
170 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
173 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
174 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
175 glEnable(GL_LIGHTING
);
177 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
178 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
179 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
183 static void Init(void)
185 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
187 glShadeModel(GL_SMOOTH
);
188 glEnable(GL_DEPTH_TEST
);
192 glMatrixMode(GL_PROJECTION
);
194 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
196 glMatrixMode(GL_MODELVIEW
);
198 glTranslatef( 0.0, 0.0, -6.0 );
200 #ifdef GL_EXT_vertex_array
201 if (use_vertex_arrays
)
203 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
204 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
205 glEnable( GL_VERTEX_ARRAY_EXT
);
206 glEnable( GL_NORMAL_ARRAY_EXT
);
211 static GLenum
Args(int argc
, wxChar
**argv
)
215 for (i
= 1; i
< argc
; i
++)
217 if (wxStrcmp(argv
[i
], _T("-sb")) == 0)
219 doubleBuffer
= GL_FALSE
;
221 else if (wxStrcmp(argv
[i
], _T("-db")) == 0)
223 doubleBuffer
= GL_TRUE
;
225 else if (wxStrcmp(argv
[i
], _T("-speed")) == 0)
227 speed_test
= GL_TRUE
;
228 doubleBuffer
= GL_TRUE
;
230 else if (wxStrcmp(argv
[i
], _T("-va")) == 0)
232 use_vertex_arrays
= GL_TRUE
;
236 wxString msg
= _T("Bad option: ");
246 // The following part was written for wxWidgets 1.66
247 MyFrame
*frame
= NULL
;
251 // `Main program' equivalent, creating windows and returning main app frame
256 // Create the main frame window
257 frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
258 wxDefaultPosition
, wxDefaultSize
);
261 frame
->SetIcon(wxIcon(_T("mondrian")));
264 wxMenu
*fileMenu
= new wxMenu
;
266 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
267 wxMenuBar
*menuBar
= new wxMenuBar
;
268 menuBar
->Append(fileMenu
, _T("&File"));
269 frame
->SetMenuBar(menuBar
);
271 // Make a TestGLCanvas
275 int *gl_attrib
= NULL
;
277 int gl_attrib
[20] = { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
278 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
289 printf("don't have double buffer, disabling\n");
293 doubleBuffer
= GL_FALSE
;
296 frame
->m_canvas
= new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
297 wxDefaultSize
, 0, _T("TestGLCanvas"), gl_attrib
);
302 frame
->m_canvas
->SetCurrent();
303 read_surface( _T("isosurf.dat") );
310 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
311 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
314 // My frame constructor
315 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
316 const wxSize
& size
, long style
)
317 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
320 SetIcon(wxIcon(sample_xpm
));
328 // Intercept menu commands
329 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
331 // true is to force the frame to close
336 * TestGLCanvas implementation
339 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
340 EVT_SIZE(TestGLCanvas::OnSize
)
341 EVT_PAINT(TestGLCanvas::OnPaint
)
342 EVT_CHAR(TestGLCanvas::OnChar
)
343 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
344 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
347 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
348 const wxPoint
& pos
, const wxSize
& size
, long style
,
349 const wxString
& name
, int* gl_attrib
)
350 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
, gl_attrib
)
355 /* Make sure server supports the vertex array extension */
356 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
357 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" ))
359 use_vertex_arrays
= GL_FALSE
;
364 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
366 // This is a dummy, to avoid an endless succession of paint messages.
367 // OnPaint handlers must always create a wxPaintDC.
371 if (!GetContext()) return;
380 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
382 // this is also necessary to update the context on some platforms
383 wxGLCanvas::OnSize(event
);
385 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
387 GetClientSize(&w
, &h
);
393 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
397 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
399 switch( event
.GetKeyCode() )
402 wxTheApp
->ExitMainLoop();
425 glShadeModel(GL_SMOOTH
);
429 glShadeModel(GL_FLAT
);
434 lighting
= !lighting
;
437 glEnable(GL_LIGHTING
);
441 glDisable(GL_LIGHTING
);
453 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
455 static int dragging
= 0;
456 static float last_x
, last_y
;
458 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
459 if(event
.LeftIsDown())
467 yrot
+= (event
.GetX() - last_x
)*1.0;
468 xrot
+= (event
.GetY() - last_y
)*1.0;
471 last_x
= event
.GetX();
472 last_y
= event
.GetY();
479 void TestGLCanvas::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
481 // Do nothing, to avoid flashing.