Difference between revisions of "Tutorial - Setup Horde with Qt4"

From Horde3D Wiki
Jump to: navigation, search
(updated the tutorial)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{|  border="0"  
 
{|  border="0"  
−
| {{ContentBlock|width=800|color=white
+
| {{ContentBlock|color=white
−
|content='''Here is a small example on how to use Horde with Qt4.'''
+
|content=Before you start you should read QtOpenGL tutorial: http://doc.trolltech.com/4.6/opengl-hellogl.html (Substitute correct version of your Qt distribution).
−
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.
+
 
 +
Horde3D library should be already installed for build scripts to succeed.
 
{{CppSourceCode|
 
{{CppSourceCode|
−
description= glwidget.h|
+
description= GLWidget.h|
 
code=
 
code=
 
<source lang="cpp" line="1">
 
<source lang="cpp" line="1">
 +
#ifndef GL_WIDGET_H
 +
#define GL_WIDGET_H
  
−
#ifndef GLWIDGET_H_INCLUDED
+
#include <QtOpenGL>
−
 
+
#include <horde3d/Horde3D.h>
−
#define GLWIDGET_H_INCLUDED
+
#include <horde3d/Horde3DUtils.h>
−
 
 
−
#include <QtOpenGL/qgl.h>
 
−
#include <QKeyEvent>
 
−
#include <Horde3D.h>
 
−
#include <Horde3DUtils.h>
 
  
−
 
+
class GLWidget : public QGLWidget {
−
class GLWidget : public QGLWidget{
 
 
     Q_OBJECT
 
     Q_OBJECT
−
 
 
     public:
 
     public:
−
         GLWidget(QWidget *parent = 0);
+
         GLWidget();
 
         ~GLWidget();
 
         ~GLWidget();
−
         QSize minimumSizeHint() const;
+
 
−
         QSize sizeHint() const;
+
         QSize minimumSizeHint() const {
 +
            return QSize(640, 480);
 +
        }
 +
         QSize sizeHint() const {
 +
            return QSize(640, 480);
 +
        }
  
 
     protected:
 
     protected:
 
         void initializeGL();
 
         void initializeGL();
 
         void paintGL();
 
         void paintGL();
−
         void resizeGL(int width,int height);
+
         void resizeGL(int width, int height);
−
        void mousePressEvent(QMouseEvent *event);
 
−
        void mouseMoveEvent(QMouseEvent *event);
 
−
        void keyPressEvent(QKeyEvent *event);
 
  
 
     private:
 
     private:
−
         ResHandle logoRes;
+
         H3DRes mCam;
 
};
 
};
  
−
 
+
#endif // GL_WIDGET_H
−
 
 
−
#endif // GLWIDGET_H_INCLUDED
 
 
</source>
 
</source>
 
}}
 
}}
−
Now the cpp file. the paintGL() function is the game loop.
+
{{CppSourceCode|width=1024|
−
{{CppSourceCode|
+
description= GLWidget.cpp|
−
description= glwidget.cpp|
 
 
code=
 
code=
 
<source lang="cpp" line="1">
 
<source lang="cpp" line="1">
 +
#include "GLWidget.h"
 +
#include <stdexcept>
  
−
#include "../include/glwidget.h"
+
GLWidget::GLWidget() : QGLWidget() { }
−
 
 
−
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{
+
GLWidget::~GLWidget() {
−
     return QSize(800,600);
+
     h3dutDumpMessages();
 +
    h3dRelease();
 
}
 
}
  
−
void GLWidget::initializeGL(){
+
void GLWidget::initializeGL() {
−
     if(!Horde3D::init()){
+
     if (!h3dInit()) {
−
         Horde3DUtils::dumpMessages();
+
         h3dutDumpMessages();
−
         return;
+
         throw std::runtime_error("Could not initialize renderer");
 
     }
 
     }
−
 
+
     H3DRes pipeline = h3dAddResource(
−
     Horde3DUtils::setResourcePath( ResourceTypes::SceneGraph, "models" );
+
        H3DResTypes::Pipeline, "pipelines/forward.pipeline.xml", 0);
−
    Horde3DUtils::setResourcePath( ResourceTypes::Geometry, "models" );
+
     H3DRes knight = h3dAddResource(
−
    Horde3DUtils::setResourcePath( ResourceTypes::Animation, "models" );
+
        H3DResTypes::SceneGraph , "models/knight/knight.scene.xml", 0);
−
    Horde3DUtils::setResourcePath( ResourceTypes::Material, "materials" );
+
     h3dutLoadResourcesFromDisk("Content");
−
    Horde3DUtils::setResourcePath( ResourceTypes::Code, "shaders" );
+
     H3DNode node = h3dAddNodes(H3DRootNode, knight);
−
    Horde3DUtils::setResourcePath( ResourceTypes::Shader, "shaders" );
+
     h3dSetNodeTransform(node, 0,0,0, 0,0,0, 1,1,1);
−
    Horde3DUtils::setResourcePath( ResourceTypes::Texture2D, "textures" );
+
     mCam = h3dAddCameraNode(H3DRootNode, "cam", pipeline);
−
    Horde3DUtils::setResourcePath( ResourceTypes::TextureCube, "textures" );
+
     h3dSetNodeTransform(mCam, 0,40,-40, 23,-166,0, 1,1,1);
−
    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){
+
void GLWidget::paintGL() {
−
     Horde3D::resize(0,0,800,600);
+
     h3dRender(mCam);
−
    Horde3D::setupCameraView(cam,45.0f,(float)width /height,0.1f,1000.0f);
 
 
}
 
}
  
−
void GLWidget::mousePressEvent(QMouseEvent *event){
+
void GLWidget::resizeGL(int width, int height) {
−
     if (event->buttons() & Qt::LeftButton)
+
     h3dSetupViewport(0, 0, width, height, true);
−
    {
+
     h3dSetupCameraView(
−
      std::cout<<"left mouse button pressed";
+
        mCam, 45.0f, static_cast<float>(width)/height, 0.1f, 1000.0f);
−
     }
 
−
 
 
−
}
 
−
 
 
−
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 );
 
−
    }
 
−
 
 
 
}
 
}
 
</source>
 
</source>
 
}}
 
}}
−
The very simple main.cpp
 
 
{{CppSourceCode|
 
{{CppSourceCode|
 
description= main.cpp|
 
description= main.cpp|
 
code=
 
code=
 
<source lang="cpp" line="1">
 
<source lang="cpp" line="1">
 +
#include "GLWidget.h"
 +
#include <QApplication>
  
−
#include <qapplication.h>
+
int main(int argc, char* argv[]) {
−
#include <include/glwidget.h>
 
−
 
 
−
int main(int argc, char* argv[])
 
−
{
 
 
     QApplication app(argc, argv);
 
     QApplication app(argc, argv);
−
     GLWidget widget;
+
     GLWidget glw;
−
 
+
     glw.show();
−
     widget.show();
 
−
 
 
 
     return app.exec();
 
     return app.exec();
 
}
 
}
 
</source>
 
</source>
 
}}
 
}}
−
Here is the custom pro file customized for ubuntu and my box you will need to modify this to your paths.
+
QMake project file:
 
{{CppSourceCode|
 
{{CppSourceCode|
−
description= main.pro|
+
description= h3d_qt4.pro|
 
code=
 
code=
 
<source lang="cpp" line="1">
 
<source lang="cpp" line="1">
−
 
−
######################################################################
 
−
# Automatically generated by qmake (2.01a) Mon Aug 4 18:39:01 2008
 
−
######################################################################
 
−
 
 
TEMPLATE = app
 
TEMPLATE = app
−
TARGET =  
+
TARGET = h3d_qt4
−
DEPENDPATH += . include src
+
DEPENDPATH += .
−
INCLUDEPATH += . include /usr/include/Horde3D1.0.0/Bindings/C++
+
INCLUDEPATH += .
−
LIBS += -L/usr/include/Horde3D1.0.0/Binaries/Linux_x86 -lHorde3D -lHorde3DUtils
+
LIBS += -lHorde3D -lHorde3DUtils
−
 
 
 
QT += opengl
 
QT += opengl
  
 
# Input
 
# Input
−
HEADERS += include/glwidget.h
+
HEADERS += GLWidget.h
−
SOURCES += main.cpp src/glwidget.cpp
+
SOURCES += GLWidget.cpp main.cpp</source>
 +
}}
 +
'''Note''': [http://www.horde3d.org/wiki/index.php5?title=Building_Horde3D#Linkage_order_for_your_application Linkage order for your application]
 +
 
 +
CMake project file:
 +
{{CppSourceCode|
 +
description= CMakeLists.txt|
 +
code=
 +
<source lang="cpp" line="1">
 +
PROJECT(H3D_QT4)
 +
 
 +
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 +
CMAKE_POLICY(VERSION 2.6)
 +
# Debug, all warnings.
 +
ADD_DEFINITIONS(-g -Wall)
 +
# Find Qt4.
 +
INCLUDE(FindQt4)
 +
FIND_PACKAGE(Qt4 REQUIRED)
 +
SET(QT_USE_QTOPENGL TRUE)
 +
INCLUDE(${QT_USE_FILE})
 +
INCLUDE_DIRECTORIES(${QT_INCLUDES}
 +
                    /usr/local/include
 +
                    /usr/include)
 +
# Headers with Q_OBJECT macro.
 +
SET(
 +
    H3D_QT4_MOC_HDR
 +
    GLWidget.h
 +
)
 +
QT4_WRAP_CPP(H3D_QT4_MOC_SRC ${H3D_QT4_MOC_HDR})
 +
# Sources.
 +
SET(
 +
    H3D_QT4_SRC
 +
    GLWidget.cpp
 +
    main.cpp
 +
)
 +
ADD_EXECUTABLE(h3d_qt4 ${H3D_QT4_MOC_SRC} ${H3D_QT4_SRC})
 +
TARGET_LINK_LIBRARIES(h3d_qt4 Horde3D Horde3DUtils ${QT_LIBRARIES})
 
</source>
 
</source>
 
}}
 
}}
−
--[[User:Wild13|Rj]] 05:28, 5 August 2008 (CEST)
 
−
}}
 
−
|  valign="top" | {{Extension_Summary|
 
−
name = Horde With Qt4|
 
−
screenshot = H3DPlaceHolder.png|
 
−
description = This tutorial introduces the use of Horde3D with Qt4|
 
−
version = 1.0|
 
−
horde3dversion = 1.0 beta|
 
−
released = 2008-08-5|
 
−
author = Raynaldo Rivera|
 
 
}}
 
}}
 +
[http://kornerr.alfamoon.com/images/h3d_qt4.png The result should look like this]
 
|}
 
|}
 
