Tutorial - Setup Horde with Qt4

From Horde3D Wiki
Jump to: navigation, search
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