Tutorial - Setup Horde with Qt4

From Horde3D Wiki
Revision as of 20:12, 8 August 2008 by Wild13 (talk | contribs)
Jump to: navigation, search
Here is a small example on how to use Horde with Qt4.

Keep in mind that the qt license is gpl and horde is lgpl and in order to use your app commercially you need to purchase a Qt license.

glwidget.h
#ifndef GLWIDGET_H_INCLUDED

#define GLWIDGET_H_INCLUDED

#include <QtOpenGL/qgl.h>
#include <QKeyEvent>
#include <Horde3D.h>
#include <Horde3DUtils.h>


class GLWidget : public QGLWidget{
    Q_OBJECT

    public:
        GLWidget(QWidget *parent = 0);
        ~GLWidget();
        QSize minimumSizeHint() const;
        QSize sizeHint() const;

    protected:
        void initializeGL();
        void paintGL();
        void resizeGL(int width,int height);
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void keyPressEvent(QKeyEvent *event);

    private:
        ResHandle logoRes;
};



#endif // GLWIDGET_H_INCLUDED

Now the cpp file. the paintGL() function is the game loop.

glwidget.cpp
#include "../include/glwidget.h"

NodeHandle model = 0, cam = 0;

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent){
    resize(sizeHint());
}

GLWidget::~GLWidget(){
    Horde3D::release();
}

QSize GLWidget::minimumSizeHint() const{
    return QSize(50,50);
}

QSize GLWidget::sizeHint() const{
    return QSize(800,600);
}

void GLWidget::initializeGL(){
    if(!Horde3D::init()){
        Horde3DUtils::dumpMessages();
        return;
    }

    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);

}

void GLWidget::paintGL(){
    float curFps = 60;
    curFps = curFps + 10.0f * ( 1/curFps);
    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();
    Horde3DUtils::dumpMessages();
}

void GLWidget::resizeGL(int width,int height){
    Horde3D::resize(0,0,800,600);
    Horde3D::setupCameraView(cam,45.0f,(float)width /height,0.1f,1000.0f);
}

void GLWidget::mousePressEvent(QMouseEvent *event){
    if (event->buttons() & Qt::LeftButton)
    {
      std::cout<<"left mouse button pressed";
    } 

}

void GLWidget::mouseMoveEvent(QMouseEvent *event){
    QPoint pnt = event->pos();
    
    if ( pnt.x() == 0 )
            std::cout<<"pointers xvalue equals zero";
    
    if ( pnt.y() == 0 )
            std::cout<<"pointers yvalue equals zero";
}

void GLWidget::keyPressEvent(QKeyEvent *event){

	switch( event->key() )
	{
	case Qt::Key_Up:
                std::cout<<"up pressed";

		break;
	case Qt::Key_Down:
                std::cout<<"down pressed";
                 
                break;

	default:
		GLWidget::keyPressEvent( event );
	}

}

The very simple main.cpp

main.cpp
#include <qapplication.h>
#include <include/glwidget.h>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    GLWidget widget;

    widget.show();

    return app.exec();
}

Here is the custom pro file customized for ubuntu and my box you will need to modify this to your paths.

main.pro
######################################################################
# Automatically generated by qmake (2.01a) Mon Aug 4 18:39:01 2008
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += . include src
INCLUDEPATH += . include /usr/include/Horde3D1.0.0/Bindings/C++
LIBS += -L/usr/include/Horde3D1.0.0/Binaries/Linux_x86 -lHorde3D -lHorde3DUtils

QT += opengl

# Input
HEADERS += include/glwidget.h
SOURCES += main.cpp src/glwidget.cpp

--Rj 05:28, 5 August 2008 (CEST)

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