1 /////////////////////////////////////////////////////////////////////////////
2 // Name: pyramid.cpp
3 // Purpose: OpenGL version >= 3.2 sample
4 // Author: Manuel Martin
5 // Created: 2015/11/16
6 // Copyright: (c) 2015 Manuel Martin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #if !wxUSE_GLCANVAS
27 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
28 #endif
29
30 // Due to oglpfuncs.h needs to be included before gl.h (to avoid some declarations),
31 // we include glcanvas.h after oglstuff.h
32 #include "oglstuff.h"
33 #include "wx/glcanvas.h"
34 #include "pyramid.h"
35
36 // the application icon (under Windows and OS/2 it is in resources and even
37 // though we could still include the XPM here it would be unused)
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../../sample.xpm"
40 #endif
41
42 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
43 EVT_MENU(Pyramid_Quit, MyFrame::OnQuit)
44 EVT_MENU(Pyramid_About, MyFrame::OnAbout)
45 #if wxUSE_LOGWINDOW
46 EVT_MENU(Pyramid_LogW, MyFrame::OnLogWindow)
47 #endif // wxUSE_LOGWINDOW
48 wxEND_EVENT_TABLE()
49
50 wxIMPLEMENT_APP(MyApp);
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // the application class
58 // ----------------------------------------------------------------------------
59
60 // 'Main program' equivalent: the program execution "starts" here
OnInit()61 bool MyApp::OnInit()
62 {
63 if ( !wxApp::OnInit() )
64 return false;
65
66 // create the main application window
67 MyFrame* frame = new MyFrame("wxWidgets OpenGL Pyramid Sample");
68
69 //Exit if the required visual attributes or OGL context couldn't be created
70 if ( ! frame->OGLAvailable() )
71 return false;
72
73 // As of October 2015 GTK+ needs the frame to be shown before we call SetCurrent()
74 frame->Show(true);
75
76 return true;
77 }
78
79 // ----------------------------------------------------------------------------
80 // main frame
81 // ----------------------------------------------------------------------------
82
83 // frame constructor
MyFrame(const wxString & title)84 MyFrame::MyFrame(const wxString& title)
85 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500, 400))
86 {
87 // set the frame icon
88 SetIcon(wxICON(sample));
89
90 #if wxUSE_MENUS
91 // create a menu bar
92 wxMenu *fileMenu = new wxMenu;
93
94 // the "About" item should be in the help menu
95 wxMenu *helpMenu = new wxMenu;
96 helpMenu->Append(Pyramid_About, "&About\tF1", "Show about dialog");
97
98 #if wxUSE_LOGWINDOW
99 fileMenu->Append(Pyramid_LogW, "&Log window", "Open the log window");
100 fileMenu->AppendSeparator();
101 #endif // wxUSE_LOGWINDOW
102 fileMenu->Append(Pyramid_Quit, "E&xit\tAlt-X", "Quit this program");
103
104 // now append the freshly created menu to the menu bar...
105 wxMenuBar *menuBar = new wxMenuBar();
106 menuBar->Append(fileMenu, "&File");
107 menuBar->Append(helpMenu, "&Help");
108
109 // ... and attach this menu bar to the frame
110 SetMenuBar(menuBar);
111 #endif // wxUSE_MENUS
112
113 #if wxUSE_STATUSBAR
114 // create a status bar just for fun (by default with 1 pane only)
115 CreateStatusBar(2);
116 SetStatusText("Welcome to wxWidgets!");
117 #endif // wxUSE_STATUSBAR
118
119 #if wxUSE_LOGWINDOW
120 //Open a log window, don't show it though
121 m_LogWin = new wxLogWindow(NULL, "Pyramid log window", false, false);
122 wxLog::SetActiveTarget(m_LogWin);
123 #endif // wxUSE_LOGWINDOW
124
125 // The canvas
126 m_mycanvas = NULL;
127 wxGLAttributes vAttrs;
128 // Defaults should be accepted
129 vAttrs.PlatformDefaults().Defaults().EndList();
130 bool accepted = wxGLCanvas::IsDisplaySupported(vAttrs) ;
131
132 if ( accepted )
133 {
134 #if wxUSE_LOGWINDOW
135 wxLogMessage("The display supports required visual attributes.");
136 #endif // wxUSE_LOGWINDOW
137 }
138 else
139 {
140 #if wxUSE_LOGWINDOW
141 wxLogMessage("First try with OpenGL default visual attributes failed.");
142 #endif // wxUSE_LOGWINDOW
143 // Try again without sample buffers
144 vAttrs.Reset();
145 vAttrs.PlatformDefaults().RGBA().DoubleBuffer().Depth(16).EndList();
146 accepted = wxGLCanvas::IsDisplaySupported(vAttrs) ;
147
148 if ( !accepted )
149 {
150 wxMessageBox("Visual attributes for OpenGL are not accepted.\nThe app will exit now.",
151 "Error with OpenGL", wxOK | wxICON_ERROR);
152 }
153 else
154 {
155 #if wxUSE_LOGWINDOW
156 wxLogMessage("Second try with other visual attributes worked.");
157 #endif // wxUSE_LOGWINDOW
158 }
159 }
160
161 if ( accepted )
162 m_mycanvas = new MyGLCanvas(this, vAttrs);
163
164 SetMinSize(wxSize(250, 200));
165 }
166
167
168 // event handlers
169
OnQuit(wxCommandEvent & WXUNUSED (event))170 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
171 {
172 // true is to force the frame to close
173 Close(true);
174 }
175
OnAbout(wxCommandEvent & WXUNUSED (event))176 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
177 {
178 wxMessageBox(wxString::Format
179 (
180 "Welcome to %s!\n"
181 "\n"
182 "This is the wxWidgets OpenGL Pyramid sample.\n"
183 "%s\n",
184 wxVERSION_STRING,
185 m_OGLString
186 ),
187 "About wxWidgets pyramid sample",
188 wxOK | wxICON_INFORMATION,
189 this);
190 }
191
192 #if wxUSE_LOGWINDOW
OnLogWindow(wxCommandEvent & WXUNUSED (event))193 void MyFrame::OnLogWindow(wxCommandEvent& WXUNUSED(event))
194 {
195 if ( m_LogWin->GetFrame()->IsIconized() )
196 m_LogWin->GetFrame()->Restore();
197
198 if ( ! m_LogWin->GetFrame()->IsShown() )
199 m_LogWin->Show();
200
201 m_LogWin->GetFrame()->SetFocus();
202 }
203 #endif // wxUSE_LOGWINDOW
204
OGLAvailable()205 bool MyFrame::OGLAvailable()
206 {
207 //Test if visual attributes were accepted.
208 if ( ! m_mycanvas )
209 return false;
210
211 //Test if OGL context could be created.
212 return m_mycanvas->OglCtxAvailable();
213 }
214
215 // ----------------------------------------------------------------------------
216 // Function for receiving messages from OGLstuff and passing them to the log window
217 // ----------------------------------------------------------------------------
fOGLErrHandler(int err,int glerr,const GLchar * glMsg)218 void fOGLErrHandler(int err, int glerr, const GLchar* glMsg)
219 {
220 #if wxUSE_LOGWINDOW
221 wxString msg;
222
223 switch (err)
224 {
225 case myoglERR_SHADERCREATE:
226 msg = _("Error in shader creation.");
227 break;
228 case myoglERR_SHADERCOMPILE:
229 msg = _("Error in shader compilation.");
230 break;
231 case myoglERR_SHADERLINK:
232 msg = _("Error in shader linkage.");
233 break;
234 case myoglERR_SHADERLOCATION:
235 msg = _("Error: Can't get uniforms locations.");
236 break;
237 case myoglERR_BUFFER:
238 msg = _("Error: Can't load buffer. Likely out of GPU memory.");
239 break;
240 case myoglERR_TEXTIMAGE:
241 msg = _("Error: Can't load texture. Likely out of GPU memory.");
242 break;
243 case myoglERR_DRAWING_TRI:
244 msg = _("Error: Can't draw the triangles.");
245 break;
246 case myoglERR_DRAWING_STR:
247 msg = _("Error: Can't draw the string.");
248 break;
249 case myoglERR_JUSTLOG:
250 msg = _("Log info: ");
251 break;
252 default:
253 msg = _("Not a GL message.");
254 }
255
256 if ( glerr != GL_NO_ERROR )
257 msg += wxString::Format(_(" GL error %d. "), glerr);
258 else if ( err == 0 )
259 msg = _("Information: ");
260 else if ( err != myoglERR_JUSTLOG )
261 msg += _(" GL reports: ");
262
263 if ( glMsg != NULL )
264 msg += wxString::FromUTF8( reinterpret_cast<const char *>(glMsg) );
265
266 wxLogMessage(msg);
267 #endif // wxUSE_LOGWINDOW
268 }
269
270 // ----------------------------------------------------------------------------
271 // These two functions allow us to convert a wxString into a RGBA pixels array
272 // ----------------------------------------------------------------------------
273
274 // Creates a 4-bytes-per-pixel, RGBA array from a wxImage.
275 // If the image has alpha channel, it's used. If not, pixels with 'cTrans' color
276 // get 'cAlpha' alpha; an the rest of pixels get alpha=255 (opaque).
277 //
278 // NOTE: The returned pointer must be deleted somewhere in the app.
MyImgToArray(const wxImage & img,const wxColour & cTrans,unsigned char cAlpha)279 unsigned char* MyImgToArray(const wxImage& img, const wxColour& cTrans, unsigned char cAlpha)
280 {
281 int w = img.GetWidth();
282 int h = img.GetHeight();
283 int siz = w * h;
284 unsigned char *resArr = new unsigned char [siz * 4];
285 unsigned char *res = resArr;
286 unsigned char *sdata = img.GetData();
287 unsigned char *alpha = NULL;
288 if ( img.HasAlpha() )
289 alpha = img.GetAlpha();
290 // Pixel by pixel
291 for ( int i = 0; i < siz; i++ )
292 { //copy the colour
293 res[0] = sdata[0] ;
294 res[1] = sdata[1] ;
295 res[2] = sdata[2] ;
296 if ( alpha != NULL )
297 { //copy alpha
298 res[3] = alpha[i] ;
299 }
300 else
301 { // Colour cTrans gets cAlpha transparency
302 if ( res[0] == cTrans.Red() && res[1] == cTrans.Green() && res[2] == cTrans.Blue() )
303 res[3] = cAlpha;
304 else
305 res[3] = 255 ;
306 }
307 sdata += 3 ;
308 res += 4 ;
309 }
310
311 return resArr;
312 }
313
314 // Creates an array of bytes that defines the pixels of the string.
315 // The background color has cAlpha transparency. 0=transparent, 255=opaque
316 //
317 // NOTE: The returned pointer must be deleted somewhere in the app.
MyTextToPixels(const wxString & sText,const wxFont & sFont,const wxColour & sForeColo,const wxColour & sBackColo,unsigned char cAlpha,int * width,int * height)318 unsigned char* MyTextToPixels(const wxString& sText, // The string
319 const wxFont& sFont, // Font to use
320 const wxColour& sForeColo, // Foreground colour
321 const wxColour& sBackColo, // Background colour
322 unsigned char cAlpha, // Background transparency
323 int* width, int* height) // Image sizes
324 {
325 if ( sText.IsEmpty() )
326 return NULL;
327
328 // The dc where we temporally draw
329 wxMemoryDC mdc;
330
331 mdc.SetFont(sFont);
332
333 // Measure
334 mdc.GetMultiLineTextExtent(sText, width, height);
335
336 /* This code should be used for old graphics cards.
337 But this sample uses OGL Core Profile, so the card is not that old.
338
339 // Adjust sizes to power of two. Needed for old cards.
340 int sizP2 = 4;
341 while ( sizP2 < *width )
342 sizP2 *= 2;
343 *width = sizP2;
344 sizP2 = 4;
345 while ( sizP2 < *height )
346 sizP2 *= 2;
347 *height = sizP2;
348 */
349
350 // Now we know dimensions, let's draw into a memory dc
351 wxBitmap bmp(*width, *height, 24);
352 mdc.SelectObject(bmp);
353 // If we have multiline string, perhaps not all of the bmp is used
354 wxBrush brush(sBackColo);
355 mdc.SetBackground(brush);
356 mdc.Clear(); // Make sure all of bmp is cleared
357 // Colours
358 mdc.SetBackgroundMode(wxPENSTYLE_SOLID);
359 mdc.SetTextBackground(sBackColo);
360 mdc.SetTextForeground(sForeColo);
361 // We draw the string and get it as an image.
362 // NOTE: OpenGL axis are bottom to up. Be aware when setting the texture coords.
363 mdc.DrawText(sText, 0, 0);
364 mdc.SelectObject(wxNullBitmap); // bmp must be detached from wxMemoryDC
365
366 // Bytes from the image. Background pixels become transparent with the
367 // cAlpha transparency value.
368 unsigned char *res = MyImgToArray(bmp.ConvertToImage(), sBackColo, cAlpha);
369
370 return res;
371 }
372
373
374 // ----------------------------------------------------------------------------
375 // The canvas inside the frame. Our OpenGL connection
376 // ----------------------------------------------------------------------------
377
wxBEGIN_EVENT_TABLE(MyGLCanvas,wxGLCanvas)378 wxBEGIN_EVENT_TABLE(MyGLCanvas, wxGLCanvas)
379 EVT_PAINT(MyGLCanvas::OnPaint)
380 EVT_SIZE(MyGLCanvas::OnSize)
381 EVT_MOUSE_EVENTS(MyGLCanvas::OnMouse)
382 wxEND_EVENT_TABLE()
383
384 //We create a wxGLContext in this constructor.
385 //We do OGL initialization at OnSize().
386 MyGLCanvas::MyGLCanvas(MyFrame* parent, const wxGLAttributes& canvasAttrs)
387 : wxGLCanvas(parent, canvasAttrs)
388 {
389 m_parent = parent;
390
391 m_oglManager = NULL;
392 m_winHeight = 0; // We have not been sized yet
393
394 // Explicitly create a new rendering context instance for this canvas.
395 wxGLContextAttrs ctxAttrs;
396 #ifndef __WXMAC__
397 // An impossible context, just to test IsOk()
398 ctxAttrs.PlatformDefaults().OGLVersion(99, 2).EndList();
399 m_oglContext = new wxGLContext(this, NULL, &ctxAttrs);
400
401 if ( !m_oglContext->IsOK() )
402 {
403 #if wxUSE_LOGWINDOW
404 wxLogMessage("Trying to set OpenGL 99.2 failed, as expected.");
405 #endif // wxUSE_LOGWINDOW
406 delete m_oglContext;
407 ctxAttrs.Reset();
408 #endif //__WXMAC__
409 ctxAttrs.PlatformDefaults().CoreProfile().OGLVersion(3, 2).EndList();
410 m_oglContext = new wxGLContext(this, NULL, &ctxAttrs);
411 #ifndef __WXMAC__
412 }
413 #endif //__WXMAC__
414
415 if ( !m_oglContext->IsOK() )
416 {
417 wxMessageBox("This sample needs an OpenGL 3.2 capable driver.\nThe app will end now.",
418 "OpenGL version error", wxOK | wxICON_INFORMATION, this);
419 delete m_oglContext;
420 m_oglContext = NULL;
421 }
422 else
423 {
424 #if wxUSE_LOGWINDOW
425 wxLogMessage("OpenGL Core Profile 3.2 successfully set.");
426 #endif // wxUSE_LOGWINDOW
427 }
428
429 }
430
~MyGLCanvas()431 MyGLCanvas::~MyGLCanvas()
432 {
433 if ( m_oglContext )
434 SetCurrent(*m_oglContext);
435
436 if ( m_oglManager )
437 {
438 delete m_oglManager;
439 m_oglManager = NULL;
440 }
441
442 if ( m_oglContext )
443 {
444 delete m_oglContext;
445 m_oglContext = NULL;
446 }
447 }
448
oglInit()449 bool MyGLCanvas::oglInit()
450 {
451 if ( !m_oglContext )
452 return false;
453
454 // The current context must be set before we get OGL pointers
455 SetCurrent(*m_oglContext);
456
457 // Initialize our OGL pointers
458 if ( !myOGLManager::Init() )
459 {
460 wxMessageBox("Error: Some OpenGL pointer to function failed.",
461 "OpenGL initialization error", wxOK | wxICON_INFORMATION, this);
462 return false;
463 }
464
465 // Create our OGL manager, pass our OGL error handler
466 m_oglManager = new myOGLManager(&fOGLErrHandler);
467
468 // Get the GL version for the current OGL context
469 wxString sglVer = "\nUsing OpenGL version: ";
470 sglVer += wxString::FromUTF8(
471 reinterpret_cast<const char *>(m_oglManager->GetGLVersion()) );
472 // Also Vendor and Renderer
473 sglVer += "\nVendor: ";
474 sglVer += wxString::FromUTF8(
475 reinterpret_cast<const char *>(m_oglManager->GetGLVendor()) );
476 sglVer += "\nRenderer: ";
477 sglVer += wxString::FromUTF8(
478 reinterpret_cast<const char *>(m_oglManager->GetGLRenderer()) );
479 // For the menu "About" info
480 m_parent->SetOGLString(sglVer);
481
482 // Load some data into GPU
483 m_oglManager->SetShadersAndTriangles();
484
485 // This string will be placed on a face of the pyramid
486 int swi = 0, shi = 0; //Image sizes
487 wxString stg("wxWidgets");
488 // Set the font. Use a big pointsize so as to smoothing edges.
489 wxFont font(wxFontInfo(48).Family(wxFONTFAMILY_MODERN));
490 if ( !font.IsOk() )
491 font = *wxSWISS_FONT;
492 wxColour bgrdColo(*wxBLACK);
493 wxColour foreColo(160, 0, 200); // Dark purple
494 // Build an array with the pixels. Background fully transparent
495 unsigned char* sPixels = MyTextToPixels(stg, font, foreColo, bgrdColo, 0,
496 &swi, &shi);
497 // Send it to GPU
498 m_oglManager->SetStringOnPyr(sPixels, swi, shi);
499 delete[] sPixels; // That memory was allocated at MyTextToPixels
500
501 // This string is placed at left bottom of the window. Its size doesn't
502 // change with window size.
503 stg = "Rotate the pyramid with\nthe left mouse button";
504 font.SetPointSize(14);
505 bgrdColo = wxColour(40, 40, 255);
506 foreColo = wxColour(*wxWHITE);
507 unsigned char* stPixels = MyTextToPixels(stg, font, foreColo, bgrdColo, 80,
508 &swi, &shi);
509 m_oglManager->SetImmutableString(stPixels, swi, shi);
510 delete[] stPixels;
511
512 return true;
513 }
514
OnPaint(wxPaintEvent & WXUNUSED (event))515 void MyGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
516 {
517 // This is a dummy, to avoid an endless succession of paint messages.
518 // OnPaint handlers must always create a wxPaintDC.
519 wxPaintDC dc(this);
520
521 // Avoid painting when we have not yet a size
522 if ( m_winHeight < 1 || !m_oglManager )
523 return;
524
525 // This should not be needed, while we have only one canvas
526 SetCurrent(*m_oglContext);
527
528 // Do the magic
529 m_oglManager->Render();
530
531 SwapBuffers();
532 }
533
534 //Note:
535 // You may wonder why OpenGL initialization was not done at wxGLCanvas ctor.
536 // The reason is due to GTK+/X11 working asynchronously, we can't call
537 // SetCurrent() before the window is shown on screen (GTK+ doc's say that the
538 // window must be realized first).
539 // In wxGTK, window creation and sizing requires several size-events. At least
540 // one of them happens after GTK+ has notified the realization. We use this
541 // circumstance and do initialization then.
542
OnSize(wxSizeEvent & event)543 void MyGLCanvas::OnSize(wxSizeEvent& event)
544 {
545 event.Skip();
546
547 // If this window is not fully initialized, dismiss this event
548 if ( !IsShownOnScreen() )
549 return;
550
551 if ( !m_oglManager )
552 {
553 //Now we have a context, retrieve pointers to OGL functions
554 if ( !oglInit() )
555 return;
556 //Some GPUs need an additional forced paint event
557 PostSizeEvent();
558 }
559
560 // This is normally only necessary if there is more than one wxGLCanvas
561 // or more than one wxGLContext in the application.
562 SetCurrent(*m_oglContext);
563
564 // It's up to the application code to update the OpenGL viewport settings.
565 const wxSize size = event.GetSize() * GetContentScaleFactor();
566 m_winHeight = size.y;
567 m_oglManager->SetViewport(0, 0, size.x, m_winHeight);
568
569 // Generate paint event without erasing the background.
570 Refresh(false);
571 }
572
OnMouse(wxMouseEvent & event)573 void MyGLCanvas::OnMouse(wxMouseEvent& event)
574 {
575 event.Skip();
576
577 // GL 0 Y-coordinate is at bottom of the window
578 int oglwinY = m_winHeight - event.GetY();
579
580 if ( event.LeftIsDown() )
581 {
582 if ( ! event.Dragging() )
583 {
584 // Store positions
585 m_oglManager->OnMouseButDown(event.GetX(), oglwinY);
586 }
587 else
588 {
589 // Rotation
590 m_oglManager->OnMouseRotDragging( event.GetX(), oglwinY );
591
592 // Generate paint event without erasing the background.
593 Refresh(false);
594 }
595 }
596 }
597
598