1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxGLCanvas demo program 
   4 // Author:      Robert Roebling 
   8 // Copyright:   (c) Robert Roebling 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation 
  17 // For compilers that support precompilation, includes "wx.h". 
  18 #include "wx/wxprec.h" 
  31 #    include <OpenGL/glu.h> 
  39 #define VIEW_ASPECT 1.3 
  41 /* `Main program' equivalent, creating windows and returning main app frame */ 
  45   /* Create the main frame window */ 
  46   MyFrame 
*frame 
= new MyFrame(NULL
, wxT("wxWindows OpenGL Demo"), wxPoint(50, 50), wxSize(400, 300)); 
  49   wxMenu 
*fileMenu 
= new wxMenu
; 
  51   fileMenu
->Append(wxID_EXIT
, wxT("E&xit")); 
  52   wxMenuBar 
*menuBar 
= new wxMenuBar
; 
  53   menuBar
->Append(fileMenu
, wxT("&File")); 
  54   frame
->SetMenuBar(menuBar
); 
  57   frame
->SetCanvas( new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200), wxSUNKEN_BORDER
) ); 
  59   /* Load file wiht mesh data */ 
  60   frame
->GetCanvas()->LoadLWO( wxT("penguin.lwo") ); 
  68   wxMessageBox( _T("This sample has to be compiled with wxUSE_GLCANVAS"), _T("Building error"), wxOK
); 
  77 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
) 
  78     EVT_MENU(wxID_EXIT
, MyFrame::OnExit
) 
  81 /* My frame constructor */ 
  82 MyFrame::MyFrame(wxFrame 
*frame
, const wxString
& title
, const wxPoint
& pos
, 
  83     const wxSize
& size
, long style
): 
  84   wxFrame(frame
, -1, title
, pos
, size
, style
) 
  91 /* Intercept menu commands */ 
  92 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
)) 
  99 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
) 
 100     EVT_SIZE(TestGLCanvas::OnSize
) 
 101     EVT_PAINT(TestGLCanvas::OnPaint
) 
 102     EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
) 
 103     EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse
) 
 106 TestGLCanvas::TestGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 107     const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
): 
 108   wxGLCanvas(parent
, id
, pos
, size
, style
, name
) 
 113 TestGLCanvas::~TestGLCanvas(void) 
 116     lw_object_free(info
.lwobject
); 
 119 void TestGLCanvas::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
 121     /* must always be here */ 
 125     if (!GetContext()) return; 
 130     /* initialize OpenGL */ 
 131     if (info
.do_init 
== TRUE
)  
 134         info
.do_init 
= FALSE
; 
 138     glMatrixMode( GL_PROJECTION 
); 
 140     gluPerspective( info
.zoom
, VIEW_ASPECT
, 1, 100 ); 
 141     glMatrixMode( GL_MODELVIEW 
); 
 144     glClearColor( .3, .4, .6, 1 ); 
 145     glClear( GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT 
); 
 147     /* transformations */ 
 150     glTranslatef( 0, 0, -30 ); 
 151     build_rotmatrix( m
,info
.quat 
); 
 152     glMultMatrixf( &m
[0][0] ); 
 155     lw_object_show( info
.lwobject 
); 
 164 void TestGLCanvas::OnSize(wxSizeEvent
& event
) 
 166     // this is also necessary to update the context on some platforms 
 167     wxGLCanvas::OnSize(event
); 
 169     // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...) 
 171     GetClientSize(&w
, &h
); 
 177         glViewport(0, 0, (GLint
) w
, (GLint
) h
); 
 181 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
)) 
 183     /* Do nothing, to avoid flashing on MSW */ 
 186 void TestGLCanvas::LoadLWO(const wxString 
&filename
) 
 188     /* test if lightwave object */ 
 189     if (!lw_is_lwobject(filename
.mb_str())) return; 
 191     /* read lightwave object */ 
 192     lwObject 
*lwobject 
= lw_object_read(filename
.mb_str()); 
 195     lw_object_scale(lwobject
, 10.0 / lw_object_radius(lwobject
)); 
 197     /* set up mesh info */ 
 199     info
.lwobject 
= lwobject
; 
 203     trackball( info
.quat
, 0.0, 0.0, 0.0, 0.0 ); 
 206 void TestGLCanvas::OnMouse( wxMouseEvent
& event 
) 
 208     wxSize 
sz(GetClientSize()); 
 209     if (event
.Dragging()) 
 211         /* drag in progress, simulate trackball */ 
 214           (2.0*info
.beginx 
-       sz
.x
) / sz
.x
, 
 215           (     sz
.y 
- 2.0*info
.beginy
) / sz
.y
, 
 216           (     2.0*event
.GetX() - sz
.x
) / sz
.x
, 
 217           (    sz
.y 
- 2.0*event
.GetY()) / sz
.y
); 
 219         add_quats( spin_quat
, info
.quat
, info
.quat 
); 
 221         /* orientation has changed, redraw mesh */ 
 225     info
.beginx 
= event
.GetX(); 
 226     info
.beginy 
= event
.GetY(); 
 229 void TestGLCanvas::InitGL(void) 
 231     GLfloat light0_pos
[4]   = { -50.0, 50.0, 0.0, 0.0 }; 
 232     GLfloat light0_color
[4] = { .6, .6, .6, 1.0 }; /* white light */ 
 233     GLfloat light1_pos
[4]   = {  50.0, 50.0, 0.0, 0.0 }; 
 234     GLfloat light1_color
[4] = { .4, .4, 1, 1.0 };  /* cold blue light */ 
 236     /* remove back faces */ 
 237     glDisable(GL_CULL_FACE
); 
 238     glEnable(GL_DEPTH_TEST
); 
 242     glShadeModel(GL_SMOOTH
); 
 243     glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
); 
 244     glHint(GL_POLYGON_SMOOTH_HINT
, GL_FASTEST
); 
 247     glLightfv(GL_LIGHT0
, GL_POSITION
, light0_pos
); 
 248     glLightfv(GL_LIGHT0
, GL_DIFFUSE
,  light0_color
);   
 249     glLightfv(GL_LIGHT1
, GL_POSITION
, light1_pos
); 
 250     glLightfv(GL_LIGHT1
, GL_DIFFUSE
,  light1_color
); 
 253     glEnable(GL_LIGHTING
); 
 255     glColorMaterial(GL_FRONT_AND_BACK
,GL_AMBIENT_AND_DIFFUSE
); 
 256     glEnable(GL_COLOR_MATERIAL
);