GLCube.d

   1 //-----------------------------------------------------------------------------
   2 // wxD/Samples - GLCube.d
   3 //
   4 // A wxD version of the wxWidgets "opengl/cube" sample.
   5 //
   6 // wxGLCanvas demo program
   7 // (c) 1998 Julian Smart
   8 //
   9 // (C) 2006 afb <afb.sourceforge.net>
  10 // Licensed under the wxWidgets license, see LICENSE.txt for details.
  11 //
  12 // $Id: GLCube.d,v 1.6 2008/03/03 11:47:07 afb Exp $
  13 //-----------------------------------------------------------------------------
  14 
  15 import wx.wx;
  16 
  17 import wx.GLCanvas;
  18 
  19 import gl.gl;
  20 
  21     public class TestGLCanvas : GLCanvas
  22     {
  23         public this(Window parent, TestGLCanvas other, int id, Point pos=wxDefaultPosition, Size size=wxDefaultSize, int style=0, string name="TestGLCanvas")
  24         {
  25             super(parent, other ? other.context : null, id, pos, size, style, name);
  26 
  27             EVT_SIZE(&OnSize);
  28             EVT_PAINT(&OnPaint);
  29             EVT_ERASE_BACKGROUND(&OnEraseBackground);
  30             EVT_ENTER_WINDOW(&OnEnterWindow);
  31         }
  32 
  33         public this(Window parent, int id, Point pos=wxDefaultPosition, Size size=wxDefaultSize, int style=0, string name="TestGLCanvas")
  34         {
  35             this(parent, null, id, pos, size, style, name);
  36         }
  37 
  38         private uint gllist = 0;
  39 
  40         private void Render()
  41         {
  42             if (!context()) return;
  43 
  44             // This is a dummy, to avoid an endless succession of paint messages.
  45             // OnPaint handlers must always create a wxPaintDC.
  46             auto wxPaintDC dc = new wxPaintDC(this);
  47 
  48             SetCurrent();
  49 
  50             // Init OpenGL once, but after SetCurrent
  51             if (!init)
  52             {
  53                 InitGL();
  54                 init = true;
  55             }
  56 
  57             glMatrixMode(GL_PROJECTION);
  58             glLoadIdentity();
  59             glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 3.0f);
  60             glMatrixMode(GL_MODELVIEW);
  61         
  62             /* clear color and depth buffers */
  63             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  64         
  65             if( gllist == 0 )
  66             {
  67                 gllist = glGenLists( 1 );
  68                 glNewList(gllist, GL_COMPILE_AND_EXECUTE );
  69                 /* draw six faces of a cube */
  70                 glBegin(GL_QUADS);
  71                 glNormal3f( 0.0f, 0.0f, 1.0f);
  72                 glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f(-0.5f, 0.5f, 0.5f);
  73                 glVertex3f(-0.5f,-0.5f, 0.5f); glVertex3f( 0.5f,-0.5f, 0.5f);
  74         
  75                 glNormal3f( 0.0f, 0.0f,-1.0f);
  76                 glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f(-0.5f, 0.5f,-0.5f);
  77                 glVertex3f( 0.5f, 0.5f,-0.5f); glVertex3f( 0.5f,-0.5f,-0.5f);
  78         
  79                 glNormal3f( 0.0f, 1.0f, 0.0f);
  80                 glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f( 0.5f, 0.5f,-0.5f);
  81                 glVertex3f(-0.5f, 0.5f,-0.5f); glVertex3f(-0.5f, 0.5f, 0.5f);
  82         
  83                 glNormal3f( 0.0f,-1.0f, 0.0f);
  84                 glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f( 0.5f,-0.5f,-0.5f);
  85                 glVertex3f( 0.5f,-0.5f, 0.5f); glVertex3f(-0.5f,-0.5f, 0.5f);
  86         
  87                 glNormal3f( 1.0f, 0.0f, 0.0f);
  88                 glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f( 0.5f,-0.5f, 0.5f);
  89                 glVertex3f( 0.5f,-0.5f,-0.5f); glVertex3f( 0.5f, 0.5f,-0.5f);
  90         
  91                 glNormal3f(-1.0f, 0.0f, 0.0f);
  92                 glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f(-0.5f,-0.5f, 0.5f);
  93                 glVertex3f(-0.5f, 0.5f, 0.5f); glVertex3f(-0.5f, 0.5f,-0.5f);
  94                 glEnd();
  95         
  96                 glEndList();
  97             }
  98             else
  99             {
 100                 glCallList(gllist);
 101             }
 102         
 103             glFlush();
 104             SwapBuffers();
 105         }
 106         
 107         private bool init = false;
 108 
 109         private void InitGL()
 110         {
 111             SetCurrent();
 112         
 113             /* set viewing projection */
 114             glMatrixMode(GL_PROJECTION);
 115             glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 3.0f);
 116         
 117             /* position viewer */
 118             glMatrixMode(GL_MODELVIEW);
 119             glTranslatef(0.0f, 0.0f, -2.0f);
 120         
 121             /* position object */
 122             glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
 123             glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
 124         
 125             glEnable(GL_DEPTH_TEST);
 126             glEnable(GL_LIGHTING);
 127             glEnable(GL_LIGHT0);
 128         }
 129 
 130         void OnEnterWindow(Object sender, Event e)
 131         {
 132             SetFocus();
 133         }
 134 
 135         void OnPaint(Object sender, Event e)
 136         {
 137             e.Skip();
 138             Render();
 139         }
 140 
 141         void OnSize(Object sender, Event e)
 142         {
 143             // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
 144             Size size = ClientSize();
 145         
 146             if (context())
 147             {
 148                 SetCurrent();
 149                 glViewport(0, 0, cast(GLint) size.Width, cast(GLint) size.Height);
 150             }
 151         }
 152 
 153         void OnEraseBackground(Object sender, Event e)
 154         {
 155           // Do nothing, to avoid flashing.
 156         }
 157     }
 158 
 159     public class MyFrame : Frame
 160     {
 161         enum Cmd { Quit = 1 }
 162 
 163         //---------------------------------------------------------------------
 164 
 165         public this(Window parent, string title, Point pos, Size size, int style=wxDEFAULT_FRAME_STYLE)
 166         {
 167             super(parent, wxID_ANY, title, pos, size, style);
 168             canvas = null;
 169 
 170             // Set the window icon
 171 
 172             icon = new Icon("../Samples/GLCube/mondrian.png");
 173 
 174             // Set up the event table
 175 
 176             EVT_MENU( MenuIDs.wxID_EXIT, &OnExit);
 177             EVT_MENU( Cmd.Quit, &OnExit);
 178             EVT_MENU( MenuIDs.wxID_NEW, &OnNewWindow);
 179         }
 180         
 181         public static MyFrame CreateWindow(MyFrame parentFrame = null, bool isCloneWindow = false)
 182         {
 183             string str = "wxWidgets OpenGL Cube Sample";
 184             if (isCloneWindow) str ~= " - Clone";
 185  
 186             MyFrame frame = new MyFrame(null, str, wxDefaultPosition,
 187                 /* Size(400, 300) */ wxDefaultSize);
 188 
 189             // Make a menubar
 190             Menu winMenu = new Menu();
 191             winMenu.Append(MenuIDs.wxID_EXIT, "&Close");
 192             winMenu.Append(MenuIDs.wxID_NEW, "&New");
 193 
 194             MenuBar menuBar = new MenuBar();
 195             menuBar.Append(winMenu, "&Window");
 196 
 197             frame.menuBar = menuBar;
 198 
 199             if (parentFrame)
 200             {
 201                 frame.canvas = new TestGLCanvas(frame, parentFrame.canvas,
 202                     wxID_ANY, wxDefaultPosition, wxDefaultSize );
 203             }
 204             else
 205             {
 206                 frame.canvas = new TestGLCanvas(frame, wxID_ANY,
 207                     wxDefaultPosition, wxDefaultSize);
 208             }
 209    
 210             // moved the Frame sizing to after canvas was added,
 211             // since otherwise the frame showed empty on wxMac ?
 212             frame.size = Size(400, 300);
 213 
 214             // Show the frame
 215             frame.Show(true);
 216 
 217             return frame;
 218         }
 219 
 220         //---------------------------------------------------------------------
 221 
 222         public void OnExit(Object sender, Event e)
 223         {
 224             // true is to force the frame to close
 225             Close();
 226         }
 227 
 228         //---------------------------------------------------------------------
 229 
 230         public void OnNewWindow(Object sender, Event e)
 231         {
 232             CreateWindow(this, true);
 233         }
 234 
 235         //---------------------------------------------------------------------
 236     
 237     
 238         TestGLCanvas canvas;
 239     }
 240 
 241     public class GLCube : App
 242     {
 243         //---------------------------------------------------------------------
 244 
 245         public override bool OnInit()
 246         {
 247              // Create the main frame window
 248             MyFrame.CreateWindow(null);
 249 
 250             return true;
 251         }
 252 
 253         //---------------------------------------------------------------------
 254 
 255         static void Main()
 256         {
 257             GLCube app = new GLCube();
 258             app.Run();
 259         }
 260 
 261         //---------------------------------------------------------------------
 262     }
 263 
 264 int main()
 265 {
 266     GLCube.Main();
 267     return 0;
 268 }

GLCube Screenshot


Source file: GLCube.d (268 lines)