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 // The following part is taken largely unchanged from the original C Version
59 GLboolean speed_test
= GL_FALSE
;
60 GLboolean use_vertex_arrays
= GL_FALSE
;
62 GLboolean doubleBuffer
= GL_TRUE
;
64 GLboolean smooth
= GL_TRUE
;
65 GLboolean lighting
= GL_TRUE
;
68 #define MAXVERTS 10000
70 static GLfloat verts
[MAXVERTS
][3];
71 static GLfloat norms
[MAXVERTS
][3];
72 static GLint numverts
;
78 static void read_surface( const wxChar
*filename
)
80 FILE *f
= wxFopen(wxFNCONV(filename
),_T("r"));
83 wxString msg
= _T("Couldn't read ");
90 while (!feof(f
) && numverts
<MAXVERTS
)
92 fscanf( f
, "%f %f %f %f %f %f",
93 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
94 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
100 wxPrintf(_T("%d vertices, %d triangles\n"), numverts
, numverts
-2);
106 static void draw_surface()
110 #ifdef GL_EXT_vertex_array
111 if (use_vertex_arrays
)
113 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
118 glBegin( GL_TRIANGLE_STRIP
);
119 for (i
=0;i
<numverts
;i
++)
121 glNormal3fv( norms
[i
] );
122 glVertex3fv( verts
[i
] );
131 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
133 glRotatef( yrot
, 0.0f
, 1.0f
, 0.0f
);
134 glRotatef( xrot
, 1.0f
, 0.0f
, 0.0f
);
144 static void InitMaterials()
146 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
147 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
148 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
149 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
150 static const GLfloat front_mat_shininess
[1] = {60.0f
};
151 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
152 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
154 static const GLfloat back_mat_shininess[1] = {60.0f};
155 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
156 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
158 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
159 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
161 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
162 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
163 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
166 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
167 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
168 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
171 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
172 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
173 glEnable(GL_LIGHTING
);
175 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
176 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
177 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
181 static void Init(void)
183 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
185 glShadeModel(GL_SMOOTH
);
186 glEnable(GL_DEPTH_TEST
);
190 glMatrixMode(GL_PROJECTION
);
192 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
194 glMatrixMode(GL_MODELVIEW
);
196 glTranslatef( 0.0, 0.0, -6.0 );
198 #ifdef GL_EXT_vertex_array
199 if (use_vertex_arrays
)
201 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
202 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
203 glEnable( GL_VERTEX_ARRAY_EXT
);
204 glEnable( GL_NORMAL_ARRAY_EXT
);
209 static GLenum
Args(int argc
, wxChar
**argv
)
213 for (i
= 1; i
< argc
; i
++)
215 if (wxStrcmp(argv
[i
], _T("-sb")) == 0)
217 doubleBuffer
= GL_FALSE
;
219 else if (wxStrcmp(argv
[i
], _T("-db")) == 0)
221 doubleBuffer
= GL_TRUE
;
223 else if (wxStrcmp(argv
[i
], _T("-speed")) == 0)
225 speed_test
= GL_TRUE
;
226 doubleBuffer
= GL_TRUE
;
228 else if (wxStrcmp(argv
[i
], _T("-va")) == 0)
230 use_vertex_arrays
= GL_TRUE
;
234 wxString msg
= _T("Bad option: ");
244 // The following part was written for wxWidgets 1.66
245 MyFrame
*frame
= NULL
;
249 // `Main program' equivalent, creating windows and returning main app frame
254 // Create the main frame window
255 frame
= new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
256 wxDefaultPosition
, wxDefaultSize
);
259 frame
->SetIcon(wxIcon(_T("mondrian")));
262 wxMenu
*fileMenu
= new wxMenu
;
264 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
265 wxMenuBar
*menuBar
= new wxMenuBar
;
266 menuBar
->Append(fileMenu
, _T("&File"));
267 frame
->SetMenuBar(menuBar
);
269 // Make a TestGLCanvas
273 int *gl_attrib
= NULL
;
275 int gl_attrib
[20] = { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
276 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
287 printf("don't have double buffer, disabling\n");
291 doubleBuffer
= GL_FALSE
;
294 frame
->m_canvas
= new TestGLCanvas(frame
, wxID_ANY
, wxDefaultPosition
,
295 wxDefaultSize
, 0, _T("TestGLCanvas"), gl_attrib
);
300 frame
->m_canvas
->SetCurrent();
301 read_surface( _T("isosurf.dat") );
308 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
309 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
312 // My frame constructor
313 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
314 const wxSize
& size
, long style
)
315 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
)
325 // Intercept menu commands
326 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
328 // true is to force the frame to close
333 * TestGLCanvas implementation
336 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
337 EVT_SIZE(TestGLCanvas::OnSize
)
338 EVT_PAINT(TestGLCanvas::OnPaint
)
339 EVT_CHAR(TestGLCanvas::OnChar
)
340 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
341 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
344 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
345 const wxPoint
& pos
, const wxSize
& size
, long style
,
346 const wxString
& name
, int* gl_attrib
)
347 : wxGLCanvas(parent
, id
, pos
, size
, style
, name
, gl_attrib
)
352 /* Make sure server supports the vertex array extension */
353 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
354 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" ))
356 use_vertex_arrays
= GL_FALSE
;
361 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
363 // This is a dummy, to avoid an endless succession of paint messages.
364 // OnPaint handlers must always create a wxPaintDC.
368 if (!GetContext()) return;
377 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
379 // this is also necessary to update the context on some platforms
380 wxGLCanvas::OnSize(event
);
382 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
384 GetClientSize(&w
, &h
);
390 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
394 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
396 switch( event
.GetKeyCode() )
399 wxTheApp
->ExitMainLoop();
422 glShadeModel(GL_SMOOTH
);
426 glShadeModel(GL_FLAT
);
431 lighting
= !lighting
;
434 glEnable(GL_LIGHTING
);
438 glDisable(GL_LIGHTING
);
450 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
452 static int dragging
= 0;
453 static float last_x
, last_y
;
455 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
456 if(event
.LeftIsDown())
464 yrot
+= (event
.GetX() - last_x
)*1.0;
465 xrot
+= (event
.GetY() - last_y
)*1.0;
468 last_x
= event
.GetX();
469 last_y
= event
.GetY();
476 void TestGLCanvas::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
478 // Do nothing, to avoid flashing.