]>
Commit | Line | Data |
---|---|---|
6a1120ad JS |
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 | ||
6a1120ad JS |
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 | ||
6a1120ad JS |
55 | // Show the frame |
56 | frame->Show(TRUE); | |
57 | ||
58 | return TRUE; | |
59 | } | |
60 | ||
aae24d21 RR |
61 | IMPLEMENT_APP(MyApp) |
62 | ||
6a1120ad JS |
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 | bool MyFrame::OnClose(void) | |
82 | { | |
83 | return TRUE; | |
84 | } | |
85 | ||
86 | BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) | |
87 | EVT_SIZE(TestGLCanvas::OnSize) | |
88 | EVT_PAINT(TestGLCanvas::OnPaint) | |
89 | EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground) | |
90 | END_EVENT_TABLE() | |
91 | ||
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) | |
95 | { | |
f6fcbb63 | 96 | m_init = FALSE; |
6a1120ad JS |
97 | } |
98 | ||
99 | TestGLCanvas::~TestGLCanvas(void) | |
100 | { | |
101 | } | |
102 | ||
103 | void TestGLCanvas::OnPaint( wxPaintEvent& event ) | |
104 | { | |
c2265822 JS |
105 | // This is a dummy, to avoid an endless succession of paint messages. |
106 | // OnPaint handlers must always create a wxPaintDC. | |
107 | wxPaintDC dc(this); | |
108 | ||
9838df2c | 109 | #ifndef __WXMOTIF__ |
f6fcbb63 | 110 | if (!GetContext()) return; |
9838df2c | 111 | #endif |
6a1120ad JS |
112 | |
113 | SetCurrent(); | |
114 | ||
f6fcbb63 RR |
115 | /* init OpenGL once, but after SetCurrent */ |
116 | if (!m_init) | |
117 | { | |
118 | InitGL(); | |
119 | m_init = TRUE; | |
120 | } | |
121 | ||
6a1120ad JS |
122 | /* clear color and depth buffers */ |
123 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
124 | ||
125 | /* draw six faces of a cube */ | |
126 | glBegin(GL_QUADS); | |
127 | glNormal3f( 0.0F, 0.0F, 1.0F); | |
128 | glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F); | |
129 | glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F); | |
130 | ||
131 | glNormal3f( 0.0F, 0.0F,-1.0F); | |
132 | glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F); | |
133 | glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F); | |
134 | ||
135 | glNormal3f( 0.0F, 1.0F, 0.0F); | |
136 | glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F); | |
137 | glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F); | |
138 | ||
139 | glNormal3f( 0.0F,-1.0F, 0.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( 1.0F, 0.0F, 0.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(-1.0F, 0.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 | glEnd(); | |
151 | ||
152 | SwapBuffers(); | |
153 | } | |
154 | ||
155 | void TestGLCanvas::OnSize(wxSizeEvent& event) | |
156 | { | |
157 | int width, height; | |
158 | GetClientSize(& width, & height); | |
159 | ||
9838df2c | 160 | #ifndef __WXMOTIF__ |
aae24d21 | 161 | if (GetContext()) |
9838df2c | 162 | #endif |
aae24d21 RR |
163 | { |
164 | SetCurrent(); | |
6a1120ad | 165 | glViewport(0, 0, width, height); |
aae24d21 | 166 | } |
6a1120ad JS |
167 | } |
168 | ||
169 | void TestGLCanvas::OnEraseBackground(wxEraseEvent& event) | |
170 | { | |
171 | // Do nothing, to avoid flashing. | |
172 | } | |
173 | ||
f6fcbb63 RR |
174 | void TestGLCanvas::InitGL(void) |
175 | { | |
176 | /* set viewing projection */ | |
177 | glMatrixMode(GL_PROJECTION); | |
178 | glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F); | |
179 | ||
180 | /* position viewer */ | |
181 | glMatrixMode(GL_MODELVIEW); | |
182 | glTranslatef(0.0F, 0.0F, -2.0F); | |
183 | ||
184 | /* position object */ | |
185 | glRotatef(30.0F, 1.0F, 0.0F, 0.0F); | |
186 | glRotatef(30.0F, 0.0F, 1.0F, 0.0F); | |
187 | ||
188 | glEnable(GL_DEPTH_TEST); | |
189 | glEnable(GL_LIGHTING); | |
190 | glEnable(GL_LIGHT0); | |
191 | } | |
192 |