wxWidgets/utils/glcanvas/samples/cube/cube.cpp
1998-08-15 17:48:39 +00:00

181 lines
4.3 KiB
C++

/////////////////////////////////////////////////////////////////////////////
// Name: cube.cpp
// Purpose: wxGLCanvas demo program
// Author: Julian Smart
// Modified by:
// Created: 04/01/98
// RCS-ID: $Id$
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
#pragma implementation
#pragma interface
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/log.h"
#include "cube.h"
IMPLEMENT_APP(MyApp)
// `Main program' equivalent, creating windows and returning main app frame
bool MyApp::OnInit(void)
{
wxLog::SetTraceMask(wxTraceMessages);
// Create the main frame window
MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
// Give it an icon
#ifdef wx_msw
frame->SetIcon(wxIcon("mondrian"));
#endif
// Make a menubar
wxMenu *fileMenu = new wxMenu;
fileMenu->Append(wxID_EXIT, "E&xit");
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(fileMenu, "&File");
frame->SetMenuBar(menuBar);
frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
InitGL();
// Show the frame
frame->Show(TRUE);
return TRUE;
}
void MyApp::InitGL(void)
{
/* set viewing projection */
glMatrixMode(GL_PROJECTION);
glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
/* position viewer */
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0F, 0.0F, -2.0F);
/* position object */
glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
END_EVENT_TABLE()
// My frame constructor
MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
const wxSize& size, long style):
wxFrame(frame, -1, title, pos, size, style)
{
m_canvas = NULL;
}
// Intercept menu commands
void MyFrame::OnExit(wxCommandEvent& event)
{
Destroy();
}
bool MyFrame::OnClose(void)
{
return TRUE;
}
BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
EVT_SIZE(TestGLCanvas::OnSize)
EVT_PAINT(TestGLCanvas::OnPaint)
EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
END_EVENT_TABLE()
TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size, long style, const wxString& name):
wxGLCanvas(parent, id, pos, size, style, name)
{
}
TestGLCanvas::~TestGLCanvas(void)
{
}
void TestGLCanvas::OnPaint( wxPaintEvent& event )
{
// This is a dummy, to avoid an endless succession of paint messages.
// OnPaint handlers must always create a wxPaintDC.
wxPaintDC dc(this);
if ( !GetContext() )
return;
SetCurrent();
/* clear color and depth buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* draw six faces of a cube */
glBegin(GL_QUADS);
glNormal3f( 0.0F, 0.0F, 1.0F);
glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
glNormal3f( 0.0F, 0.0F,-1.0F);
glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
glNormal3f( 0.0F, 1.0F, 0.0F);
glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
glNormal3f( 0.0F,-1.0F, 0.0F);
glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
glNormal3f( 1.0F, 0.0F, 0.0F);
glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
glNormal3f(-1.0F, 0.0F, 0.0F);
glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
glEnd();
SwapBuffers();
}
void TestGLCanvas::OnSize(wxSizeEvent& event)
{
int width, height;
GetClientSize(& width, & height);
if ( GetContext() )
glViewport(0, 0, width, height);
}
void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
{
// Do nothing, to avoid flashing.
}