]> git.saurik.com Git - wxWidgets.git/blame - utils/glcanvas/samples/cube/cube.cpp
fixed a logical error (! isempty) instead of isempty
[wxWidgets.git] / utils / glcanvas / samples / cube / cube.cpp
CommitLineData
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
32// This statement initializes the whole application and calls OnInit
33MyApp myApp;
34
35IMPLEMENT_APP(MyApp)
36
37// `Main program' equivalent, creating windows and returning main app frame
38bool MyApp::OnInit(void)
39{
40 wxLog::SetTraceMask(wxTraceMessages);
41
42 // Create the main frame window
43 MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
44
45 // Give it an icon
46#ifdef wx_msw
47 frame->SetIcon(wxIcon("mondrian"));
48#endif
49
50 // Make a menubar
51 wxMenu *fileMenu = new wxMenu;
52
53 fileMenu->Append(wxID_EXIT, "E&xit");
54 wxMenuBar *menuBar = new wxMenuBar;
55 menuBar->Append(fileMenu, "&File");
56 frame->SetMenuBar(menuBar);
57
58 frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
59
60 InitGL();
61
62 // Show the frame
63 frame->Show(TRUE);
64
65 return TRUE;
66}
67
68void MyApp::InitGL(void)
69{
70 /* set viewing projection */
71 glMatrixMode(GL_PROJECTION);
72 glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
73
74 /* position viewer */
75 glMatrixMode(GL_MODELVIEW);
76 glTranslatef(0.0F, 0.0F, -2.0F);
77
78 /* position object */
79 glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
80 glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
81
82 glEnable(GL_DEPTH_TEST);
83 glEnable(GL_LIGHTING);
84 glEnable(GL_LIGHT0);
85}
86
87BEGIN_EVENT_TABLE(MyFrame, wxFrame)
88 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
89END_EVENT_TABLE()
90
91// My frame constructor
92MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
93 const wxSize& size, long style):
94 wxFrame(frame, -1, title, pos, size, style)
95{
96 m_canvas = NULL;
97}
98
99// Intercept menu commands
100void MyFrame::OnExit(wxCommandEvent& event)
101{
102 Destroy();
103}
104
105bool MyFrame::OnClose(void)
106{
107 return TRUE;
108}
109
110BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
111 EVT_SIZE(TestGLCanvas::OnSize)
112 EVT_PAINT(TestGLCanvas::OnPaint)
113 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
114END_EVENT_TABLE()
115
116TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
117 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
118 wxGLCanvas(parent, id, pos, size, style, name)
119{
120}
121
122TestGLCanvas::~TestGLCanvas(void)
123{
124}
125
126void TestGLCanvas::OnPaint( wxPaintEvent& event )
127{
128 if ( !GetContext() )
129 return;
130
131 SetCurrent();
132
133 /* clear color and depth buffers */
134 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
135
136 /* draw six faces of a cube */
137 glBegin(GL_QUADS);
138 glNormal3f( 0.0F, 0.0F, 1.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( 0.0F, 0.0F,-1.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
146 glNormal3f( 0.0F, 1.0F, 0.0F);
147 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
148 glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
149
150 glNormal3f( 0.0F,-1.0F, 0.0F);
151 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
152 glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
153
154 glNormal3f( 1.0F, 0.0F, 0.0F);
155 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
156 glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
157
158 glNormal3f(-1.0F, 0.0F, 0.0F);
159 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
160 glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
161 glEnd();
162
163 SwapBuffers();
164}
165
166void TestGLCanvas::OnSize(wxSizeEvent& event)
167{
168 int width, height;
169 GetClientSize(& width, & height);
170
171 if ( GetContext() )
172 glViewport(0, 0, width, height);
173}
174
175void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
176{
177 // Do nothing, to avoid flashing.
178}
179