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 Please set wxUSE_GLCANVAS to 1 in setup.h.
33 #include "wx/glcanvas.h"
40 // The following part is taken largely unchanged from the original C Version
44 GLboolean speed_test
= GL_FALSE
;
45 GLboolean use_vertex_arrays
= GL_FALSE
;
47 GLboolean doubleBuffer
= GL_TRUE
;
49 GLboolean smooth
= GL_TRUE
;
50 GLboolean lighting
= GL_TRUE
;
53 #define MAXVERTS 10000
55 static GLfloat verts
[MAXVERTS
][3];
56 static GLfloat norms
[MAXVERTS
][3];
57 static GLint numverts
;
63 static void read_surface( char *filename
)
67 f
= fopen(filename
,"r");
69 wxString
msg("Couldn't read ");
76 while (!feof(f
) && numverts
<MAXVERTS
) {
77 fscanf( f
, "%f %f %f %f %f %f",
78 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
79 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
84 printf("%d vertices, %d triangles\n", numverts
, numverts
-2);
89 static void draw_surface( void )
93 #ifdef GL_EXT_vertex_array
94 if (use_vertex_arrays
) {
95 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
99 glBegin( GL_TRIANGLE_STRIP
);
100 for (i
=0;i
<numverts
;i
++) {
101 glNormal3fv( norms
[i
] );
102 glVertex3fv( verts
[i
] );
105 #ifdef GL_EXT_vertex_array
111 static void draw1(void)
113 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
115 glRotatef( yrot
, 0.0, 1.0, 0.0 );
116 glRotatef( xrot
, 1.0, 0.0, 0.0 );
126 static void InitMaterials(void)
128 static float ambient
[] = {0.1, 0.1, 0.1, 1.0};
129 static float diffuse
[] = {0.5, 1.0, 1.0, 1.0};
130 static float position0
[] = {0.0, 0.0, 20.0, 0.0};
131 static float position1
[] = {0.0, 0.0, -20.0, 0.0};
132 static float front_mat_shininess
[] = {60.0};
133 static float front_mat_specular
[] = {0.2, 0.2, 0.2, 1.0};
134 static float front_mat_diffuse
[] = {0.5, 0.28, 0.38, 1.0};
136 static float back_mat_shininess[] = {60.0};
137 static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
138 static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
140 static float lmodel_ambient
[] = {1.0, 1.0, 1.0, 1.0};
141 static float lmodel_twoside
[] = {GL_FALSE
};
143 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
144 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
145 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
148 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
149 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
150 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
153 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
154 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
155 glEnable(GL_LIGHTING
);
157 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
158 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
159 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
163 static void Init(void)
165 glClearColor(0.0, 0.0, 0.0, 0.0);
167 glShadeModel(GL_SMOOTH
);
168 glEnable(GL_DEPTH_TEST
);
172 glMatrixMode(GL_PROJECTION
);
174 glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
176 glMatrixMode(GL_MODELVIEW
);
178 glTranslatef( 0.0, 0.0, -6.0 );
180 #ifdef GL_EXT_vertex_array
181 if (use_vertex_arrays
) {
182 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
183 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
184 glEnable( GL_VERTEX_ARRAY_EXT
);
185 glEnable( GL_NORMAL_ARRAY_EXT
);
191 static void Reshape(int width
, int height
)
193 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
197 static GLenum
Args(int argc
, char **argv
)
201 for (i
= 1; i
< argc
; i
++) {
202 if (strcmp(argv
[i
], "-sb") == 0) {
203 doubleBuffer
= GL_FALSE
;
205 else if (strcmp(argv
[i
], "-db") == 0) {
206 doubleBuffer
= GL_TRUE
;
208 else if (strcmp(argv
[i
], "-speed") == 0) {
209 speed_test
= GL_TRUE
;
210 doubleBuffer
= GL_TRUE
;
212 else if (strcmp(argv
[i
], "-va") == 0) {
213 use_vertex_arrays
= GL_TRUE
;
216 wxString
msg("Bad option: ");
226 // The following part was written for wxWindows 1.66
227 MyFrame
*frame
= NULL
;
231 // `Main program' equivalent, creating windows and returning main app frame
232 bool MyApp::OnInit(void)
236 // Create the main frame window
237 frame
= new MyFrame(NULL
, "Isosurf GL Sample", wxPoint(50, 50), wxSize(200, 200));
240 frame
->SetIcon(wxIcon("mondrian"));
243 wxMenu
*fileMenu
= new wxMenu
;
245 fileMenu
->Append(wxID_EXIT
, "E&xit");
246 wxMenuBar
*menuBar
= new wxMenuBar
;
247 menuBar
->Append(fileMenu
, "&File");
248 frame
->SetMenuBar(menuBar
);
250 // Make a TestGLCanvas
254 int *gl_attrib
= NULL
;
256 int gl_attrib
[20] = { GLX_RGBA
, GLX_RED_SIZE
, 1, GLX_GREEN_SIZE
, 1,
257 GLX_BLUE_SIZE
, 1, GLX_DEPTH_SIZE
, 1,
258 GLX_DOUBLEBUFFER
, None
};
263 printf("don't have double buffer, disabling\n");
267 doubleBuffer
= GL_FALSE
;
269 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200), 0, "TestGLCanvas",
275 frame
->m_canvas
->SetCurrent();
276 read_surface( "isosurf.dat" );
283 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
284 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
287 // My frame constructor
288 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
289 const wxSize
& size
, long style
):
290 wxFrame(frame
, -1, title
, pos
, size
, style
)
295 // Intercept menu commands
296 void MyFrame::OnExit(wxCommandEvent
& event
)
302 * TestGLCanvas implementation
305 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
306 EVT_SIZE(TestGLCanvas::OnSize
)
307 EVT_PAINT(TestGLCanvas::OnPaint
)
308 EVT_CHAR(TestGLCanvas::OnChar
)
309 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
310 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
313 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
314 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, int* gl_attrib
):
315 wxGLCanvas(parent
, id
, pos
, size
, style
, name
, gl_attrib
)
319 /* Make sure server supports the vertex array extension */
320 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
321 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" )) {
322 use_vertex_arrays
= GL_FALSE
;
327 TestGLCanvas::~TestGLCanvas(void)
331 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
333 // This is a dummy, to avoid an endless succession of paint messages.
334 // OnPaint handlers must always create a wxPaintDC.
338 if (!GetContext()) return;
345 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
348 if (!GetContext()) return;
353 GetClientSize(& width
, & height
);
354 Reshape(width
, height
);
357 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
359 switch(event
.KeyCode()) {
377 glShadeModel(GL_SMOOTH
);
379 glShadeModel(GL_FLAT
);
383 lighting
= !lighting
;
385 glEnable(GL_LIGHTING
);
387 glDisable(GL_LIGHTING
);
400 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
402 static int dragging
= 0;
403 static float last_x
, last_y
;
405 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
406 if(event
.LeftIsDown()) {
410 yrot
+= (event
.GetX() - last_x
)*1.0;
411 xrot
+= (event
.GetY() - last_y
)*1.0;
414 last_x
= event
.GetX();
415 last_y
= event
.GetY();
420 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
422 // Do nothing, to avoid flashing.