wxOGL

From WxWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This wiki is just an adaption from the one existing in wxPyWiki to the C++ language. It will include at least the information posted there and any other you will like to contributed. Some parts has been just copy-paste, my apologize if this violate some copyrights.

Status

Development Status

  • OGL development appears to be dead.
  • There does not appear to be OGL maintenance.

Object Graphics Library (OGL) is a C++ library supporting the creation and manipulation of simple and complex graphic images on a canvas. It can be found in the directory utils/ogl/src in the wxWindows distribution. The file ogl.h must be included to make use of the library.

Documentation Status

The standard OGL documentation is old and erronius.

Standard OGL docs consist of:

  • class library documentation
  • notes
  • a complicated sample

This wiki entry is an effort to fix the situation.

If you are using OGL, please post your notes and corrections to this wiki page..!

Sample Code

Minimal wxOGL Application

This is a minimal application demonstrating wxOGL, contributed by LionKimbro

#include "wx/wx.h"
#include "wx/ogl/ogl.h"

class MyApp: public wxApp
{
	virtual bool OnInit();
};

class MyFrame: public wxFrame
{
	wxDiagram * diagram;
	wxShape * shape;

public:

	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
	~MyFrame();
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	MyFrame *frame = new MyFrame(_("wxWidgets - Object Graphics Library"),
	                             wxPoint(50, 50), wxSize(450, 340) );
	frame->Show(TRUE);
	SetTopWindow(frame);
	return TRUE;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
	wxShapeCanvas *canvas = new wxShapeCanvas(this, wxID_ANY, pos, size, 0, _T("a"));      

	canvas->SetBackgroundColour(*wxWHITE);
	canvas->SetCursor(wxCursor(wxCURSOR_CROSS));

	diagram = new wxDiagram();	

	canvas->SetDiagram(diagram);

	shape = new wxCircleShape(20.0);
	shape->SetX(25.0);
	shape->SetY(25.0);
	canvas->AddShape(shape);

	diagram->ShowAll(1);
}

MyFrame::~MyFrame()
{
	delete shape;
	delete diagram;
}

The above code just create the wxShapeCanvas and graph a circle on it. Although that not so much that will help you get started.

Mouse event in circle

This example code, let you define some events in the shape, for example It has implemented the left ans right click.

#include "wx/wx.h"
#include "wx/ogl/ogl.h"

class MyApp: public wxApp
{
	virtual bool OnInit();
};

class OGLFrame: public wxFrame
{
public:
	OGLFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};

class OGLCanvas: public wxShapeCanvas
{
public:
	OGLCanvas(wxWindow *parent = NULL, wxWindowID id = wxID_ANY,
	          const wxPoint& pos = wxDefaultPosition,
	          const wxSize& size = wxDefaultSize, long style = wxRETAINED);
	~OGLCanvas(void);
};

class MyEvtHandler: public wxShapeEvtHandler
{
public:
	MyEvtHandler(wxShapeEvtHandler *prev = NULL, wxShape *shape = NULL, 
	             const wxString& lab = wxEmptyString)
	: wxShapeEvtHandler(prev, shape) { }

	~MyEvtHandler(void) {}

	void OnLeftClick(double x, double y, int keys = 0, int attachment = 0);
	void OnRightClick(double x, double y, int keys = 0, int attachment = 0);    
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	OGLFrame *frame = new OGLFrame(_("wxOGL"), wxPoint(50, 50), wxSize(450, 340) );
	frame->Show(true);	
	SetTopWindow(frame);
	return true;
}

OGLFrame::OGLFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame*)NULL, -1, title, pos, size)
{
	OGLCanvas *canvas = new OGLCanvas(this);	
}

OGLCanvas::OGLCanvas(wxWindow *parent, wxWindowID id, const wxPoint& pos,
                     const wxSize& size, long style)
: wxShapeCanvas(parent, id, pos, size, style)
{
	SetBackgroundColour(*wxWHITE);
	SetCursor(wxCursor(wxCURSOR_CROSS));
	wxDiagram *diagram = new wxDiagram();	
	SetDiagram(diagram);		

	wxCircleShape *circle = new wxCircleShape(100);	
	circle->SetX(200);
	circle->SetY(200);
	AddShape(circle);	
	diagram->ShowAll(1);

	MyEvtHandler *evthandler = new MyEvtHandler();
	evthandler->SetShape(circle);
	evthandler->SetPreviousHandler(circle->GetEventHandler());
	circle->SetEventHandler(evthandler);
}

OGLCanvas::~OGLCanvas(void)
{
}

void MyEvtHandler::OnLeftClick(double WXUNUSED(x), double WXUNUSED(y),
                               int keys, int WXUNUSED(attachment))
{
	wxMessageBox(wxT("Left click"), wxT("wxOGL"), wxOK | wxICON_INFORMATION);
}

void MyEvtHandler::OnRightClick(double WXUNUSED(x), double WXUNUSED(y),
                                int keys, int WXUNUSED(attachment))
{
	wxMessageBox(wxT("Right click"), wxT("wxOGL"), wxOK | wxICON_INFORMATION);
}

Compiling Sample Code

Although this is a very basic thing, maybe some newbies like I was (or am?) need it.

g++ ogl.cpp -o ogl `wx-config --libs` `wx-config --cxxflags` -lwx_gtk2u_ogl-2.8