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 #if defined(__WXMAC__) || defined(__WXCOCOA__)
33 # include <OpenGL/gl.h>
34 # include <OpenGL/glu.h>
44 // disabled because this has apparently changed in OpenGL 1.2, so doesn't link
45 // correctly if this is on...
46 #ifdef GL_EXT_vertex_array
47 #undef GL_EXT_vertex_array
52 #include "../../sample.xpm"
54 // The following part is taken largely unchanged from the original C Version
56 GLboolean speed_test
= GL_FALSE
;
57 GLboolean use_vertex_arrays
= GL_FALSE
;
59 GLboolean doubleBuffer
= GL_TRUE
;
61 GLboolean smooth
= GL_TRUE
;
62 GLboolean lighting
= GL_TRUE
;
65 #define MAXVERTS 10000
67 static GLfloat verts
[MAXVERTS
][3];
68 static GLfloat norms
[MAXVERTS
][3];
69 static GLint numverts
;
75 static void read_surface( const wxChar
*filename
)
77 FILE *f
= wxFopen(filename
,_T("r"));
80 wxString msg
= _T("Couldn't read ");
87 while (!feof(f
) && numverts
<MAXVERTS
)
89 fscanf( f
, "%f %f %f %f %f %f",
90 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
91 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
97 wxPrintf(_T("%d vertices, %d triangles\n"), numverts
, numverts
-2);
103 static void draw_surface()
107 #ifdef GL_EXT_vertex_array
108 if (use_vertex_arrays
)
110 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
115 glBegin( GL_TRIANGLE_STRIP
);
116 for (i
=0;i
<numverts
;i
++)
118 glNormal3fv( norms
[i
] );
119 glVertex3fv( verts
[i
] );
128 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
130 glRotatef( yrot
, 0.0f
, 1.0f
, 0.0f
);
131 glRotatef( xrot
, 1.0f
, 0.0f
, 0.0f
);
141 static void InitMaterials()
143 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
144 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
145 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
146 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
147 static const GLfloat front_mat_shininess
[1] = {60.0f
};
148 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
149 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
151 static const GLfloat back_mat_shininess[1] = {60.0f};
152 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
153 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
155 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
156 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
158 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
159 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
160 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
163 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
164 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
165 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
168 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
169 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
170 glEnable(GL_LIGHTING
);
172 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
173 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
174 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
178 static void Init(void)
180 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
182 glShadeModel(GL_SMOOTH
);
183 glEnable(GL_DEPTH_TEST
);
187 glMatrixMode(GL_PROJECTION
);
189 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
191 glMatrixMode(GL_MODELVIEW
);
193 glTranslatef( 0.0, 0.0, -6.0 );
195 #ifdef GL_EXT_vertex_array
196 if (use_vertex_arrays
)
198 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
199 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
200 glEnable( GL_VERTEX_ARRAY_EXT
);
201 glEnable( GL_NORMAL_ARRAY_EXT
);
206 static GLenum
Args(int argc
, wxChar
**argv
)
210 for (i
= 1; i
< argc
; i
++)
212 if (wxStrcmp(argv
[i
], _T("-sb")) == 0)
214 doubleBuffer
= GL_FALSE
;
216 else if (wxStrcmp(argv
[i
], _T("-db")) == 0)
218 doubleBuffer
= GL_TRUE
;
220 else if (wxStrcmp(argv
[i
], _T("-speed")) == 0)
222 speed_test
= GL_TRUE
;
223 doubleBuffer
= GL_TRUE
;
225 else if (wxStrcmp(argv
[i
], _T("-va")) == 0)
227 use_vertex_arrays
= GL_TRUE
;
231 wxString msg
= _T("Bad option: ");
241 // The following part was written for wxWidgets 1.66
242 MyFrame
*frame
= NULL
;
246 // `Main program' equivalent, creating windows and returning main app frame
251 // Create the main frame window
252 frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
253 wxDefaultPosition
, wxDefaultSize
);
256 frame
->SetIcon(wxIcon(_T("mondrian")));
259 wxMenu
*fileMenu
= new wxMenu
;
261 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
262 wxMenuBar
*menuBar
= new wxMenuBar
;
263 menuBar
->Append(fileMenu
, _T("&File"));
264 frame
->SetMenuBar(menuBar
);
266 // Make a TestGLCanvas
270 int *gl_attrib
= NULL
;
272 int gl_attrib
[20] = { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
273 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
284 printf("don't have double buffer, disabling\n");
288 doubleBuffer
= GL_FALSE
;
291 frame
->m_canvas
= new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
292 wxDefaultSize
, 0, _T("TestGLCanvas"), gl_attrib
);
297 frame
->m_canvas
->SetCurrent();
298 read_surface( _T("isosurf.dat") );
305 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
306 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
309 // My frame constructor
310 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
311 const wxSize
& size
, long style
)
312 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
315 SetIcon(wxIcon(sample_xpm
));
323 // Intercept menu commands
324 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
326 // true is to force the frame to close
331 * TestGLCanvas implementation
334 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
335 EVT_SIZE(TestGLCanvas::OnSize
)
336 EVT_PAINT(TestGLCanvas::OnPaint
)
337 EVT_CHAR(TestGLCanvas::OnChar
)
338 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
339 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
342 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
343 const wxPoint
& pos
, const wxSize
& size
, long style
,
344 const wxString
& name
, int* gl_attrib
)
345 : wxGLCanvas(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
, name
, gl_attrib
)
350 /* Make sure server supports the vertex array extension */
351 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
352 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" ))
354 use_vertex_arrays
= GL_FALSE
;
359 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
361 // This is a dummy, to avoid an endless succession of paint messages.
362 // OnPaint handlers must always create a wxPaintDC.
366 if (!GetContext()) return;
375 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
377 // this is also necessary to update the context on some platforms
378 wxGLCanvas::OnSize(event
);
380 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
382 GetClientSize(&w
, &h
);
388 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
392 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
394 switch( event
.GetKeyCode() )
397 wxTheApp
->ExitMainLoop();
420 glShadeModel(GL_SMOOTH
);
424 glShadeModel(GL_FLAT
);
429 lighting
= !lighting
;
432 glEnable(GL_LIGHTING
);
436 glDisable(GL_LIGHTING
);
448 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
450 static int dragging
= 0;
451 static float last_x
, last_y
;
453 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
454 if(event
.LeftIsDown())
462 yrot
+= (event
.GetX() - last_x
)*1.0;
463 xrot
+= (event
.GetY() - last_y
)*1.0;
466 last_x
= event
.GetX();
467 last_y
= event
.GetY();
474 void TestGLCanvas::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
476 // Do nothing, to avoid flashing.