[[category: Tutorials]]
 
[[category: Tutorials]]

Latest revision as of 04:59, 10 February 2010

Before you start you should read QtOpenGL tutorial: http://doc.trolltech.com/4.6/opengl-hellogl.html (Substitute correct version of your Qt distribution).

Horde3D library should be already installed for build scripts to succeed.

GLWidget.h
#ifndef GL_WIDGET_H
#define GL_WIDGET_H

#include <QtOpenGL>
#include <horde3d/Horde3D.h>
#include <horde3d/Horde3DUtils.h>

class GLWidget : public QGLWidget {
    Q_OBJECT
    public:
        GLWidget();
        ~GLWidget();

        QSize minimumSizeHint() const {
            return QSize(640, 480);
        }
        QSize sizeHint() const {
            return QSize(640, 480);
        }

    protected:
        void initializeGL();
        void paintGL();
        void resizeGL(int width, int height);

    private:
        H3DRes mCam;
};

#endif // GL_WIDGET_H
GLWidget.cpp
#include "GLWidget.h"
#include <stdexcept>

GLWidget::GLWidget() : QGLWidget() { }

GLWidget::~GLWidget() {
    h3dutDumpMessages();
    h3dRelease();
}

void GLWidget::initializeGL() {
    if (!h3dInit()) {
        h3dutDumpMessages();
        throw std::runtime_error("Could not initialize renderer");
    }
    H3DRes pipeline = h3dAddResource(
        H3DResTypes::Pipeline, "pipelines/forward.pipeline.xml", 0);
    H3DRes knight = h3dAddResource(
        H3DResTypes::SceneGraph , "models/knight/knight.scene.xml", 0);
    h3dutLoadResourcesFromDisk("Content");
    H3DNode node = h3dAddNodes(H3DRootNode, knight);
    h3dSetNodeTransform(node, 0,0,0, 0,0,0, 1,1,1);
    mCam = h3dAddCameraNode(H3DRootNode, "cam", pipeline);
    h3dSetNodeTransform(mCam, 0,40,-40, 23,-166,0, 1,1,1);
}

