]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/cube/cube.cpp
no message
[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 // InitGL();
56
57 // Show the frame
58 frame->Show(TRUE);
59
60 return TRUE;
61 }
62
63 void MyApp::InitGL(void)
64 {
65 /* set viewing projection */
66 glMatrixMode(GL_PROJECTION);
67 glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
68
69 /* position viewer */
70 glMatrixMode(GL_MODELVIEW);
71 glTranslatef(0.0F, 0.0F, -2.0F);
72
73 /* position object */
74 glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
75 glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
76
77 glEnable(GL_DEPTH_TEST);
78 glEnable(GL_LIGHTING);
79 glEnable(GL_LIGHT0);
80 }
81
82 IMPLEMENT_APP(MyApp)
83
84 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
85 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
86 END_EVENT_TABLE()
87
88 // My frame constructor
89 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
90 const wxSize& size, long style):
91 wxFrame(frame, -1, title, pos, size, style)
92 {
93 m_canvas = NULL;
94 }
95
96 // Intercept menu commands
97 void MyFrame::OnExit(wxCommandEvent& event)
98 {
99 Destroy();
100 }
101
102 bool MyFrame::OnClose(void)
103 {
104 return TRUE;
105 }
106
107 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
108 EVT_SIZE(TestGLCanvas::OnSize)
109 EVT_PAINT(TestGLCanvas::OnPaint)
110 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
111 END_EVENT_TABLE()
112
113 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
114 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
115 wxGLCanvas(parent, id, pos, size, style, name)
116 {
117 }
118
119 TestGLCanvas::~TestGLCanvas(void)
120 {
121 }
122
123 void TestGLCanvas::OnPaint( wxPaintEvent& event )
124 {
125 // This is a dummy, to avoid an endless succession of paint messages.
126 // OnPaint handlers must always create a wxPaintDC.
127 wxPaintDC dc(this);
128
129 if ( !GetContext() )
130 return;
131
132 SetCurrent();
133
134 /* clear color and depth buffers */
135 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136
137 /* draw six faces of a cube */
138 glBegin(GL_QUADS);
139 glNormal3f( 0.0F, 0.0F, 1.0F);
140 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
141 glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
142
143 glNormal3f( 0.0F, 0.0F,-1.0F);
144 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
145 glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
146
147 glNormal3f( 0.0F, 1.0F, 0.0F);
148 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
149 glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
150
151 glNormal3f( 0.0F,-1.0F, 0.0F);
152 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
153 glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
154
155 glNormal3f( 1.0F, 0.0F, 0.0F);
156 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
157 glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
158
159 glNormal3f(-1.0F, 0.0F, 0.0F);
160 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
161 glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
162 glEnd();
163
164 SwapBuffers();
165 }
166
167 void TestGLCanvas::OnSize(wxSizeEvent& event)
168 {
169 int width, height;
170 GetClientSize(& width, & height);
171
172 if (GetContext())
173 {
174 SetCurrent();
175 glViewport(0, 0, width, height);
176 }
177 }
178
179 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
180 {
181 // Do nothing, to avoid flashing.
182 }
183