Tutorial - Setup Horde with Gtkmm

From Horde3D Wiki
Revision as of 22:21, 7 August 2008 by Wild13 (talk | contribs) (New page: {| border="0" | {{ContentBlock|width=800|color=white |content='''Here is a small example on how to use Horde with Gtkmm.''' This is a small tutorial on how to setup Horde with Gtk+ usin...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Here is a small example on how to use Horde with Gtkmm.

This is a small tutorial on how to setup Horde with Gtk+ using Gtkmm and Gtkglextmm.

cHordeWidget.hpp
#ifndef CHORDEWIDGET_HPP_INCLUDED
#define CHORDEWIDGET_HPP_INCLUDED

#include <iostream> 
#include <Horde3D.h>
#include <Horde3DUtils.h>
#include <cstdlib>
//We include the gtkmm header
#include <gtkmm.h>
//Now we include the gtkglmm header
#include <gtkglmm.h>

//We declare the cHordeWidget class and it inherits the Gtk::GL::DrawingArea class
class cHordeWidget : public Gtk::GL::DrawingArea{
    public:
        cHordeWidget();
        virtual ~cHordeWidget();

    protected:
        virtual void on_realize();
        virtual bool on_configure_event(GdkEventConfigure* event);
        virtual bool on_expose_event(GdkEventExpose* event);
    private:
        ResHandle logoRes;
};

#endif // CHORDEWIDGET_HPP_INCLUDED

Now for the cHordeWidget.cpp *Read the comments*

cHordeWidget.cpp
#include "../include/cHordeWidget.hpp"

NodeHandle model = 0, cam = 0;

cHordeWidget::cHordeWidget(){

    Glib::RefPtr<Gdk::GL::Config> glconfig;
    //We pick are opengl config options.
    glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE);
    //We set are options here.
    set_gl_capability(glconfig);

}

cHordeWidget::~cHordeWidget(){
    //we release the Horde3D engine here
    Horde3D::release();
}

void cHordeWidget::on_realize(){
    //We must call this base class before anything else
    Gtk::GL::DrawingArea::on_realize();

    //Get Our opengl window must do
    Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

    //Begin working with the gl context everything we do will go under here.
    glwindow->gl_begin(get_gl_context());
    //Resize our Horde3D screen
    Horde3D::resize(0,0,800,600);
    //set camera view
    Horde3D::setupCameraView(cam,45.0f,(float)800/600,0.1f,1000.0f);
    //end opengl
    glwindow->gl_end();
}

bool cHordeWidget::on_configure_event(GdkEventConfigure* event){

    //get gl window
    Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();
    //Begin working with the gl context everything we do will go under here.
    glwindow->gl_begin(get_gl_context());

    Horde3D::init();

    Horde3DUtils::setResourcePath( ResourceTypes::SceneGraph, "models" );
    Horde3DUtils::setResourcePath( ResourceTypes::Geometry, "models" );
    Horde3DUtils::setResourcePath( ResourceTypes::Animation, "models" );
    Horde3DUtils::setResourcePath( ResourceTypes::Material, "materials" );
    Horde3DUtils::setResourcePath( ResourceTypes::Code, "shaders" );
    Horde3DUtils::setResourcePath( ResourceTypes::Shader, "shaders" );
    Horde3DUtils::setResourcePath( ResourceTypes::Texture2D, "textures" );
    Horde3DUtils::setResourcePath( ResourceTypes::TextureCube, "textures" );
    Horde3DUtils::setResourcePath( ResourceTypes::Effect, "effects" );
    Horde3DUtils::setResourcePath( ResourceTypes::Pipeline, "pipelines" );

    ResHandle pipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "deferred.pipeline.xml",0);
    ResHandle modelRes = Horde3D::addResource(ResourceTypes::SceneGraph,"charecter.scene.xml",0);
    ResHandle animRes = Horde3D::addResource(ResourceTypes::Animation,"walk.anim.xml",0);
    ResHandle fontRes = Horde3D::addResource(ResourceTypes::Material,"font.material.xml",0);

    logoRes = Horde3D::addResource(ResourceTypes::Material,"logo.material.xml",0);

    Horde3DUtils::loadResourcesFromDisk("media");

    Horde3D::setOption( EngineOptions::LoadTextures, 1 );
    Horde3D::setOption( EngineOptions::TexCompression, 0 );
    Horde3D::setOption( EngineOptions::FastAnimation, 0 );
    Horde3D::setOption( EngineOptions::AnisotropyFactor, 8 );
    Horde3D::setOption( EngineOptions::ShadowMapSize, 2048 );
    Horde3D::setOption( EngineOptions::DebugViewMode,0.0f);
    model = Horde3D::addNodes(RootNode,modelRes);

    //Horde3D::setupModelAnimStage(model,0,animRes,"",true);

    NodeHandle light = Horde3D::addLightNode(RootNode,"Light1", 0,"LIGHTING","SHADOWMAP");

    Horde3D::setNodeTransform(light,0,2,0,0,00,0,1,1,1);

    Horde3D::setNodeParamf( light, LightNodeParams::Radius, 50 );
    Horde3D::setNodeParamf( light, LightNodeParams::FOV, 90 );
    Horde3D::setNodeParami( light, LightNodeParams::ShadowMapCount, 3 );
    Horde3D::setNodeParamf( light, LightNodeParams::ShadowSplitLambda, 0.9f );
    Horde3D::setNodeParamf( light, LightNodeParams::ShadowMapBias, 0.001f );
    Horde3D::setNodeParamf( light, LightNodeParams::Col_R, 0.9f );
    Horde3D::setNodeParamf( light, LightNodeParams::Col_G, 0.7f );
    Horde3D::setNodeParamf( light, LightNodeParams::Col_B, 0.75f );

    cam = Horde3D::addCameraNode(RootNode,"Camera",pipeRes);

    Horde3DUtils::dumpMessages();
    //end opengl
    glwindow->gl_end();

    return true;
}
//This on_expose_event is are game loop everything game related goes here
bool cHordeWidget::on_expose_event(GdkEventExpose* event){
    float curFps = 60;
    curFps = curFps + 10.0f * ( 1/curFps);
    
    //get gl::window
    Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();
    
    //Begin working with the gl context everything we do will go under here.
    glwindow->gl_begin(get_gl_context());

    Horde3D::setNodeTransform(model,curFps*10,0,0,0,0,0,1,1,1);
    Horde3D::showOverlay(0.75f,0,0,0,1,0,1,0,1,0.2f,1,1,0.75f,0.2f,0,1,7,logoRes);
    Horde3D::render(cam);
    Horde3D::clearOverlays();

    //end opengl
    glwindow->gl_end();
    //Since we have a double buffer opengl context we must swap them here
    glwindow->swap_buffers();

    return true;
}

Finnaly the main.cpp *Read the Comments*

main.cpp
#include <gtkmm.h>
//Include are custom widget
#include "include/cHordeWidget.hpp"

int main(int argc, char *argv[]){
    //Init Gtk+
    Gtk::Main init (argc, argv);
    //Init Gtk+ opengl
    Gtk::GL::init (argc, argv);
    //Create our Window object
    Gtk::Window window;
    //Set our window title
    window.set_title ("Horde3D In Gtk+ Window");
    //Create our cHordeWidget
    cHordeWidget drawing;
    //Set the cHordeWidgets size
    drawing.set_size_request(800, 600);
    //Add the cHordeWidget to our window
    window.add (drawing);
    //Show our cHordeWidget. All widgets are invisible by default.
    window.show_all ();
    //Run our window
    Gtk::Main::run (window);
    //Just a return statement
    return EXIT_SUCCESS;
}

--Rj 23:21, 7 August 2008 (CEST)

Horde With Gtkmm
H3DPlaceHolder.png
This tutorial introduces the use of Horde3D with Gtkmm
Version: 1.0
Compatible with Horde3D: 1.0 beta
Release date: 2008-07-8
Author(s): Raynaldo Rivera