Wx::GLCanvas

From WxPerl wiki

Jump to: navigation, search

Please find an example of code to get Wx::GLCanvas started.

#!/usr/bin/perl -w

#############################################################################
## Name:        wxglcanvasexample
## Purpose:     getting started with Wx::GLCanvas
## Author:      Wim de Vries
## Modified by:
## Created:     12/08/2009
## Copyright:   (c) 2009 Wim de Vries
## Licence:     This program is free software; you can redistribute it and/or
##              modify it under the same terms as Perl itself
#############################################################################
use strict;

my $app;
package MyApp;
use strict;
use vars qw(@ISA);
@ISA=qw(Wx::App);

my $frame;
my $GL;

sub OnInit {
my $this = @_;
$frame = MyFrame->new( "WxGLCanvasExample", [10,10], [800,800]); #instance of the package below; the real wxframe is created up there
unless ($frame) {print "unable to create frame -- exiting."; return undef};
$frame->{TIMER}->Start(1000);#just checking if (timer) messaging control stays in $frame
$GL = new MyCanvas();#parameters are set in the (new) constructor itself
$frame->Show( 1 );
1; #everything's ok, on with the show.
}

1;

#######################################################
package MyFrame;
use vars qw(@ISA);
use strict;
use base qw(Wx::GLCanvas Class::Accessor::Fast);
use Wx qw(wxVSCROLL wxSTAY_ON_TOP wxWidth wxHeight wxBITMAP_TYPE_PNG wxMINIMIZE_BOX wxSYSTEM_MENU wxCAPTION wxCLOSE_BOX wxCLIP_CHILDREN wxNullBitmap wxRED_PEN wxCOPY wxRED_BRUSH wxRED wxSOLID wxSP_3DBORDER wxBLACK wxWHITE wxFONTFAMILY_DEFAULT wxFONTSTYLE_NORMAL wxFONTWEIGHT_BOLD  wxFONTENCODING_DEFAULT wxSYS_SYSTEM_FONT wxSIMPLE_BORDER wxBLUE wxNO_BORDER wxDEFAULT_DIALOG_STYLE wxYES_NO wxICON_QUESTION wxALPHA_OPAQUE wxALWAYS_SHOW_SB wxTRANSPARENT wxNullPalette); #the WXconstants you will use
@ISA=qw(Wx::Frame);
use Wx::Event qw(:everything);

#****************************** FRAME NEW ************************************************************
sub new { #of frame
my $class = shift;
my $this = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2], wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION |wxCLOSE_BOX | wxCLIP_CHILDREN);
$this->{TIMER} = Wx::Timer->new( $this, 1 );
EVT_TIMER( $this, 1, \&OnTimer );
print "wxversion:".Wx::wxVERSION."\n";
return $this;  # return the MyFrame object (instance of class Wx::Frame) to the calling application.
};

sub OnTimer {
print "time\n"; #should be printed in the console every second
}


package MyCanvas;
use strict;
use Wx qw(wxNullPalette wxID_ANY wxSIMPLE_BORDER wxVSCROLL);#whatever WXconstants you use here 
use OpenGL qw/ :all /;
use Wx::GLCanvas qw/ :all /;

use vars qw(@ISA);
@ISA=qw(Wx::GLCanvas);
use Wx::Event qw(:everything);
my $canvas;
my $context;
my $firstPaint = 1;

sub new {
my $class = shift;
$canvas = $class->SUPER::new($frame, wxID_ANY,[0,0],[800,800],wxSIMPLE_BORDER | wxVSCROLL,"",[WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0]);#
print "Wx::GLCanvas version:".$Wx::GLCanvas::VERSION."\n";
EVT_PAINT( $canvas, \&OnPaint );
return $canvas;
};

sub OnPaint {
my($this) = @_;
print "painting\n";
if ($firstPaint)
  {$this->SetCurrent();
   $firstPaint = 0;
  };
my $paintDc = Wx::PaintDC->new($this);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
   glViewport (0, 0, 800, 800);#this is the default
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(-400, 400, 400, -400);
   glMatrixMode(GL_MODELVIEW);
	glBegin(GL_LINES);
		glVertex3f(-300, -300, 0.0);
		glVertex3f(-300, 300, 0.0);

		glVertex3f(-300, 300, 0.0);
		glVertex3f(300, 300, 0.0);

		glVertex3f(300, 300, 0.0);
		glVertex3f(300, -300, 0.0);

		glVertex3f(300, -300, 0.0);
		glVertex3f(-300, -300, 0.0);
	glEnd();
glFlush ();
$this->SwapBuffers();
}

package main;
$app = MyApp->new();# create an instance of the Wx::App-derived class
$app->MainLoop(); # run
Personal tools
Google AdSense