void GLWidget::paintGL() {
    h3dRender(mCam);
}

void GLWidget::resizeGL(int width, int height) {
    h3dSetupViewport(0, 0, width, height, true);
    h3dSetupCameraView(
        mCam, 45.0f, static_cast<float>(width)/height, 0.1f, 1000.0f);
}
main.cpp
#include "GLWidget.h"
#include <QApplication>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    GLWidget glw;
    glw.show();
    return app.exec();
}

QMake project file:

h3d_qt4.pro
TEMPLATE = app
TARGET = h3d_qt4
DEPENDPATH += .
INCLUDEPATH += .
LIBS += -lHorde3D -lHorde3DUtils
QT += opengl

# Input
HEADERS += GLWidget.h
SOURCES += GLWidget.cpp main.cpp

Note: Linkage order for your application

CMake project file:

CMakeLists.txt
PROJECT(H3D_QT4)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
CMAKE_POLICY(VERSION 2.6)
# Debug, all warnings.
ADD_DEFINITIONS(-g -Wall)
# Find Qt4.
INCLUDE(FindQt4)
FIND_PACKAGE(Qt4 REQUIRED)
SET(QT_USE_QTOPENGL TRUE)
INCLUDE(${QT_USE_FILE})
INCLUDE_DIRECTORIES(${QT_INCLUDES}
                    /usr/local/include
                    /usr/include)
# Headers with Q_OBJECT macro.
SET(
    H3D_QT4_MOC_HDR
    GLWidget.h
)
QT4_WRAP_CPP(H3D_QT4_MOC_SRC ${H3D_QT4_MOC_HDR})
# Sources.
SET(
    H3D_QT4_SRC
    GLWidget.cpp
    main.cpp
)
ADD_EXECUTABLE(h3d_qt4 ${H3D_QT4_MOC_SRC} ${H3D_QT4_SRC})
TARGET_LINK_LIBRARIES(h3d_qt4 Horde3D Horde3DUtils ${QT_LIBRARIES})

The result should look like this