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
53 #include "../../sample.xpm"
55 // The following part is taken largely unchanged from the original C Version
57 GLboolean speed_test
= GL_FALSE
;
58 GLboolean use_vertex_arrays
= GL_FALSE
;
60 GLboolean doubleBuffer
= GL_TRUE
;
62 GLboolean smooth
= GL_TRUE
;
63 GLboolean lighting
= GL_TRUE
;
66 #define MAXVERTS 10000
68 static GLfloat verts
[MAXVERTS
][3];
69 static GLfloat norms
[MAXVERTS
][3];
70 static GLint numverts
;
76 static void read_surface(const char *filename
)
78 std::ifstream
inFile(filename
);
83 wxLogError("Couldn't read \"%s\"", filename
);
87 while ((inFile
>> verts
[numverts
][0] >> verts
[numverts
][1] >> verts
[numverts
][2]
88 >> norms
[numverts
][0] >> norms
[numverts
][1] >> norms
[numverts
][2]) && numverts
<MAXVERTS
)
93 wxPrintf(_T("%d vertices, %d triangles\n"), numverts
, numverts
-2);
97 static void draw_surface()
101 #ifdef GL_EXT_vertex_array
102 if (use_vertex_arrays
)
104 glDrawArraysEXT( GL_TRIANGLE_STRIP
, 0, numverts
);
109 glBegin( GL_TRIANGLE_STRIP
);
110 for (i
=0;i
<numverts
;i
++)
112 glNormal3fv( norms
[i
] );
113 glVertex3fv( verts
[i
] );
122 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
124 glRotatef( yrot
, 0.0f
, 1.0f
, 0.0f
);
125 glRotatef( xrot
, 1.0f
, 0.0f
, 0.0f
);
131 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
135 static void InitMaterials()
137 static const GLfloat ambient
[4] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
138 static const GLfloat diffuse
[4] = {0.5f
, 1.0f
, 1.0f
, 1.0f
};
139 static const GLfloat position0
[4] = {0.0f
, 0.0f
, 20.0f
, 0.0f
};
140 static const GLfloat position1
[4] = {0.0f
, 0.0f
, -20.0f
, 0.0f
};
141 static const GLfloat front_mat_shininess
[1] = {60.0f
};
142 static const GLfloat front_mat_specular
[4] = {0.2f
, 0.2f
, 0.2f
, 1.0f
};
143 static const GLfloat front_mat_diffuse
[4] = {0.5f
, 0.28f
, 0.38f
, 1.0f
};
145 static const GLfloat back_mat_shininess[1] = {60.0f};
146 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
147 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
149 static const GLfloat lmodel_ambient
[4] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
150 static const GLfloat lmodel_twoside
[1] = {GL_FALSE
};
152 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
153 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
154 glLightfv(GL_LIGHT0
, GL_POSITION
, position0
);
157 glLightfv(GL_LIGHT1
, GL_AMBIENT
, ambient
);
158 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, diffuse
);
159 glLightfv(GL_LIGHT1
, GL_POSITION
, position1
);
162 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
163 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
164 glEnable(GL_LIGHTING
);
166 glMaterialfv(GL_FRONT_AND_BACK
, GL_SHININESS
, front_mat_shininess
);
167 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, front_mat_specular
);
168 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, front_mat_diffuse
);
172 static void Init(void)
174 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
176 glShadeModel(GL_SMOOTH
);
177 glEnable(GL_DEPTH_TEST
);
181 glMatrixMode(GL_PROJECTION
);
183 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
185 glMatrixMode(GL_MODELVIEW
);
187 glTranslatef( 0.0, 0.0, -6.0 );
189 #ifdef GL_EXT_vertex_array
190 if (use_vertex_arrays
)
192 glVertexPointerEXT( 3, GL_FLOAT
, 0, numverts
, verts
);
193 glNormalPointerEXT( GL_FLOAT
, 0, numverts
, norms
);
194 glEnable( GL_VERTEX_ARRAY_EXT
);
195 glEnable( GL_NORMAL_ARRAY_EXT
);
200 static GLenum
Args(int argc
, wxChar
**argv
)
204 for (i
= 1; i
< argc
; i
++)
206 if (wxStrcmp(argv
[i
], _T("-sb")) == 0)
208 doubleBuffer
= GL_FALSE
;
210 else if (wxStrcmp(argv
[i
], _T("-db")) == 0)
212 doubleBuffer
= GL_TRUE
;
214 else if (wxStrcmp(argv
[i
], _T("-speed")) == 0)
216 speed_test
= GL_TRUE
;
217 doubleBuffer
= GL_TRUE
;
219 else if (wxStrcmp(argv
[i
], _T("-va")) == 0)
221 use_vertex_arrays
= GL_TRUE
;
225 wxString msg
= _T("Bad option: ");
238 // `Main program' equivalent, creating windows and returning main app frame
241 if ( !wxApp::OnInit() )
246 // Create the main frame window
247 new MyFrame(NULL
, wxT("wxWidgets OpenGL Isosurf Sample"),
248 wxDefaultPosition
, wxDefaultSize
);
250 read_surface("isosurf.dat");
257 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
258 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
261 // My frame constructor
262 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
263 const wxSize
& size
, long style
)
264 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
, style
),
267 SetIcon(wxICON(sample
));
271 wxMenu
*fileMenu
= new wxMenu
;
273 fileMenu
->Append(wxID_EXIT
, _T("E&xit"));
274 wxMenuBar
*menuBar
= new wxMenuBar
;
275 menuBar
->Append(fileMenu
, _T("&File"));
279 // Make a TestGLCanvas
283 int *gl_attrib
= NULL
;
285 int gl_attrib
[20] = { WX_GL_RGBA
, WX_GL_MIN_RED
, 1, WX_GL_MIN_GREEN
, 1,
286 WX_GL_MIN_BLUE
, 1, WX_GL_DEPTH_SIZE
, 1,
288 # if defined(__WXMAC__) || defined(__WXCOCOA__)
297 printf("don't have double buffer, disabling\n");
301 doubleBuffer
= GL_FALSE
;
307 m_canvas
= new TestGLCanvas(this, wxID_ANY
, wxDefaultPosition
,
308 GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib
);
316 // Intercept menu commands
317 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
319 // true is to force the frame to close
324 * TestGLCanvas implementation
327 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
328 EVT_SIZE(TestGLCanvas::OnSize
)
329 EVT_PAINT(TestGLCanvas::OnPaint
)
330 EVT_CHAR(TestGLCanvas::OnChar
)
331 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent
)
334 TestGLCanvas::TestGLCanvas(wxWindow
*parent
,
339 const wxString
& name
,
341 : wxGLCanvas(parent
, id
, gl_attrib
, pos
, size
,
342 style
| wxFULL_REPAINT_ON_RESIZE
, name
)
344 // Explicitly create a new rendering context instance for this canvas.
345 m_glRC
= new wxGLContext(this);
347 // Make the new context current (activate it for use) with this canvas.
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
;
358 TestGLCanvas::~TestGLCanvas()
363 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
365 // This is a dummy, to avoid an endless succession of paint messages.
366 // OnPaint handlers must always create a wxPaintDC.
369 // This is normally only necessary if there is more than one wxGLCanvas
370 // or more than one wxGLContext in the application.
377 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
379 // This is normally only necessary if there is more than one wxGLCanvas
380 // or more than one wxGLContext in the application.
383 // It's up to the application code to update the OpenGL viewport settings.
384 // This is OK here only because there is only one canvas that uses the
385 // context. See the cube sample for that case that multiple canvases are
386 // made current with one context.
387 glViewport(0, 0, event
.GetSize().x
, event
.GetSize().y
);
390 void TestGLCanvas::OnChar(wxKeyEvent
& event
)
392 switch( event
.GetKeyCode() )
395 wxTheApp
->ExitMainLoop();
418 glShadeModel(GL_SMOOTH
);
422 glShadeModel(GL_FLAT
);
427 lighting
= !lighting
;
430 glEnable(GL_LIGHTING
);
434 glDisable(GL_LIGHTING
);
446 void TestGLCanvas::OnMouseEvent(wxMouseEvent
& event
)
448 static int dragging
= 0;
449 static float last_x
, last_y
;
451 // Allow default processing to happen, or else the canvas cannot gain focus
455 if(event
.LeftIsDown())
463 yrot
+= (event
.GetX() - last_x
)*1.0;
464 xrot
+= (event
.GetY() - last_y
)*1.0;
467 last_x
= event
.GetX();
468 last_y
= event
.GetY();