2000-02-28 03:22:57 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: penguin.cpp
|
|
|
|
// Purpose: wxGLCanvas demo program
|
|
|
|
// Author: Robert Roebling
|
2005-11-14 12:38:24 -05:00
|
|
|
// Modified by: Sandro Sigala
|
2000-02-28 03:22:57 -05:00
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Robert Roebling
|
2002-03-17 09:16:03 -05:00
|
|
|
// Licence: wxWindows licence
|
2000-02-28 03:22:57 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2004-10-13 17:19:38 -04:00
|
|
|
#if !wxUSE_GLCANVAS
|
|
|
|
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
|
|
|
|
#endif
|
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
#include "penguin.h"
|
2006-11-10 10:16:02 -05:00
|
|
|
#ifdef __DARWIN__
|
|
|
|
#include <OpenGL/glu.h>
|
2001-05-16 12:23:13 -04:00
|
|
|
#else
|
2006-11-10 10:16:02 -05:00
|
|
|
#include <GL/glu.h>
|
2001-05-16 12:23:13 -04:00
|
|
|
#endif
|
2000-02-28 03:22:57 -05:00
|
|
|
|
2005-02-26 15:03:27 -05:00
|
|
|
#include "../../sample.xpm"
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ---------------------------------------------------------------------------
|
2000-02-28 03:22:57 -05:00
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
2003-05-06 03:22:13 -04:00
|
|
|
bool MyApp::OnInit()
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
// Create the main frame window
|
2005-11-14 12:38:24 -05:00
|
|
|
MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"),
|
2003-11-14 23:21:10 -05:00
|
|
|
wxDefaultPosition, wxDefaultSize);
|
2000-02-28 03:22:57 -05:00
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
#if wxUSE_ZLIB
|
|
|
|
if (wxFileExists(wxT("penguin.dxf.gz")))
|
|
|
|
frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
|
|
|
|
#else
|
|
|
|
if (wxFileExists(wxT("penguin.dxf")))
|
|
|
|
frame->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
|
|
|
|
#endif
|
2000-02-28 03:22:57 -05:00
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
/* Show the frame */
|
|
|
|
frame->Show(true);
|
|
|
|
|
|
|
|
return true;
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// MyFrame
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2005-11-14 12:38:24 -05:00
|
|
|
EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen)
|
|
|
|
EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit)
|
|
|
|
EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout)
|
2000-02-28 03:22:57 -05:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// MyFrame constructor
|
2000-02-28 03:22:57 -05:00
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
|
2003-11-14 23:21:10 -05:00
|
|
|
const wxSize& size, long style)
|
|
|
|
: wxFrame(frame, wxID_ANY, title, pos, size, style)
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-02-26 15:03:27 -05:00
|
|
|
SetIcon(wxIcon(sample_xpm));
|
2005-11-14 12:38:24 -05:00
|
|
|
|
|
|
|
// Make the "File" menu
|
|
|
|
wxMenu *fileMenu = new wxMenu;
|
|
|
|
fileMenu->Append(wxID_OPEN, wxT("&Open..."));
|
|
|
|
fileMenu->AppendSeparator();
|
|
|
|
fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X"));
|
|
|
|
// Make the "Help" menu
|
|
|
|
wxMenu *helpMenu = new wxMenu;
|
|
|
|
helpMenu->Append(wxID_HELP, wxT("&About..."));
|
|
|
|
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
menuBar->Append(fileMenu, wxT("&File"));
|
|
|
|
menuBar->Append(helpMenu, wxT("&Help"));
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
|
2008-02-10 08:26:01 -05:00
|
|
|
Show(true);
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
|
2008-02-10 08:26:01 -05:00
|
|
|
GetClientSize(), wxSUNKEN_BORDER);
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// File|Open... command
|
|
|
|
void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
|
|
|
|
#if wxUSE_ZLIB
|
|
|
|
wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
|
|
|
|
#else
|
|
|
|
wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
|
|
|
|
#endif
|
2006-05-28 19:32:12 -04:00
|
|
|
wxFD_OPEN);
|
2005-11-14 12:38:24 -05:00
|
|
|
if (!filename.IsEmpty())
|
|
|
|
{
|
|
|
|
m_canvas->LoadDXF(filename);
|
|
|
|
m_canvas->Refresh(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// File|Exit command
|
|
|
|
void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) )
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2003-11-14 23:21:10 -05:00
|
|
|
// true is to force the frame to close
|
|
|
|
Close(true);
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// Help|About... command
|
|
|
|
void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// TestGLCanvas
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
|
|
|
|
EVT_SIZE(TestGLCanvas::OnSize)
|
|
|
|
EVT_PAINT(TestGLCanvas::OnPaint)
|
|
|
|
EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
|
|
|
|
EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2008-02-10 08:26:01 -05:00
|
|
|
TestGLCanvas::TestGLCanvas(wxWindow *parent,
|
|
|
|
wxWindowID id,
|
|
|
|
const wxPoint& pos,
|
|
|
|
const wxSize& size,
|
|
|
|
long style,
|
|
|
|
const wxString& name)
|
|
|
|
: wxGLCanvas(parent, id, NULL, pos, size,
|
|
|
|
style | wxFULL_REPAINT_ON_RESIZE, name)
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2008-02-10 08:26:01 -05:00
|
|
|
// Explicitly create a new rendering context instance for this canvas.
|
|
|
|
m_glRC = new wxGLContext(this);
|
|
|
|
|
|
|
|
// Make the new context current (activate it for use) with this canvas.
|
|
|
|
SetCurrent(*m_glRC);
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
m_gldata.initialized = false;
|
|
|
|
|
|
|
|
// initialize view matrix
|
|
|
|
m_gldata.beginx = 0.0f;
|
|
|
|
m_gldata.beginy = 0.0f;
|
|
|
|
m_gldata.zoom = 45.0f;
|
|
|
|
trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f);
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
TestGLCanvas::~TestGLCanvas()
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2008-02-10 08:26:01 -05:00
|
|
|
delete m_glRC;
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2003-09-11 03:20:55 -04:00
|
|
|
void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-11-14 12:38:24 -05:00
|
|
|
// must always be here
|
2000-02-28 03:22:57 -05:00
|
|
|
wxPaintDC dc(this);
|
|
|
|
|
2008-02-10 08:26:01 -05:00
|
|
|
SetCurrent(*m_glRC);
|
2003-11-14 23:21:10 -05:00
|
|
|
|
|
|
|
// Initialize OpenGL
|
2005-11-14 12:38:24 -05:00
|
|
|
if (!m_gldata.initialized)
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
|
|
|
InitGL();
|
2005-11-14 12:38:24 -05:00
|
|
|
ResetProjectionMode();
|
|
|
|
m_gldata.initialized = true;
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
2003-11-14 23:21:10 -05:00
|
|
|
|
|
|
|
// Clear
|
|
|
|
glClearColor( 0.3f, 0.4f, 0.6f, 1.0f );
|
2000-02-28 03:22:57 -05:00
|
|
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
// Transformations
|
2000-02-28 03:22:57 -05:00
|
|
|
glLoadIdentity();
|
2005-11-14 12:38:24 -05:00
|
|
|
glTranslatef( 0.0f, 0.0f, -20.0f );
|
|
|
|
GLfloat m[4][4];
|
|
|
|
build_rotmatrix( m, m_gldata.quat );
|
2000-02-28 03:22:57 -05:00
|
|
|
glMultMatrixf( &m[0][0] );
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
m_renderer.Render();
|
2003-11-14 23:21:10 -05:00
|
|
|
|
|
|
|
// Flush
|
2000-02-28 03:22:57 -05:00
|
|
|
glFlush();
|
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
// Swap
|
2000-02-28 03:22:57 -05:00
|
|
|
SwapBuffers();
|
|
|
|
}
|
|
|
|
|
2008-02-10 08:26:01 -05:00
|
|
|
void TestGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2008-02-10 08:26:01 -05:00
|
|
|
// Reset the OpenGL view aspect.
|
|
|
|
// This is OK only because there is only one canvas that uses the context.
|
|
|
|
// See the cube sample for that case that multiple canvases are made current with one context.
|
2005-11-14 12:38:24 -05:00
|
|
|
ResetProjectionMode();
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2003-09-11 03:20:55 -04:00
|
|
|
void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-11-14 12:38:24 -05:00
|
|
|
// Do nothing, to avoid flashing on MSW
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// Load the DXF file. If the zlib support is compiled in wxWidgets,
|
|
|
|
// supports also the ".dxf.gz" gzip compressed files.
|
|
|
|
void TestGLCanvas::LoadDXF(const wxString& filename)
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-11-14 12:38:24 -05:00
|
|
|
wxFileInputStream stream(filename);
|
|
|
|
if (stream.Ok())
|
|
|
|
#if wxUSE_ZLIB
|
|
|
|
{
|
|
|
|
if (filename.Right(3).Lower() == wxT(".gz"))
|
|
|
|
{
|
|
|
|
wxZlibInputStream zstream(stream);
|
|
|
|
m_renderer.Load(zstream);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
m_renderer.Load(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
m_renderer.Load(stream);
|
|
|
|
}
|
|
|
|
#endif
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
void TestGLCanvas::OnMouse(wxMouseEvent& event)
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-11-14 12:38:24 -05:00
|
|
|
if (event.Dragging())
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2005-11-14 12:38:24 -05:00
|
|
|
wxSize sz(GetClientSize());
|
2003-11-14 23:21:10 -05:00
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
/* drag in progress, simulate trackball */
|
|
|
|
float spin_quat[4];
|
|
|
|
trackball(spin_quat,
|
2005-11-14 12:38:24 -05:00
|
|
|
(2.0*m_gldata.beginx - sz.x) / sz.x,
|
|
|
|
(sz.y - 2.0*m_gldata.beginy) / sz.y,
|
|
|
|
(2.0*event.GetX() - sz.x) / sz.x,
|
|
|
|
(sz.y - 2.0*event.GetY()) / sz.y);
|
2003-11-14 23:21:10 -05:00
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
add_quats(spin_quat, m_gldata.quat, m_gldata.quat);
|
2002-03-17 09:16:03 -05:00
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
/* orientation has changed, redraw mesh */
|
2003-11-14 23:21:10 -05:00
|
|
|
Refresh(false);
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
2002-03-17 09:16:03 -05:00
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
m_gldata.beginx = event.GetX();
|
|
|
|
m_gldata.beginy = event.GetY();
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2003-11-14 23:21:10 -05:00
|
|
|
void TestGLCanvas::InitGL()
|
2000-02-28 03:22:57 -05:00
|
|
|
{
|
2003-11-14 23:21:10 -05:00
|
|
|
static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f };
|
|
|
|
|
|
|
|
// white light
|
|
|
|
static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
|
|
|
|
|
|
|
|
static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f };
|
|
|
|
|
|
|
|
// cold blue light
|
|
|
|
static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f };
|
2000-02-28 03:22:57 -05:00
|
|
|
|
|
|
|
/* remove back faces */
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2003-11-14 23:21:10 -05:00
|
|
|
|
2000-02-28 03:22:57 -05:00
|
|
|
/* speedups */
|
|
|
|
glEnable(GL_DITHER);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
|
|
|
|
glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
|
|
|
|
|
|
|
|
/* light */
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
|
2003-11-14 23:21:10 -05:00
|
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
|
2000-02-28 03:22:57 -05:00
|
|
|
glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
|
|
|
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
|
|
|
|
glEnable(GL_LIGHT0);
|
|
|
|
glEnable(GL_LIGHT1);
|
|
|
|
glEnable(GL_LIGHTING);
|
2003-11-14 23:21:10 -05:00
|
|
|
|
|
|
|
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
|
|
|
glEnable(GL_COLOR_MATERIAL);
|
2000-02-28 03:22:57 -05:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
void TestGLCanvas::ResetProjectionMode()
|
|
|
|
{
|
2008-02-10 08:26:01 -05:00
|
|
|
// This is normally only necessary if there is more than one wxGLCanvas
|
|
|
|
// or more than one wxGLContext in the application.
|
|
|
|
SetCurrent(*m_glRC);
|
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
int w, h;
|
|
|
|
GetClientSize(&w, &h);
|
2008-02-10 08:26:01 -05:00
|
|
|
|
|
|
|
// It's up to the application code to update the OpenGL viewport settings.
|
|
|
|
// In order to avoid extensive context switching, consider doing this in
|
|
|
|
// OnPaint() rather than here, though.
|
|
|
|
glViewport(0, 0, (GLint) w, (GLint) h);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2005-11-14 12:38:24 -05:00
|
|
|
}
|