Difference between revisions of "Tutorial - Setup Horde with Gtkmm"
From Horde3D Wiki
Line 31: | Line 31: | ||
virtual ~cHordeWidget(); | virtual ~cHordeWidget(); | ||
bool on_timeout(); | bool on_timeout(); | ||
− | + | float _x, _y, _z, _rx, _ry; // Viewer position and orientation | |
+ | float _curFPS; | ||
protected: | protected: | ||
virtual void on_realize(); | virtual void on_realize(); | ||
virtual bool on_configure_event(GdkEventConfigure* event); | virtual bool on_configure_event(GdkEventConfigure* event); | ||
virtual bool on_expose_event(GdkEventExpose* event); | virtual bool on_expose_event(GdkEventExpose* event); | ||
− | + | virtual bool on_motion_notify_event(GdkEventMotion* event); | |
void init_hordeScene(); | void init_hordeScene(); | ||
− | |||
void setup_hordeViewport(); | void setup_hordeViewport(); | ||
− | |||
void render_hordeScene(); | void render_hordeScene(); | ||
− | |||
void update_hordeScene(); | void update_hordeScene(); | ||
− | |||
void get_FramesPerSecond(); | void get_FramesPerSecond(); | ||
− | |||
private: | private: | ||
− | ResHandle _pipeRes, _fontMatRes, _logoMatRes, _hdrPipeRes, _forwardPipeRes; | + | ResHandle _pipeRes, _fontMatRes, _logoMatRes, _hdrPipeRes, _forwardPipeRes, _deferredPipeRes; |
NodeHandle _cam, _knight, _particleSys; | NodeHandle _cam, _knight, _particleSys; | ||
− | + | ||
− | |||
bool _freeze; | bool _freeze; | ||
Line 59: | Line 54: | ||
int frames; | int frames; | ||
− | Glib::Timer m_animtimer; | + | Glib::Timer m_animtimer; |
− | Glib::Timer m_fpstimer; | + | Glib::Timer m_fpstimer; |
}; | }; | ||
Line 79: | Line 74: | ||
cHordeWidget::cHordeWidget(){ | cHordeWidget::cHordeWidget(){ | ||
− | |||
− | |||
− | |||
Glib::RefPtr<Gdk::GL::Config> glconfig; | Glib::RefPtr<Gdk::GL::Config> glconfig; | ||
− | + | ||
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE); | glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE); | ||
− | + | add_events(Gdk::POINTER_MOTION_MASK); | |
set_gl_capability(glconfig); | set_gl_capability(glconfig); | ||
− | + | Glib::signal_timeout().connect( sigc::mem_fun(*this, &cHordeWidget::on_timeout), 10); | |
− | Glib::signal_timeout().connect( sigc::mem_fun(*this, &cHordeWidget::on_timeout), | + | m_animtimer.start(); |
} | } | ||
Line 171: | Line 163: | ||
Horde3D::setOption( EngineOptions::ShadowMapSize, 2048 ); | Horde3D::setOption( EngineOptions::ShadowMapSize, 2048 ); | ||
− | |||
− | |||
_hdrPipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "hdr.pipeline.xml", 0 ); | _hdrPipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "hdr.pipeline.xml", 0 ); | ||
_forwardPipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "forward.pipeline.xml", 0 ); | _forwardPipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "forward.pipeline.xml", 0 ); | ||
+ | _deferredPipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "deferred.pipeline.xml",0); | ||
// Font | // Font | ||
_fontMatRes = Horde3D::addResource( ResourceTypes::Material, "font.material.xml", 0 ); | _fontMatRes = Horde3D::addResource( ResourceTypes::Material, "font.material.xml", 0 ); | ||
Line 188: | Line 179: | ||
ResHandle particleSysRes = Horde3D::addResource( ResourceTypes::SceneGraph, "particleSys1.scene.xml", 0 ); | ResHandle particleSysRes = Horde3D::addResource( ResourceTypes::SceneGraph, "particleSys1.scene.xml", 0 ); | ||
− | + | ResHandle skyBoxRes = Horde3D::addResource( ResourceTypes::SceneGraph, "skybox.scene.xml" ,0); | |
+ | // Load resources | ||
Horde3DUtils::loadResourcesFromDisk( "media" ); | Horde3DUtils::loadResourcesFromDisk( "media" ); | ||
− | + | ||
// Add camera | // Add camera | ||
− | _cam = Horde3D::addCameraNode( RootNode, "Camera", | + | _cam = Horde3D::addCameraNode( RootNode, "Camera", _forwardPipeRes ); |
//Horde3D::setNodeParami( _cam, CameraNodeParams::OcclusionCulling, 1 ); | //Horde3D::setNodeParami( _cam, CameraNodeParams::OcclusionCulling, 1 ); | ||
// Add environment | // Add environment | ||
NodeHandle env = Horde3D::addNodes( RootNode, envRes ); | NodeHandle env = Horde3D::addNodes( RootNode, envRes ); | ||
+ | |||
Horde3D::setNodeTransform( env, 0, -20, 0, 0, 0, 0, 20, 20, 20 ); | Horde3D::setNodeTransform( env, 0, -20, 0, 0, 0, 0, 20, 20, 20 ); | ||
// Add knight | // Add knight | ||
Line 203: | Line 196: | ||
Horde3D::setupModelAnimStage( _knight, 0, knightAnim1Res, "", false ); | Horde3D::setupModelAnimStage( _knight, 0, knightAnim1Res, "", false ); | ||
Horde3D::setupModelAnimStage( _knight, 1, knightAnim2Res, "", false ); | Horde3D::setupModelAnimStage( _knight, 1, knightAnim2Res, "", false ); | ||
+ | |||
// Attach particle system to hand joint | // Attach particle system to hand joint | ||
Horde3D::findNodes( _knight, "Bip01_R_Hand", SceneNodeTypes::Joint ); | Horde3D::findNodes( _knight, "Bip01_R_Hand", SceneNodeTypes::Joint ); | ||
Line 209: | Line 203: | ||
Horde3D::setNodeTransform( _particleSys, 0, 40, 0, 90, 0, 0, 1, 1, 1 ); | Horde3D::setNodeTransform( _particleSys, 0, 40, 0, 90, 0, 0, 1, 1, 1 ); | ||
+ | //add skybox | ||
+ | NodeHandle sky = Horde3D::addNodes( RootNode, skyBoxRes); | ||
+ | //set skybox position | ||
+ | Horde3D::setNodeTransform( sky, 0, 0, 0, 0, 0, 0, 210, 50, 210 ); | ||
// Add light source | // Add light source | ||
NodeHandle light = Horde3D::addLightNode( RootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" ); | NodeHandle light = Horde3D::addLightNode( RootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" ); | ||
Line 237: | Line 235: | ||
void cHordeWidget::render_hordeScene(){ | void cHordeWidget::render_hordeScene(){ | ||
− | m_fpstimer.start(); | + | m_fpstimer.start(); |
− | //Horde3D::setOption( EngineOptions::DebugViewMode, 1.0f ); | + | //Horde3D::setOption( EngineOptions::DebugViewMode, _debugViewMode ? 1.0f : 0.0f ); |
− | //Horde3D::setOption( EngineOptions::WireframeMode, 1.0f ); | + | //Horde3D::setOption( EngineOptions::WireframeMode, _wireframeMode ? 1.0f : 0.0f ); |
// Do animation blending | // Do animation blending | ||
Line 254: | Line 252: | ||
// Set camera parameters | // Set camera parameters | ||
− | |||
Line 260: | Line 257: | ||
Horde3D::showOverlay( 0.75f, 0, 0, 0, 1, 0, 1, 0, | Horde3D::showOverlay( 0.75f, 0, 0, 0, 1, 0, 1, 0, | ||
1, 0.2f, 1, 1, 0.75f, 0.2f, 0, 1, | 1, 0.2f, 1, 1, 0.75f, 0.2f, 0, 1, | ||
− | + | 7, _logoMatRes ); | |
// Render scene | // Render scene | ||
Line 268: | Line 265: | ||
Horde3D::clearOverlays(); | Horde3D::clearOverlays(); | ||
− | get_FramesPerSecond(); | + | get_FramesPerSecond(); |
} | } | ||
Line 274: | Line 271: | ||
void cHordeWidget::update_hordeScene(){ | void cHordeWidget::update_hordeScene(){ | ||
− | double seconds = m_animtimer.elapsed(); | + | double seconds = m_animtimer.elapsed(); |
− | _animTime += seconds * 30; | + | _animTime += seconds * 30; |
− | m_animtimer.reset(); | + | m_animtimer.reset(); |
− | + | Horde3D::setNodeTransform( _cam, _x, _y, _z, _rx ,_ry, 0, 1, 1, 1 ); | |
− | Gtk::Widget::queue_draw(); | + | Gtk::Widget::queue_draw(); |
} | } | ||
Line 295: | Line 292: | ||
} | } | ||
− | std::cout<<_curFPS<<endl; | + | std::cout<<_curFPS<<endl; |
} | } | ||
+ | //This event handler must be set and return false in order for are mouse movement to be captured | ||
+ | bool cHordeWidget::on_motion_notify_event(GdkEventMotion* event){ | ||
− | + | return false; | |
+ | } | ||
</source> | </source> | ||
Line 312: | Line 312: | ||
#define CWINDOW_HPP_INCLUDED | #define CWINDOW_HPP_INCLUDED | ||
#include <gtkmm.h> | #include <gtkmm.h> | ||
+ | #include <math.h> | ||
#include "cHordeWidget.hpp" | #include "cHordeWidget.hpp" | ||
Line 322: | Line 323: | ||
virtual bool on_key_press_event(GdkEventKey* event); | virtual bool on_key_press_event(GdkEventKey* event); | ||
− | + | virtual bool on_key_release_event(GdkEventKey* event); | |
+ | virtual bool on_motion_notify_event(GdkEventMotion* event); | ||
+ | virtual bool on_focus_in (GdkEventFocus*); | ||
+ | bool on_timeout(); | ||
+ | void handleinput(); | ||
private: | private: | ||
Gtk::VBox m_vBox; | Gtk::VBox m_vBox; | ||
Line 341: | Line 346: | ||
#include "../include/cWindow.hpp" | #include "../include/cWindow.hpp" | ||
+ | |||
+ | inline float degToRad( float f ) | ||
+ | { | ||
+ | return f * (3.1415926f / 180.0f); | ||
+ | } | ||
+ | //array to hold keys | ||
+ | int keyDown[256]; | ||
cWindow::cWindow(){ | cWindow::cWindow(){ | ||
− | |||
set_title("Horde with gtkmm"); | set_title("Horde with gtkmm"); | ||
− | |||
set_reallocate_redraws(true); | set_reallocate_redraws(true); | ||
− | + | ||
add(m_vBox); | add(m_vBox); | ||
− | + | add_events(Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK | | |
+ | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK); | ||
+ | Glib::signal_timeout().connect( sigc::mem_fun(*this, &cWindow::on_timeout), 10); | ||
+ | signal_focus_in_event().connect(sigc::mem_fun(*this, &cWindow::on_focus_in) ); | ||
m_hordeWidget.set_size_request(800,600); | m_hordeWidget.set_size_request(800,600); | ||
− | + | ||
m_vBox.pack_start(m_hordeWidget); | m_vBox.pack_start(m_hordeWidget); | ||
− | + | ||
show_all(); | show_all(); | ||
} | } | ||
Line 363: | Line 376: | ||
bool cWindow::on_key_press_event(GdkEventKey* event){ | bool cWindow::on_key_press_event(GdkEventKey* event){ | ||
− | + | if(event->keyval == GDK_Escape){ | |
− | + | Gtk::Main::quit();}else{ | |
− | + | ||
− | + | //sets the key in the array to true | |
− | + | keyDown[event->keyval] = 1;} | |
+ | return true; | ||
+ | } | ||
+ | |||
+ | bool cWindow::on_key_release_event(GdkEventKey *event){ | ||
+ | //sets the key in the array to false | ||
+ | keyDown[event->keyval] = 0; | ||
+ | return true; | ||
+ | } | ||
− | + | void cWindow::handleinput(){ | |
− | + | float curVel = 240/m_hordeWidget._curFPS; | |
− | + | //checks if a certain key is true and acts on it | |
+ | if(keyDown['w']){ | ||
+ | m_hordeWidget._x -= sinf( degToRad( m_hordeWidget._ry ) ) * cosf( -degToRad( m_hordeWidget._rx ) ) * curVel; | ||
+ | m_hordeWidget._y -= sinf( -degToRad( m_hordeWidget._rx ) ) * curVel; | ||
+ | m_hordeWidget._z -= cosf( degToRad( m_hordeWidget._ry ) ) * cosf( -degToRad( m_hordeWidget._rx ) ) * curVel; | ||
+ | } | ||
− | + | if(keyDown['s']){ | |
− | + | m_hordeWidget._x += sinf( degToRad( m_hordeWidget._ry ) ) * cosf( -degToRad( m_hordeWidget._rx ) ) * curVel; | |
− | + | m_hordeWidget._y += sinf( -degToRad( m_hordeWidget._rx ) ) * curVel; | |
+ | m_hordeWidget._z += cosf( degToRad( m_hordeWidget._ry ) ) * cosf( -degToRad( m_hordeWidget._rx ) ) * curVel; | ||
} | } | ||
+ | |||
+ | if(keyDown['a']){ | ||
+ | m_hordeWidget._x += sinf( degToRad( m_hordeWidget._ry - 90) ) * curVel; | ||
+ | m_hordeWidget._z += cosf( degToRad( m_hordeWidget._ry - 90 ) ) * curVel; | ||
+ | } | ||
+ | |||
+ | if(keyDown['d']){ | ||
+ | m_hordeWidget._x += sinf( degToRad( m_hordeWidget._ry + 90 ) ) * curVel; | ||
+ | m_hordeWidget._z += cosf( degToRad( m_hordeWidget._ry + 90 ) ) * curVel; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | //called only when mouse is moved *cause for the camera appearing glitchy* | ||
+ | bool cWindow::on_motion_notify_event(GdkEventMotion* event){ | ||
+ | |||
+ | m_hordeWidget._ry -= (event->x -(get_width() /2)) / 450 ; | ||
+ | |||
+ | m_hordeWidget._rx -= (event->y -(get_height()/2)) / 450 ; | ||
+ | |||
+ | if( m_hordeWidget._rx > 90 ) m_hordeWidget._rx = 90; | ||
+ | if( m_hordeWidget._rx < -90 ) m_hordeWidget._rx = -90; | ||
+ | |||
+ | return false; | ||
+ | } | ||
+ | //called constantly | ||
+ | bool cWindow::on_timeout(){ | ||
+ | int xp = 0; | ||
+ | int yp = 0; | ||
+ | get_pointer(xp,yp); | ||
+ | handleinput(); | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | bool cWindow::on_focus_in(GdkEventFocus* event ) | ||
+ | { | ||
+ | //Hide the curosr | ||
+ | // Ok, here we create a dummy cursor. See gdkmm reference for more details | ||
+ | Gdk::Cursor cursor = Gdk::Cursor(Gdk::Display::get_default(), | ||
+ | Gdk::Pixbuf::create( | ||
+ | Gdk::COLORSPACE_RGB, true, 8, 1, 1), | ||
+ | 0, 0); | ||
+ | |||
+ | get_window()->set_cursor(cursor); | ||
return true; | return true; |
Revision as of 07:56, 15 August 2008
|
|