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
|
|
|
|
// 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
|
2018-09-22 19:15:08 -04:00
|
|
|
MyFrame *frame = new MyFrame(NULL, "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
|
2018-09-22 19:15:08 -04:00
|
|
|
if (wxFileExists("penguin.dxf.gz"))
|
|
|
|
frame->GetCanvas()->LoadDXF("penguin.dxf.gz");
|
2005-11-14 12:38:24 -05:00
|
|
|
#else
|
2018-09-22 19:15:08 -04:00
|
|
|
if (wxFileExists("penguin.dxf"))
|
|
|
|
frame->GetCanvas()->LoadDXF("penguin.dxf");
|
2005-11-14 12:38:24 -05:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
2000-02-28 03:22:57 -05:00
|
|
|
|
2005-11-14 12:38:24 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// MyFrame
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_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)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2000-02-28 03:22:57 -05:00
|
|
|
|
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
|
|
|
{
|
2010-06-20 13:42:33 -04:00
|
|
|
SetIcon(wxICON(sample));
|
2005-11-14 12:38:24 -05:00
|
|
|
|
|
|
|
// Make the "File" menu
|
|
|
|
wxMenu *fileMenu = new wxMenu;
|
2018-09-22 19:15:08 -04:00
|
|
|
fileMenu->Append(wxID_OPEN, "&Open...");
|
2005-11-14 12:38:24 -05:00
|
|
|
fileMenu->AppendSeparator();
|
2018-09-22 19:15:08 -04:00
|
|
|
fileMenu->Append(wxID_EXIT, "E&xit\tALT-X");
|
2005-11-14 12:38:24 -05:00
|
|
|
// Make the "Help" menu
|
|
|
|
wxMenu *helpMenu = new wxMenu;
|
2018-09-22 19:15:08 -04:00
|
|
|
helpMenu->Append(wxID_HELP, "&About");
|
2005-11-14 12:38:24 -05:00
|
|
|
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
2018-09-22 19:15:08 -04:00
|
|
|
menuBar->Append(fileMenu, "&File");
|
|
|
|
menuBar->Append(helpMenu, "&Help");
|
2005-11-14 12:38:24 -05:00
|
|
|
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) )
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
wxString filename = wxFileSelector("Choose DXF Model", "", "", "",
|
2005-11-14 12:38:24 -05:00
|
|
|
#if wxUSE_ZLIB
|
2018-09-22 19:15:08 -04:00
|
|
|
"DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*",
|
2005-11-14 12:38:24 -05:00
|
|
|
#else
|
2018-09-22 19:15:08 -04:00
|
|
|
"DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*",
|
2005-11-14 12:38:24 -05:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2012-01-20 11:51:09 -05:00
|
|
|
// Help|About command
|
2005-11-14 12:38:24 -05:00
|
|
|
void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
wxMessageBox("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al");
|
2005-11-14 12:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// TestGLCanvas
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
|
2000-02-28 03:22:57 -05:00
|
|
|
EVT_SIZE(TestGLCanvas::OnSize)
|
|
|
|
EVT_PAINT(TestGLCanvas::OnPaint)
|
|
|
|
EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
|
|
|
|
EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2000-02-28 03:22:57 -05:00
|
|
|
|
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);
|
|
|
|
|
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);
|
2011-05-03 12:29:04 -04:00
|
|
|
if (stream.IsOk())
|
2005-11-14 12:38:24 -05:00
|
|
|
#if wxUSE_ZLIB
|
|
|
|
{
|
2018-09-22 19:15:08 -04:00
|
|
|
if (filename.Right(3).Lower() == ".gz")
|
2005-11-14 12:38:24 -05:00
|
|
|
{
|
|
|
|
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 */
|
2012-09-08 11:46:27 -04:00
|
|
|
glEnable(GL_CULL_FACE);
|
2000-02-28 03:22:57 -05:00
|
|
|
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()
|
|
|
|
{
|
2011-08-27 08:13:13 -04:00
|
|
|
if ( !IsShownOnScreen() )
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
}
|