]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/cube/cube.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
32 // `Main program' equivalent, creating windows and returning main app frame
33 bool MyApp::OnInit(void)
35 wxLog::SetTraceMask(wxTraceMessages
);
37 // Create the main frame window
38 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
42 frame
->SetIcon(wxIcon("mondrian"));
46 wxMenu
*fileMenu
= new wxMenu
;
48 fileMenu
->Append(wxID_EXIT
, "E&xit");
49 wxMenuBar
*menuBar
= new wxMenuBar
;
50 menuBar
->Append(fileMenu
, "&File");
51 frame
->SetMenuBar(menuBar
);
53 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));
63 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
64 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
67 // My frame constructor
68 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
69 const wxSize
& size
, long style
):
70 wxFrame(frame
, -1, title
, pos
, size
, style
)
75 // Intercept menu commands
76 void MyFrame::OnExit(wxCommandEvent
& event
)
81 bool MyFrame::OnClose(void)
86 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
87 EVT_SIZE(TestGLCanvas::OnSize
)
88 EVT_PAINT(TestGLCanvas::OnPaint
)
89 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
92 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
93 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
94 wxGLCanvas(parent
, id
, pos
, size
, style
, name
)
99 TestGLCanvas::~TestGLCanvas(void)
103 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
105 // This is a dummy, to avoid an endless succession of paint messages.
106 // OnPaint handlers must always create a wxPaintDC.
109 if (!GetContext()) return;
113 /* init OpenGL once, but after SetCurrent */
120 /* clear color and depth buffers */
121 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
123 /* draw six faces of a cube */
125 glNormal3f( 0.0F
, 0.0F
, 1.0F
);
126 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
);
127 glVertex3f(-0.5F
,-0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
);
129 glNormal3f( 0.0F
, 0.0F
,-1.0F
);
130 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
);
131 glVertex3f( 0.5F
, 0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
);
133 glNormal3f( 0.0F
, 1.0F
, 0.0F
);
134 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
);
135 glVertex3f(-0.5F
, 0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
);
137 glNormal3f( 0.0F
,-1.0F
, 0.0F
);
138 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
);
139 glVertex3f( 0.5F
,-0.5F
, 0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
);
141 glNormal3f( 1.0F
, 0.0F
, 0.0F
);
142 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
);
143 glVertex3f( 0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
);
145 glNormal3f(-1.0F
, 0.0F
, 0.0F
);
146 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
);
147 glVertex3f(-0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
);
153 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
156 GetClientSize(& width
, & height
);
161 glViewport(0, 0, width
, height
);
165 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
167 // Do nothing, to avoid flashing.
170 void TestGLCanvas::InitGL(void)
172 /* set viewing projection */
173 glMatrixMode(GL_PROJECTION
);
174 glFrustum(-0.5F
, 0.5F
, -0.5F
, 0.5F
, 1.0F
, 3.0F
);
176 /* position viewer */
177 glMatrixMode(GL_MODELVIEW
);
178 glTranslatef(0.0F
, 0.0F
, -2.0F
);
180 /* position object */
181 glRotatef(30.0F
, 1.0F
, 0.0F
, 0.0F
);
182 glRotatef(30.0F
, 0.0F
, 1.0F
, 0.0F
);
184 glEnable(GL_DEPTH_TEST
);
185 glEnable(GL_LIGHTING
);