]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/cube/cube.cpp
7a9735c98bb1c34adc9a714220781665e2ec0796
[wxWidgets.git] / utils / glcanvas / samples / cube / cube.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cube.cpp
3 // Purpose: wxGLCanvas demo program
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/log.h"
29
30 #include "cube.h"
31
32 // `Main program' equivalent, creating windows and returning main app frame
33 bool MyApp::OnInit(void)
34 {
35 wxLog::SetTraceMask(wxTraceMessages);
36
37 // Create the main frame window
38 MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
39
40 // Give it an icon
41 #ifdef wx_msw
42 frame->SetIcon(wxIcon("mondrian"));
43 #endif
44
45 // Make a menubar
46 wxMenu *fileMenu = new wxMenu;
47
48 fileMenu->Append(wxID_EXIT, "E&xit");
49 wxMenuBar *menuBar = new wxMenuBar;
50 menuBar->Append(fileMenu, "&File");
51 frame->SetMenuBar(menuBar);
52
53 frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
54
55 // Show the frame
56 frame->Show(TRUE);
57
58 return TRUE;
59 }
60
61 IMPLEMENT_APP(MyApp)
62
63 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
64 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
65 END_EVENT_TABLE()
66
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)
71 {
72 m_canvas = NULL;
73 }
74
75 // Intercept menu commands
76 void MyFrame::OnExit(wxCommandEvent& event)
77 {
78 Destroy();
79 }
80
81 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
82 EVT_SIZE(TestGLCanvas::OnSize)
83 EVT_PAINT(TestGLCanvas::OnPaint)
84 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
85 END_EVENT_TABLE()
86
87 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
88 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
89 wxGLCanvas(parent, id, pos, size, style, name)
90 {
91 m_init = FALSE;
92 }
93
94 TestGLCanvas::~TestGLCanvas(void)
95 {
96 }
97
98 void TestGLCanvas::OnPaint( wxPaintEvent& event )
99 {
100 // This is a dummy, to avoid an endless succession of paint messages.
101 // OnPaint handlers must always create a wxPaintDC.
102 wxPaintDC dc(this);
103
104 #ifndef __WXMOTIF__
105 if (!GetContext()) return;
106 #endif
107
108 SetCurrent();
109
110 /* init OpenGL once, but after SetCurrent */
111 if (!m_init)
112 {
113 InitGL();
114 m_init = TRUE;
115 }
116
117 /* clear color and depth buffers */
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
119
120 /* draw six faces of a cube */
121 glBegin(GL_QUADS);
122 glNormal3f( 0.0F, 0.0F, 1.0F);
123 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
124 glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
125
126 glNormal3f( 0.0F, 0.0F,-1.0F);
127 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
128 glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
129
130 glNormal3f( 0.0F, 1.0F, 0.0F);
131 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
132 glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
133
134 glNormal3f( 0.0F,-1.0F, 0.0F);
135 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
136 glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
137
138 glNormal3f( 1.0F, 0.0F, 0.0F);
139 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
140 glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
141
142 glNormal3f(-1.0F, 0.0F, 0.0F);
143 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
144 glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
145 glEnd();
146
147 SwapBuffers();
148 }
149
150 void TestGLCanvas::OnSize(wxSizeEvent& event)
151 {
152 int width, height;
153 GetClientSize(& width, & height);
154
155 #ifndef __WXMOTIF__
156 if (GetContext())
157 #endif
158 {
159 SetCurrent();
160 glViewport(0, 0, width, height);
161 }
162 }
163
164 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
165 {
166 // Do nothing, to avoid flashing.
167 }
168
169 void TestGLCanvas::InitGL(void)
170 {
171 /* set viewing projection */
172 glMatrixMode(GL_PROJECTION);
173 glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
174
175 /* position viewer */
176 glMatrixMode(GL_MODELVIEW);
177 glTranslatef(0.0F, 0.0F, -2.0F);
178
179 /* position object */
180 glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
181 glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
182
183 glEnable(GL_DEPTH_TEST);
184 glEnable(GL_LIGHTING);
185 glEnable(GL_LIGHT0);
186 }
187