]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/opengl/cube/cube.cpp
Further refine of #15226: wxRichTextCtrl: Implement setting properties with undo...
[wxWidgets.git] / samples / opengl / cube / cube.cpp
index 74cd1bf7ebf194a3877541138eaa2ad75130fc01..b07776050215d54b03863f0fe214264966959e18 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Julian Smart
 // Modified by: Vadim Zeitlin to use new wxGLCanvas API (2007-04-09)
 // Created:     04/01/98
 // Author:      Julian Smart
 // Modified by: Vadim Zeitlin to use new wxGLCanvas API (2007-04-09)
 // Created:     04/01/98
-// RCS-ID:      $Id$
 // Copyright:   (c) Julian Smart
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 // Copyright:   (c) Julian Smart
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "cube.h"
 
 
 #include "cube.h"
 
-#if !defined(__WXMSW__) && !defined(__WXPM__)
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../../sample.xpm"
 #endif
 
     #include "../../sample.xpm"
 #endif
 
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// control ids
+enum
+{
+    SpinTimer = wxID_HIGHEST + 1
+};
+
 // ----------------------------------------------------------------------------
 // helper functions
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // helper functions
 // ----------------------------------------------------------------------------
@@ -57,20 +66,20 @@ static void CheckGLError()
         // so check that we get a different error than the last time
         if ( err == errLast )
         {
         // so check that we get a different error than the last time
         if ( err == errLast )
         {
-            wxLogError(_T("OpenGL error state couldn't be reset."));
+            wxLogError(wxT("OpenGL error state couldn't be reset."));
             return;
         }
 
         errLast = err;
 
             return;
         }
 
         errLast = err;
 
-        wxLogError(_T("OpenGL error %d"), err);
+        wxLogError(wxT("OpenGL error %d"), err);
     }
 }
 
 // function to draw the texture for cube faces
 static wxImage DrawDice(int size, unsigned num)
 {
     }
 }
 
 // function to draw the texture for cube faces
 static wxImage DrawDice(int size, unsigned num)
 {
-    wxASSERT_MSG( num >= 1 && num <= 6, _T("invalid dice index") );
+    wxASSERT_MSG( num >= 1 && num <= 6, wxT("invalid dice index") );
 
     const int dot = size/16;        // radius of a single dot
     const int gap = 5*size/32;      // gap between dots
 
     const int dot = size/16;        // radius of a single dot
     const int gap = 5*size/32;      // gap between dots
@@ -128,6 +137,7 @@ TestGLContext::TestGLContext(wxGLCanvas *canvas)
     SetCurrent(*canvas);
 
     // set up the parameters we want to use
     SetCurrent(*canvas);
 
     // set up the parameters we want to use
+    glEnable(GL_CULL_FACE);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_LIGHTING);
     glEnable(GL_LIGHT0);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_LIGHTING);
     glEnable(GL_LIGHT0);
@@ -161,7 +171,7 @@ TestGLContext::TestGLContext(wxGLCanvas *canvas)
         const wxImage img(DrawDice(256, i + 1));
 
         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
         const wxImage img(DrawDice(256, i + 1));
 
         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(),
                      0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
     }
 
                      0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
     }
 
@@ -258,22 +268,38 @@ bool MyApp::OnInit()
 int MyApp::OnExit()
 {
     delete m_glContext;
 int MyApp::OnExit()
 {
     delete m_glContext;
+    delete m_glStereoContext;
 
     return wxApp::OnExit();
 }
 
 
     return wxApp::OnExit();
 }
 
