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 printf("don't have double buffer, disabling\n");
263 doubleBuffer
= GL_FALSE
;
265 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200), 0, "TestGLCanvas",
271 frame
->m_canvas
->SetCurrent();
272 read_surface( "isosurf.dat" );
279 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
280 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
283 // My frame constructor
284 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
285 const wxSize
& size
, long style
):
286 wxFrame(frame
, -1, title
, pos
, size
, style
)
291 // Intercept menu commands
292 void MyFrame::OnExit(wxCommandEvent
& event
)
298 * TestGLCanvas implementation
301 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
302 EVT_SIZE(TestGLCanvas::OnSize
)
303 EVT_PAINT(TestGLCanvas::OnPaint
)
304 EVT_CHAR(TestGLCanvas::OnChar
)
305 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
306 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
309 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
310 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, int* gl_attrib
):
311 wxGLCanvas(parent
, id
, pos
, size
, style
, name
, gl_attrib
)
315 /* Make sure server supports the vertex array extension */
316 char* extensions
= (char *) glGetString( GL_EXTENSIONS
);
317 if (!extensions
|| !strstr( extensions
, "GL_EXT_vertex_array" )) {
318 use_vertex_arrays
= GL_FALSE
;
323 TestGLCanvas::~TestGLCanvas(void)
327 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
329 // This is a dummy, to avoid an endless succession of paint messages.
330 // OnPaint handlers must always create a wxPaintDC.
337 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
341 GetClientSize(& width
, & height
);
342 Reshape(width
, height
);
345 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
347 switch(event
.KeyCode()) {
365 glShadeModel(GL_SMOOTH
);
367 glShadeModel(GL_FLAT
);
371 lighting
= !lighting
;
373 glEnable(GL_LIGHTING
);
375 glDisable(GL_LIGHTING
);
388 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
390 static int dragging
= 0;
391 static float last_x
, last_y
;
393 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
394 if(event
.LeftIsDown()) {
398 yrot
+= (event
.GetX() - last_x
)*1.0;
399 xrot
+= (event
.GetY() - last_y
)*1.0;
402 last_x
= event
.GetX();
403 last_y
= event
.GetY();
408 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
410 // Do nothing, to avoid flashing.