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"
35 #if defined(__WXMAC__) || defined(__WXCOCOA__)
37 # include <OpenGL/gl.h>
38 # include <OpenGL/glu.h>
48 // disabled because this has apparently changed in OpenGL 1.2, so doesn't link
49 // correctly if this is on...
50 #ifdef GL_EXT_vertex_array
51 #undef GL_EXT_vertex_array
56 // The following part is taken largely unchanged from the original C Version
60 GLboolean speed_test
= GL_FALSE
;
61 GLboolean use_vertex_arrays
= GL_FALSE
;
63 GLboolean doubleBuffer
= GL_TRUE
;
65 GLboolean smooth
= GL_TRUE
;
66 GLboolean lighting
= GL_TRUE
;
69 #define MAXVERTS 10000
71 static GLfloat verts
[MAXVERTS
][3];
72 static GLfloat norms
[MAXVERTS
][3];
73 static GLint numverts
;
79 static void read_surface( const wxChar
*filename
)
81 FILE *f
= wxFopen(filename
,_T("r"));
84 wxString msg
= _T("Couldn't read ");
91 while (!feof(f
) && numverts
<MAXVERTS
)
93 fscanf( f
, "%f %f %f %f %f %f",
94 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
95 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
101 wxPrintf(_T("%d vertices, %d triangles\n"), numverts
, numverts
-2);
107 static void draw_surface()
111 #ifdef GL_EXT_vertex_array
112 if (use_vertex_arrays
)
114 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
119 glBegin( GL_TRIANGLE_STRIP
);
120 for (i
=0;i
<numverts
;i
++)
122 glNormal3fv( norms
[i
] );
123 glVertex3fv( verts
[i
] );
132 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
134 glRotatef( yrot
, 0.0f
, 1.0f
, 0.0f
);
135 glRotatef( xrot
, 1.0f
, 0.0f
, 0.0f
);
145 static void InitMaterials()
147 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
148 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
149 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
150 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
151 static const GLfloat front_mat_shininess
[1] = {60.0f
};
152 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
153 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
155 static const GLfloat back_mat_shininess[1] = {60.0f};
156 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
157 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
159 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
160 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
162 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
163 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
164 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
167 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
168 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
169 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
172 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
173 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
174 glEnable(GL_LIGHTING
);
176 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
177 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
178 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
182 static void Init(void)
184 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
186 glShadeModel(GL_SMOOTH
);
187 glEnable(GL_DEPTH_TEST
);
191 glMatrixMode(GL_PROJECTION
);
193 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
195 glMatrixMode(GL_MODELVIEW
);
197 glTranslatef( 0.0, 0.0, -6.0 );
199 #ifdef GL_EXT_vertex_array
200 if (use_vertex_arrays
)
202 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
203 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
204 glEnable( GL_VERTEX_ARRAY_EXT
);
205 glEnable( GL_NORMAL_ARRAY_EXT
);
210 static GLenum
Args(int argc
, wxChar
**argv
)
214 for (i
= 1; i
< argc
; i
++)
216 if (wxStrcmp(argv
[i
], _T("-sb")) == 0)
218 doubleBuffer
= GL_FALSE
;
220 else if (wxStrcmp(argv
[i
], _T("-db")) == 0)
222 doubleBuffer
= GL_TRUE
;
224 else if (wxStrcmp(argv
[i
], _T("-speed")) == 0)
226 speed_test
= GL_TRUE
;
227 doubleBuffer
= GL_TRUE
;
229 else if (wxStrcmp(argv
[i
], _T("-va")) == 0)
231 use_vertex_arrays
= GL_TRUE
;
235 wxString msg
= _T("Bad option: ");
245 // The following part was written for wxWidgets 1.66
246 MyFrame
*frame
= NULL
;
250 // `Main program' equivalent, creating windows and returning main app frame
255 // Create the main frame window
256 frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
257 wxDefaultPosition
, wxDefaultSize
);
260 frame
->SetIcon(wxIcon(_T("mondrian")));
263 wxMenu
*fileMenu
= new wxMenu
;
265 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
266 wxMenuBar
*menuBar
= new wxMenuBar
;
267 menuBar
->Append(fileMenu
, _T("&File"));
268 frame
->SetMenuBar(menuBar
);
270 // Make a TestGLCanvas
274 int *gl_attrib
= NULL
;
276 int gl_attrib
[20] = { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
277 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
288 printf("don't have double buffer, disabling\n");
292 doubleBuffer
= GL_FALSE
;
295 frame
->m_canvas
= new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
296 wxDefaultSize
, 0, _T("TestGLCanvas"), gl_attrib
);
301 frame
->m_canvas
->SetCurrent();
302 read_surface( _T("isosurf.dat") );
309 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
310 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
313 // My frame constructor
314 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
315 const wxSize
& size
, long style
)
316 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
326 // Intercept menu commands
327 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
329 // true is to force the frame to close
334 * TestGLCanvas implementation
337 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
338 EVT_SIZE(TestGLCanvas::OnSize
)
339 EVT_PAINT(TestGLCanvas::OnPaint
)
340 EVT_CHAR(TestGLCanvas::OnChar
)
341 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
342 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
345 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
346 const wxPoint
& pos
, const wxSize
& size
, long style
,
347 const wxString
& name
, int* gl_attrib
)
348 : wxGLCanvas(parent
, id
, pos
, size
, style
, name
, gl_attrib
)
353 /* Make sure server supports the vertex array extension */
354 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
355 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" ))
357 use_vertex_arrays
= GL_FALSE
;
362 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
364 // This is a dummy, to avoid an endless succession of paint messages.
365 // OnPaint handlers must always create a wxPaintDC.
369 if (!GetContext()) return;
378 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
380 // this is also necessary to update the context on some platforms
381 wxGLCanvas::OnSize(event
);
383 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
385 GetClientSize(&w
, &h
);
391 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
395 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
397 switch( event
.GetKeyCode() )
400 wxTheApp
->ExitMainLoop();
423 glShadeModel(GL_SMOOTH
);
427 glShadeModel(GL_FLAT
);
432 lighting
= !lighting
;
435 glEnable(GL_LIGHTING
);
439 glDisable(GL_LIGHTING
);
451 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
453 static int dragging
= 0;
454 static float last_x
, last_y
;
456 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
457 if(event
.LeftIsDown())
465 yrot
+= (event
.GetX() - last_x
)*1.0;
466 xrot
+= (event
.GetY() - last_y
)*1.0;
469 last_x
= event
.GetX();
470 last_y
= event
.GetY();
477 void TestGLCanvas::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
479 // Do nothing, to avoid flashing.