-TestGLContext& MyApp::GetContext(wxGLCanvas *canvas)
+TestGLContext& MyApp::GetContext(wxGLCanvas *canvas, bool useStereo)
 {
 {
-    if ( !m_glContext )
+    TestGLContext *glContext;
+    if ( useStereo )
     {
     {
-        // Create the OpenGL context for the first window which needs it:
-        // subsequently created windows will all share the same context.
-        m_glContext = new TestGLContext(canvas);
+        if ( !m_glStereoContext )
+        {
+            // Create the OpenGL context for the first stereo window which needs it:
+            // subsequently created windows will all share the same context.
+            m_glStereoContext = new TestGLContext(canvas);
+        }
+        glContext = m_glStereoContext;
+    }
+    else
+    {
+        if ( !m_glContext )
+        {
+            // Create the OpenGL context for the first mono window which needs it:
+            // subsequently created windows will all share the same context.
+            m_glContext = new TestGLContext(canvas);
+        }
+        glContext = m_glContext;
     }
 
     }
 
-    m_glContext->SetCurrent(*canvas);
+    glContext->SetCurrent(*canvas);
 
 
-    return *m_glContext;
+    return *glContext;
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -283,19 +309,33 @@ TestGLContext& MyApp::GetContext(wxGLCanvas *canvas)
 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
     EVT_PAINT(TestGLCanvas::OnPaint)
     EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
     EVT_PAINT(TestGLCanvas::OnPaint)
     EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
+    EVT_TIMER(SpinTimer, TestGLCanvas::OnSpinTimer)
 END_EVENT_TABLE()
 
 END_EVENT_TABLE()
 
-TestGLCanvas::TestGLCanvas(wxWindow *parent)
+TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList)
     // With perspective OpenGL graphics, the wxFULL_REPAINT_ON_RESIZE style
     // flag should always be set, because even making the canvas smaller should
     // be followed by a paint event that updates the entire canvas with new
     // viewport settings.
     // With perspective OpenGL graphics, the wxFULL_REPAINT_ON_RESIZE style
     // flag should always be set, because even making the canvas smaller should
     // be followed by a paint event that updates the entire canvas with new
     // viewport settings.
-    : wxGLCanvas(parent, wxID_ANY, NULL /* attribs */,
+    : wxGLCanvas(parent, wxID_ANY, attribList,
                  wxDefaultPosition, wxDefaultSize,
                  wxDefaultPosition, wxDefaultSize,
-                 wxFULL_REPAINT_ON_RESIZE)
+                 wxFULL_REPAINT_ON_RESIZE),
+      m_xangle(30.0),
+      m_yangle(30.0),
+      m_spinTimer(this,SpinTimer),
+      m_useStereo(false),
+      m_stereoWarningAlreadyDisplayed(false)
 {
 {
-    m_xangle =
-    m_yangle = 30;
+    if ( attribList )
+    {
+        int i = 0;
+        while ( attribList[i] != 0 )
+        {
+            if ( attribList[i] == WX_GL_STEREO )
+                m_useStereo = true;
+            ++i;
+        }
+    }
 }
 
 void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
 }
 
 void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
@@ -311,51 +351,100 @@ void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
     // is wrong when next another canvas is repainted.
     const wxSize ClientSize = GetClientSize();
 
     // is wrong when next another canvas is repainted.
     const wxSize ClientSize = GetClientSize();
 
+    TestGLContext& canvas = wxGetApp().GetContext(this, m_useStereo);
     glViewport(0, 0, ClientSize.x, ClientSize.y);
 
     // Render the graphics and swap the buffers.
     glViewport(0, 0, ClientSize.x, ClientSize.y);
 
     // Render the graphics and swap the buffers.
-    wxGetApp().GetContext(this).DrawRotatedCube(m_xangle, m_yangle);
+    GLboolean quadStereoSupported;
+    glGetBooleanv( GL_STEREO, &quadStereoSupported);
+    if ( quadStereoSupported )
+    {
+        glDrawBuffer( GL_BACK_LEFT );
+        glMatrixMode(GL_PROJECTION);
+        glLoadIdentity();
+        glFrustum(-0.47f, 0.53f, -0.5f, 0.5f, 1.0f, 3.0f);
+        canvas.DrawRotatedCube(m_xangle, m_yangle);
+        CheckGLError();
+        glDrawBuffer( GL_BACK_RIGHT );
+        glMatrixMode(GL_PROJECTION);
+        glLoadIdentity();
+        glFrustum(-0.53f, 0.47f, -0.5f, 0.5f, 1.0f, 3.0f);
+        canvas.DrawRotatedCube(m_xangle, m_yangle);
+        CheckGLError();
+    }
+    else
+    {
+        canvas.DrawRotatedCube(m_xangle, m_yangle);
+        if ( m_useStereo && !m_stereoWarningAlreadyDisplayed )
+        {
+            m_stereoWarningAlreadyDisplayed = true;
+            wxLogError("Stereo not supported by the graphics card.");
+        }
+    }
     SwapBuffers();
 }
 
     SwapBuffers();
 }
 
-void TestGLCanvas::OnKeyDown( wxKeyEvent& event )
+void TestGLCanvas::Spin(float xSpin, float ySpin)
 {
 {
-    float *p = NULL;
+    m_xangle += xSpin;
+    m_yangle += ySpin;
 
 
-    bool inverse = false;
+    Refresh(false);
+}
+
+void TestGLCanvas::OnKeyDown(wxKeyEvent& event)
+{
+    float angle = 5.0;
 
     switch ( event.GetKeyCode() )
     {
         case WXK_RIGHT:
 
     switch ( event.GetKeyCode() )
     {
         case WXK_RIGHT:
-            inverse = true;
-            // fall through
+            Spin( 0.0, -angle );
+            break;
 
         case WXK_LEFT:
 
         case WXK_LEFT:
-            // rotate around Y axis
-            p = &m_yangle;
+            Spin( 0.0, angle );
             break;
 
         case WXK_DOWN:
             break;
 
         case WXK_DOWN:
-            inverse = true;
-            // fall through
+            Spin( -angle, 0.0 );
+            break;
 
         case WXK_UP:
 
         case WXK_UP:
-            // rotate around X axis
-            p = &m_xangle;
+            Spin( angle, 0.0 );
+            break;
+
+        case WXK_SPACE:
+            if ( m_spinTimer.IsRunning() )
+                m_spinTimer.Stop();
+            else
+                m_spinTimer.Start( 25 );
             break;
 
         default:
             event.Skip();
             return;
     }
             break;
 
         default:
             event.Skip();
             return;
     }
+}
 
 
-    float angle = 5;
-    if ( inverse )
-        angle = -angle;
+void TestGLCanvas::OnSpinTimer(wxTimerEvent& WXUNUSED(event))
+{
+    Spin(0.0, 4.0);
+}
 
 
-    *p += angle;
+wxString glGetwxString(GLenum name)
+{
+    const GLubyte *v = glGetString(name);
+    if ( v == 0 )
+    {
+        // The error is not important. It is GL_INVALID_ENUM.
+        // We just want to clear the error stack.
+        glGetError();
 
 
-    Refresh(false);
+        return wxString();
+    }
+
+    return wxString((const char*)v);
 }
 
 
 }
 
 
@@ -365,23 +454,27 @@ void TestGLCanvas::OnKeyDown( wxKeyEvent& event )
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(wxID_NEW, MyFrame::OnNewWindow)
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(wxID_NEW, MyFrame::OnNewWindow)
+    EVT_MENU(NEW_STEREO_WINDOW, MyFrame::OnNewStereoWindow)
     EVT_MENU(wxID_CLOSE, MyFrame::OnClose)
 END_EVENT_TABLE()
 
     EVT_MENU(wxID_CLOSE, MyFrame::OnClose)
 END_EVENT_TABLE()
 
-MyFrame::MyFrame()
-       : wxFrame(NULL, wxID_ANY, _T("wxWidgets OpenGL Cube Sample"))
+MyFrame::MyFrame( bool stereoWindow )
+       : wxFrame(NULL, wxID_ANY, wxT("wxWidgets OpenGL Cube Sample"))
 {
 {
-    new TestGLCanvas(this);
+    int stereoAttribList[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_STEREO, 0 };
+
+    new TestGLCanvas(this, stereoWindow ? stereoAttribList : NULL);
 
     SetIcon(wxICON(sample));
 
     // Make a menubar
     wxMenu *menu = new wxMenu;
     menu->Append(wxID_NEW);
 
     SetIcon(wxICON(sample));
 
     // Make a menubar
     wxMenu *menu = new wxMenu;
     menu->Append(wxID_NEW);
+    menu->Append(NEW_STEREO_WINDOW, "New Stereo Window");
     menu->AppendSeparator();
     menu->Append(wxID_CLOSE);
     wxMenuBar *menuBar = new wxMenuBar;
     menu->AppendSeparator();
     menu->Append(wxID_CLOSE);
     wxMenuBar *menuBar = new wxMenuBar;
-    menuBar->Append(menu, _T("&Cube"));
+    menuBar->Append(menu, wxT("&Cube"));
 
     SetMenuBar(menuBar);
 
 
     SetMenuBar(menuBar);
 
@@ -394,6 +487,15 @@ MyFrame::MyFrame()
     static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
     wxLogStatus("Double-buffered display %s supported",
                 wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not");
     static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
     wxLogStatus("Double-buffered display %s supported",
                 wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not");
+
+    if ( stereoWindow )
+    {
+        const wxString vendor = glGetwxString(GL_VENDOR).Lower();
+        const wxString renderer = glGetwxString(GL_RENDERER).Lower();
+        if ( vendor.find("nvidia") != wxString::npos &&
+                renderer.find("quadro") == wxString::npos )
+            ShowFullScreen(true);
+    }
 }
 
 void MyFrame::OnClose(wxCommandEvent& WXUNUSED(event))
 }
 
 void MyFrame::OnClose(wxCommandEvent& WXUNUSED(event))
@@ -407,3 +509,7 @@ void MyFrame::OnNewWindow( wxCommandEvent& WXUNUSED(event) )
     new MyFrame();
 }
 
     new MyFrame();
 }
 
+void MyFrame::OnNewStereoWindow( wxCommandEvent& WXUNUSED(event) )
+{
+    new MyFrame(true);
+}