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"
36 // The following part is taken largely unchanged from the original C Version
40 GLboolean speed_test
= GL_FALSE
;
41 GLboolean use_vertex_arrays
= GL_FALSE
;
43 GLboolean doubleBuffer
= GL_TRUE
;
45 GLboolean smooth
= GL_TRUE
;
46 GLboolean lighting
= GL_TRUE
;
49 #define MAXVERTS 10000
51 static GLfloat verts
[MAXVERTS
][3];
52 static GLfloat norms
[MAXVERTS
][3];
53 static GLint numverts
;
59 static void read_surface( char *filename
)
63 f
= fopen(filename
,"r");
65 wxString
msg("Couldn't read ");
72 while (!feof(f
) && numverts
<MAXVERTS
) {
73 fscanf( f
, "%f %f %f %f %f %f",
74 &verts
[numverts
][0], &verts
[numverts
][1], &verts
[numverts
][2],
75 &norms
[numverts
][0], &norms
[numverts
][1], &norms
[numverts
][2] );
80 printf("%d vertices, %d triangles\n", numverts
, numverts
-2);
85 static void draw_surface( void )
89 #ifdef GL_EXT_vertex_array
90 if (use_vertex_arrays
) {
91 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
95 glBegin( GL_TRIANGLE_STRIP
);
96 for (i
=0;i
<numverts
;i
++) {
97 glNormal3fv( norms
[i
] );
98 glVertex3fv( verts
[i
] );
101 #ifdef GL_EXT_vertex_array
107 static void draw1(void)
109 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
111 glRotatef( yrot
, 0.0, 1.0, 0.0 );
112 glRotatef( xrot
, 1.0, 0.0, 0.0 );
122 static void InitMaterials(void)
124 static float ambient
[] = {0.1, 0.1, 0.1, 1.0};
125 static float diffuse
[] = {0.5, 1.0, 1.0, 1.0};
126 static float position0
[] = {0.0, 0.0, 20.0, 0.0};
127 static float position1
[] = {0.0, 0.0, -20.0, 0.0};
128 static float front_mat_shininess
[] = {60.0};
129 static float front_mat_specular
[] = {0.2, 0.2, 0.2, 1.0};
130 static float front_mat_diffuse
[] = {0.5, 0.28, 0.38, 1.0};
132 static float back_mat_shininess[] = {60.0};
133 static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
134 static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
136 static float lmodel_ambient
[] = {1.0, 1.0, 1.0, 1.0};
137 static float lmodel_twoside
[] = {GL_FALSE
};
139 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
140 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
141 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
144 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
145 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
146 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
149 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
150 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
151 glEnable(GL_LIGHTING
);
153 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
154 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
155 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
159 static void Init(void)
161 glClearColor(0.0, 0.0, 0.0, 0.0);
163 glShadeModel(GL_SMOOTH
);
164 glEnable(GL_DEPTH_TEST
);
168 glMatrixMode(GL_PROJECTION
);
170 glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
172 glMatrixMode(GL_MODELVIEW
);
174 glTranslatef( 0.0, 0.0, -6.0 );
176 #ifdef GL_EXT_vertex_array
177 if (use_vertex_arrays
) {
178 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
179 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
180 glEnable( GL_VERTEX_ARRAY_EXT
);
181 glEnable( GL_NORMAL_ARRAY_EXT
);
187 static void Reshape(int width
, int height
)
189 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
193 static GLenum
Args(int argc
, char **argv
)
197 for (i
= 1; i
< argc
; i
++) {
198 if (strcmp(argv
[i
], "-sb") == 0) {
199 doubleBuffer
= GL_FALSE
;
201 else if (strcmp(argv
[i
], "-db") == 0) {
202 doubleBuffer
= GL_TRUE
;
204 else if (strcmp(argv
[i
], "-speed") == 0) {
205 speed_test
= GL_TRUE
;
206 doubleBuffer
= GL_TRUE
;
208 else if (strcmp(argv
[i
], "-va") == 0) {
209 use_vertex_arrays
= GL_TRUE
;
212 wxString
msg("Bad option: ");
222 // The following part was written for wxWindows 1.66
223 MyFrame
*frame
= NULL
;
227 // `Main program' equivalent, creating windows and returning main app frame
228 bool MyApp::OnInit(void)
232 // Create the main frame window
233 frame
= new MyFrame(NULL
, "Isosurf GL Sample", wxPoint(50, 50), wxSize(200, 200));
236 frame
->SetIcon(wxIcon("mondrian"));
239 wxMenu
*fileMenu
= new wxMenu
;
241 fileMenu
->Append(wxID_EXIT
, "E&xit");
242 wxMenuBar
*menuBar
= new wxMenuBar
;
243 menuBar
->Append(fileMenu
, "&File");
244 frame
->SetMenuBar(menuBar
);
246 // Make a TestGLCanvas
250 int *gl_attrib
= NULL
;
252 int gl_attrib
[20] = { GLX_RGBA
, GLX_RED_SIZE
, 1, GLX_GREEN_SIZE
, 1,
253 GLX_BLUE_SIZE
, 1, GLX_DEPTH_SIZE
, 1,
254 GLX_DOUBLEBUFFER
, None
};
259 || !wxGLCanvas::HaveVisual(gl_attrib
)
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
)
301 bool MyFrame::OnClose(void)
308 * TestGLCanvas implementation
311 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
312 EVT_SIZE(TestGLCanvas::OnSize
)
313 EVT_PAINT(TestGLCanvas::OnPaint
)
314 EVT_CHAR(TestGLCanvas::OnChar
)
315 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
316 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
319 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
320 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, int* gl_attrib
):
321 wxGLCanvas(parent
, id
, pos
, size
, style
, name
, gl_attrib
)
325 /* Make sure server supports the vertex array extension */
326 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
327 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" )) {
328 use_vertex_arrays
= GL_FALSE
;
333 TestGLCanvas::~TestGLCanvas(void)
337 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
339 // This is a dummy, to avoid an endless succession of paint messages.
340 // OnPaint handlers must always create a wxPaintDC.
347 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
351 GetClientSize(& width
, & height
);
352 Reshape(width
, height
);
355 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
357 switch(event
.KeyCode()) {
375 glShadeModel(GL_SMOOTH
);
377 glShadeModel(GL_FLAT
);
381 lighting
= !lighting
;
383 glEnable(GL_LIGHTING
);
385 glDisable(GL_LIGHTING
);
393 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
395 static int dragging
= 0;
396 static float last_x
, last_y
;
398 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
399 if(event
.LeftIsDown()) {
403 yrot
+= (event
.GetX() - last_x
)*1.0;
404 xrot
+= (event
.GetY() - last_y
)*1.0;
407 last_x
= event
.GetX();
408 last_y
= event
.GetY();
413 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
415 // Do nothing, to avoid flashing.