Click here to Skip to main content
Click here to Skip to main content

XML parser of OSG with Qt support

, 24 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Simple XML parser for creation scenes in OSG 3D Engine

Download OSGXML.zip

Introduction

In the project, which had been created by my there was the need of 3D Engine for visualization of scenes with 3D content. As a result, I needed in simple 3D Engine which can be used without problems with copyright and has open source politic for code. I chose Open Scene Graph 3D Engine. It is well known 3D Engine which has wide support of big society and allows to use direct call of commands of OpenGL. However, I fronted with some problems. The biggest of them is using only direct compiling scenes in C++ code. I think, that it is nonsence to make for each scene separate dll or exe files. I decided, that it would be better to make a special XML parser, which can be used for fast building of 3D scenes by simple semantics.

Background

In my project I used Qt framework 4.8.4. There were two main reasons for using of Qt.

Firstly, I needed a simple way for controlling of animation and possibilities for controlling of code. Qt allows to use C++ classes in scripts. Core QScript supports to call functions of C++ classes and creates easy way for controlling of states of OSG scenes.

The second reason is that OSG does not have audio system. I compilled OSG version 3.1.3 with ffmpeg, but it allows to play only video stream without audio support. After some research, I decided to use QMultimedia for playing of audio surround of video files. This way is compatible with wide computer platforms.

I made soft which allowed to create 3D scenes with simple way.

Using the code

This XML parser contains 27 classes, and it is useless to present all of them. Original code for Qt can be downloaded from Download OSGXML.zip. I want to present only XML structure for discribing of OSG Scenes. It includes next nodes:

Group - for grouping of elements of Scene.

MatrixTransform - for grouping of elements of Scene with appling of matrix transformation for group.

Switch - for grouping of elements of Scene with appling of control of showing and hiding of elements.

Model - for downloading of 3d models. I prefer to use .fbx format. It can support animation of model.

ImageSurface - for visualization of images in 3D space.

Simple XML structure is shown in next listing

<?xml version="1.0" encoding="utf-8"?>
<OSGStructure xmlns="">
  <OSGStructure.Scripts>
  <Script Path="Script1.js"/> 
 </OSGStructure.Scripts>  
    <Group>
    <MatrixTransform >
 <MatrixTransform.SetMatrix>
 
  <Rotate Parametr="{ Angle=90, Axis=X, Option=Quat}"/>
 
  <Translate X="0.0" Y="0.0" Z="0.0001"/>
 
 </MatrixTransform.SetMatrix>
 
 <Model Name="Four" Path="raymen/raymen.fbx" Scale="0.05">
  <Model.Animation>
   <AnimationManager Name="Four_Animation" PlayMode="ONCE"/>
  </Model.Animation>
 
  <Model.SetTriggers>
 
   <KeyTrigger Name="Start_Four_Animation" Key="KEY_Space" />
 
  </Model.SetTriggers>
 
  <Model.SetResources>
 
   <AudioPlayer Name="Character" Path="Character.wav" />
 
  </Model.SetResources>
 </Model>
    </MatrixTransform>
    </Group>
</OSGStructure>

In this XML code MatrixTransform.SetMatrix is node for setting Matrix by Rotate, Transform and Scale. Model.Animation allows to set AnimationManager for parsing of original model and controlling of animation. Model.SetTriggers allows to set Triggers for using of such events as pressing of Key on Keyboard or moving of mouse. At present, I have created only KeyTrigger, but it can be expanded on mouse or touch (for Android). Model.SetResources is created for containing of non-visual elements such as AudioPlayer. OSGStructure.Scripts contains Script. Scripts are used fow whole scene. For each scene can be used many scripts. Of course, these scripts are executed slow for realtime game, but they are simple and for visualization of scenes it is suitable. Scripts can have the next listing

function StartAnimation()
{
    Four.Four_Animation.playAnimation();
}
 
function StartAudio()
{
    Four.Character.stop();
 
    Four.Character.play();
 
}
 
Four.Start_Four_Animation.keyDown.connect(StartAnimation);
 
Four.Start_Four_Animation.keyDown.connect(StartAudio);

In this script Four is named element of OSG Scene. It has child trigger Start_Four_Animation which is emit signal keyDown. This signal is bound with two functions. In these functions Four.Four_Animation.playAnimation() and Four.Character.play() are executed and they start animation and playing of audio track.

The next listing shows using of ImageSurface and binding of textures.

<ImageSurface Visible="false" Name="Video1" Stretch="{Type=Uniform, TargetName=VideoPlayer}">
 
 <ImageSurface.Textures>
      <Texture2D  Path="1.tga" />
        </ImageSurface.Textures>
 
</ImageSurface>

This code can be modified for using of video file by VideoTexture

 <ImageSurface.Textures>
      <VideoTexture Name="VideoPlayer" Path="Making.avi" />
 </ImageSurface.Textures>
 
 <ImageSurface.SetTriggers>
 
  <KeyTrigger Name="Play_Pause" Key="KEY_P" />
 
  <KeyTrigger Name="Stop" Key="KEY_R" />
 
 </ImageSurface.SetTriggers>

This code support multitexturing and it can be presented as

 <ImageSurface.Textures>
      <Texture2D  Path="1.tga" />
 
 <Texture2D Path="2.tga" />
 
 </ImageSurface.Textures>
 

 <ImageSurface.Textures>
      <Texture2D  Path="1.tga" />
     <VideoTexture Name="VideoPlayer" Path="Making.avi" />
 
 </ImageSurface.Textures>
 

 <ImageSurface.Textures>
      <VideoTexture Name="VideoPlayer1" Path="Making.avi" />
 <VideoTexture Name="VideoPlayer2" Path="01.mp4" />
 
 </ImageSurface.Textures>

This way for appling of textures can be used with models and these textures can be applied for specific mesh of model or scene. For this attribute TargetMesh is used for targeting of specific mesh. This way can be used for appling of VideoTexture to mesh.

<pre lang="C++"><Model Name="Four" Path="raymen/raymen.fbx" Scale="0.05">
 <Model.Animation>
  <AnimationManager Name="Four_Animation" PlayMode="ONCE"/>
 </Model.Animation>
 
 <Model.SetTriggers>
 
  <KeyTrigger Name="Start_Four_Animation" Key="KEY_Space" />
 
 </Model.SetTriggers>
 
 <Model.SetResources>
 
  <AudioPlayer Name="Character" Path="Character.wav" />
 </Model.SetResources>
 
 <Model.Textures>
  <VideoTexture TargetMesh="Mesh_Rayman3_Rayman_3" Name="VideoPlayer" Path="Making.avi" /> 
 </Model.Textures>
 
</Model>

Showing and hiding elements in Switch are doing by appling of attribute Visible="false" or calling function setStateNode in scripts

Examples of XML files and preview of them can be downloaded by this link OSGXML-Examples

Points of Interest

This project was developed for Augmented Reality product, but some solving can be used separatly. For instance, playing video from ffmpeg or playing Animation of models. Posibilities of this XML parser are very limited, but I hope that it can help someone on the way of using of OSG. Demo version of Augmented Reality can be downloaded by this link OSGAR

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Evgeny Pereguda
Software Developer
Australia Australia
No Biography provided

Comments and Discussions

 
Questionnice artcle in Qt PinmemberArun Kumar K S26-Dec-12 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 24 Dec 2012
Article Copyright 2012 by Evgeny Pereguda
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid