Setting up the rendering of a simple scene
The following example shows how to implement a simple 3D application by simply subclassing the UIEventListener class. The example below uses the vlWin32 bindings but the concepts remain valid for other bindings as well.
Complete source: src/examples/Win32_direct.cpp
{
public:
MyApplet()
{
mRendering->sceneManagers()->push_back( mSceneManager.get() );
}
{
sceneManager()->tree()->addActor( teapot.
get(), effect.
get(), mCubeTransform.get() );
trackball->
setCamera( rendering()->camera() );
}
{
mCubeTransform->setLocalMatrix( matrix );
}
{
}
const vl::Rendering* rendering()
const {
return mRendering.get(); }
public:
};
int APIENTRY WinMain(HINSTANCE , HINSTANCE , LPSTR , int )
{
int x = 0, y = 0, width = 512, height= 512;
return res;
}
Implementing a rotating cube using the vl::Applet utility class
Visualization Library comes with several utility components to quickly develop applictions under several GUI frameworks. The vl::Applet class is an utility class that implements an event-driven application with a default rendering pipeline. Below is a practical examples of how to implement a vl::Applet that renders a spinning cube and how to use it (unchanged!) with Win32
, MFC
, Qt4
, SDL
and GLUT!
The vl::Applet subclasses the UIEventListener in a way very similar to the MyApplet class seen in the example above, but provides a few useful "pre-packaged" services such as quitting the application by pressing ESC, swticthing to fullscreen by pressing F1, taking a screenshot by pressing F5, both tracball and ghost manipulators etc. See the documentation page of vl::Applet for more information.
Complete source: src/examples/Applets/App_RotatingCube.hpp
{
public:
{
}
{
mCubeTransform->setLocalMatrix( matrix );
}
protected:
};
- Using the Qt4 Utility Bindings
Complete source: src/examples/Qt4_example.cpp
#include "Applets/App_RotatingCube.hpp"
int main(
int argc,
char *argv[])
{
QApplication app(argc, argv);
int x = 10;
int y = 10;
int width = 512;
int height= 512;
qt4_window->
initQt4Widget(
"Visualization Library on Qt4 - Rotating Cube", format,
NULL, x, y, width, height );
int val = app.exec();
return val;
}
- Using the wxWidgets Utility Bindings
Complete source: src/examples/WX_example.cpp
#include "Applets/App_RotatingCube.hpp"
class MyFrame: public wxFrame
{
public:
MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size, long style = wxDEFAULT_FRAME_STYLE):
wxFrame(parent, wxID_ANY, title, pos, size, style) {}
~MyFrame(){}
};
class MyApp: public wxApp
{
public:
bool OnInit();
int OnExit();
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
VisualizationLibrary::init();
MyFrame *frame =
new MyFrame(
NULL, L
"WXGLCanvas", wxDefaultPosition, wxSize(400, 300));
int context_format[] =
{
WX_GL_RGBA,
WX_GL_DOUBLEBUFFER,
WX_GL_MIN_RED, 8,
WX_GL_MIN_GREEN, 8,
WX_GL_MIN_BLUE, 8,
WX_GL_MIN_ALPHA, 8,
WX_GL_DEPTH_SIZE, 24,
WX_GL_STENCIL_SIZE, 8,
0
};
ref<WXGLCanvas> vl_gl_canvas =
new WXGLCanvas( frame,
NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE, context_format );
mat4 view_mat = mat4::getLookAt(eye, center, up);
frame->Show();
int x = 0;
int y = 0;
int width = 512;
int height= 512;
frame->SetPosition( wxPoint(x,y) );
frame->SetClientSize( wxSize(width,height) );
frame->SetLabel(wxT("Visualization Library on wxWindows - Rotating Cube"));
return true;
}
int MyApp::OnExit()
{
VisualizationLibrary::shutdown();
return 0;
}
- Using the Win32 Utility Bindings
Complete source: src/examples/Win32_example.cpp
#include "Applets/App_RotatingCube.hpp"
int APIENTRY WinMain(HINSTANCE , HINSTANCE , LPSTR , int )
{
int x = 0;
int y = 0;
int width = 512;
int height= 512;
return res;
}
- Using the MFC Utility Bindings
Complete source: src/examples/MFC_example/MFC_example.cpp
#include "Applets/App_RotatingCube.hpp"
class MFC_Example: public CWinApp
{
public:
MFC_Example() {}
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual BOOL OnIdle(LONG lCount);
protected:
DECLARE_MESSAGE_MAP ()
};
BEGIN_MESSAGE_MAP(MFC_Example, CWinApp)
END_MESSAGE_MAP()
MFC_Example mfc_app;
BOOL MFC_Example::OnIdle(LONG lCount)
{
if( mVLCWin->continuousUpdate() )
mVLCWin->Win32Context::update();
else
Sleep(1);
return TRUE;
}
int MFC_Example::ExitInstance()
{
CWinApp::ExitInstance();
return 0;
}
BOOL MFC_Example::InitInstance()
{
CWinApp::InitInstance();
int x = 100;
int y = 100;
int width = 512;
int height= 512;
mVLCWin->initMFCWindow(
NULL,
NULL,
"Visualization Library on MFC - Rotating Cube", format, x, y, width, height);
m_pMainWnd = mVLCWin.get();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
- Using the SDL GUI Utility Bindings
Complete source: src/examples/SDL_example.cpp
#include "Applets/App_RotatingCube.hpp"
int main(
int argc,
char* args[])
{
int x = 0;
int y = 0;
int width = 512;
int height= 512;
sdl_window->
initSDLWindow(
"Visualization Library on SDL - Rotating Cube", format, x, y, width, height );
return 0;
}
- Using the GLUT GUI Utility Bindings
Complete source: src/examples/GLUT_example.cpp
#include "Applets/App_RotatingCube.hpp"
int main (
int argc,
char *argv[] )
{
int pargc = argc;
glutInit( &pargc, argv );
int x = 0;
int y = 0;
int width = 512;
int height= 512;
glut_window->
initGLUTWindow(
"Visualization Library on GLUT - Rotating Cube", format, x, y, width, height );
glutMainLoop();
return 0;
}