Category Archives: C++

Thread is waiting for init_thread_header and init_thread_wait

The other day I ran into an issue where a thread got stuck on accessing a singleton. It was quite curious because there was no locking with mutexes or critical sections going on, yet the thread stalled. The callstack showed init_thread_header and init_thread_wait as the top most functions of the callstack, so the thread was definitely waiting for something… As it turns out, the constructor of the singleton class was calling some functions, which in turn would try to access the singleton again, this would of course result in an endless loop, depending on which implementation of singletons you use.
If you would instantiate the singleton with new, there would be an endless loop, and the stack would overflow. But if you used a static instance, the thread would simply stall at completion of the first loop, and Visual Studio would not complain, warn, or break.

Here is a simple example of how you can recreate this issue.

class MySingleton
{
public:
	static MySingleton& GetInstance()
	{
		static MySingleton instance;
		return instance;
	}

	MySingleton()
	{
		MySingleton::GetInstance(); // This is obviously bad
	}

	~MySingleton()
	{
	}
};

int main(void)
{
	MySingleton::GetInstance();
}

Of course the code I was working with was not so straightforward. So you would definitely have to look at the entire callstack to find the source of the problem.

Interesting issue, something to look out of in case your code stalls while accessing a singleton. But then again, we probably shouldn’t be calling any functions in constructors anyway… ūüôā

ARM NEON C++ Cheat Sheet

Newer ARM processors have their own flavor of SIMD instructions called NEON. In my little Android application Arashi, NEON is used a lot to speed up the simulation of particles.

Here is a table explaining some of the NEON functions that are used:

C++ NEON functions
NEON Explanation Pseudocode
vdupq_n_f32(a) New NEON value a
vsubq_f32(a, b) Subtract a – b
vaddq_f32(a, b) Add a + b
vmulq_f32(a, b) Multiply a * b
vmlaq_f32(a, b, c) Multiply and add a + (b * c)
vmlsq_f32(a, b, c) Multiply and subtract a – (b * c)
vrsqrteq_f32(a) Reciprocal square root 1 / sqrt(a)
vcgtq_f32(a, b) Compare greater than a > b ? 1 : 0
vcltq_f32(a, b) Compare less than a < b ? 1 : 0
vbslq_f32(mask, a, b) Select by mask mask != 0 ? a : b
vminq_f32(a, b) Get minimum a < b ? a : b
vmaxq_f32(a, b) Get maximum a > b ? a : b

Compiling Unreal Tournament 4 from GitHub source

I am just writing down the steps I take to compile Unreal Tournament 4 from the GitHub source and mention any problems I encounter.

If you plan to use these steps as a guide on how to compile UT4 for yourself, I recommend reading through the steps first! I made some mistakes, so make sure you don’t make the same mistakes ūüôā

Prerequisites

  1. TortoiseGit https://code.google.com/p/tortoisegit/
  2. Visual Studio 2013
  3. A GitHub account associated with an Unreal Engine 4 account

Downloading the source

Use TortoiseGit to download all UT and UE4 sourcecode from https://github.com/EpicGames/UnrealTournament.git

Use right mouse button on a folder and choose “Git Clone…”.

 

TortoiseGit

Log in with your GitHub username and password and it will start downloading.

TortoiseGit2

Download additional necessary binary content

Run [UnrealTournament\Setup.bat] to download necessary additional binary content.

SetupBat

 

SetupBat2

Compiling the Unreal Tournament project

  1. Open up [UnrealTournament\Engine\Source\Programs\UnrealSwarm\UnrealSwarm.sln] with Visual Studio 2013.
  2. Select “Development” as the Solution Configuration.
  3. Compile the whole solution, why not.UnrealSwarm
  4. Then you can close the solution.
  5. Run [UnrealTournament\GenerateProjectFiles.bat] to generate UE4.slnGenerateProjectFiles
  6. Open [UnrealTournament\UE4.sln] with Visual Studio 2013.
  7. Set the Solution Configuration to “Development Editor”.DevEditor
  8. Build “ShaderCompileWorker” and “UnrealLightmass”.ProjectsToCompile
  9. When that’s done, compile the “UnrealTournament” project. This can take quite a while.
  10. After it is done compiling, rightclick the “UnrealTournament” project and select Debug > Start new instance.
  11. The UE4 editor will start.
  12. Press “Launch” to start Unreal Tournament. The first time this can take a long time.LaunchBtn
    1. This took like forever, only to be greeted by this error:Error
      LogPlayLevel: Running AutomationTool...
      LogPlayLevel: RunUAT.bat ERROR: Visual studio and/or AutomationTool.csproj was not found, nor was Engine\Binaries\DotNET\AutomationTool.exe. Can't run the automation tool.
    2. Apparently we also needed to compile this “AutomationTool”!
    3. Close the editor, and compile the “AutomationTool” project under “Programs”.
    4. Once again, start the editor and press “Launch”.
    5. Hurrah! More errors:Error2
      LogPlayLevel: Program.Main: ERROR: AutomationTool terminated with exception:
      LogPlayLevel: Program.Main: ERROR: Exception in AutomationTool: AutomationScripts.Automation.dll was not found or could not be loaded, can't run scripts.
      LogPlayLevel: Stacktrace: at AutomationTool.ScriptCompiler.LoadPreCompiledScriptAssemblies(List`1 OutScriptAssemblies) in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\ScriptCompiler.cs:line 256
      LogPlayLevel: at AutomationTool.ScriptCompiler.FindAndCompileAllScripts(List`1 AdditionalScriptsFolders) in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\ScriptCompiler.cs:line 87
      LogPlayLevel: at AutomationTool.Automation.Process(String[] CommandLine) in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\Automation.cs:line 324
      LogPlayLevel: at AutomationTool.Program.MainProc(Object Param) in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\Program.cs:line 168
      LogPlayLevel: at AutomationTool.InternalUtils.RunSingleInstance(MainProc Main, Object Param) in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\Utils.cs:line 705
      LogPlayLevel: at AutomationTool.Program.Main() in d:\my_projects\UnrealTournament\Engine\Source\Programs\AutomationTool\Program.cs:line 115
      LogPlayLevel: Program.Main: ERROR: AutomationScripts.Automation.dll was not found or could not be loaded, can't run scripts.
    6. Perhaps we also need to compile the “AutomationScripts.Automation” project.
    7. Close the editor, and compile ALL PROJECTS under Programs/Automation, just to be sure.
      Automation
    8. Again, start the editor and press “Launch”.
    9. Now it starts compiling UnrealTournament from within the editor. Once more¬†this will take a while…
    10. After a loooong time, I get this error! ūüôĀError3
    11. I have no idea how to fix this error!
    12. I asked on the forums what the deal is: https://forums.unrealtournament.com/showthread.php?16535-UnrealTournament-exe-incompatible-with-64-bit-windows-(16-Bit-Application)
    13. Apparently you cannot start the game from within the editor (yet).
  13. To start the compiled UT4 game, in the UnrealTournament project, set Command Arguments under Debugging to “unrealtournament -game” and¬†start debugging it!¬†Again,¬†rightclick the “UnrealTournament” project and select Debug > Start new instance.
  14. Hurrahh

References

  1. https://github.com/epicgames/unrealtournament
  2. https://wiki.unrealengine.com/Getting_Started_With_Unreal_Tournament#Building_the_Editor_and_Game
  3. https://code.google.com/p/tortoisegit/
  4. https://forums.unrealtournament.com/showthread.php?16535-UnrealTournament-exe-incompatible-with-64-bit-windows-(16-Bit-Application

 

Open source Skeletal Animation and Software Skinning

tl;dr I wrote a tiny open source skeletal animation and software skinning library for C++: https://github.com/FakeTruth/SkeletalAnimation

Why Skeletal Animations?

Animations can really bring life to projects, that’s why for some projects I am working on I want to have¬†skeletal animation. Looking around on the internet I could not really find an open source solution that did what I wanted. I was looking for something really simple.

ozz-animation

On my Google adventure I came across ozz-animation which I thought would do exactly what I was looking for. A complete package for animating skeletons and software skinning. There was a problem though. This library added tools to convert FBX files to skeletons and animations, and one tool to convert FBX files to a skinned mesh. The mesh however does not contain materials or texture coordinates or any other things you might be interested in, just vertices and normals. The source of the converters also seem really complicated so I was not sure how to add materials and texture coordinates to that. Instead I decided to roll out my own library, which may be useful to other people as well.

My own open source solution

As it turned out, it was really easy to write a library that handles skeletal animation and skinning. Following a simple tutorial that actually included skinning in vertex shaders I was able to write a library that does the skinning on the CPU.

Included in my library is also a converter that converts loaded Assimp scenes to models that can be animated, as well as (de)serialization functions to store and load these models on and from disk.

Why software skinning? Just for the sake of simplicity. I am not looking to animate models with millions of vertices, just simple models which can easily be skinned on the CPU. It is also easier to port to different platforms without worrying about shader languages.

It is really easy to load a model from Assimp:

void LoadModel()
{
    Assimp::Importer Importer;
    const aiScene* pScene = Importer.ReadFile("some_animated_model.fbx",
        aiProcess_LimitBoneWeights |
        aiProcess_Triangulate |
        aiProcess_JoinIdenticalVertices |
        aiProcess_SortByPType);

    AssimpConverter::Convert(pScene, g_AnimatedModel);
}

Then to make the model animate, just call the Update function:

void Update(float a_Dt) // a_Dt is the elapsed time since last frame in seconds
{
  g_AnimatedModel.Update(a_Dt);
}

Rendering the model is very straightforward because you have direct access to the model’s vertices and normals. As an example here is one way how you could render the model with OpenGL.

void Render()
{
    for (unsigned int i = 0; i < g_AnimatedModel.GetNumMeshes(); ++i)
    {
        const SA::sAnimatedMesh& AnimMesh = g_AnimatedModel.GetMesh(i);

        glBegin(GL_TRIANGLES);
        for (unsigned int i = 0; i < AnimMesh.NumIndices; ++i)
        {
            unsigned int Index = AnimMesh.pIndices[i];
            glm::vec3 n = AnimMesh.pNormals[Index];
            glm::vec3 v = AnimMesh.pTransformedVertices[Index];

            glColor4f(n.x, n.y, n.z, 1);
            glVertex3f(v.x, v.y, v.z);
        }
        glEnd();
    }
}

The license I decided to go with is the MIT License, so why not check it out? https://github.com/FakeTruth/SkeletalAnimation

References

  1. https://code.google.com/p/ozz-animation/
  2. http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html
  3. http://assimp.sourceforge.net/

Qt 5.1.1 with OpenGL for Windows 32-bit Visual Studio 2012

In case you would like to use the latest version of Qt using OpenGL instead of ANGLE in combination with Visual Studio 2012 and target a 32-bit platform you will be disappointed to find out that there are no precompiled libraries available from the Qt-Project website…

Instead of using precompiled libraries you need to compile Qt yourself if you still want to use Qt with Visual Studio 2012.

Requirements

In order to compile Qt you first need a couple of prerequisites, be sure to get the 32-bit versions.

  1. Download and install Python http://www.python.org/getit/ (x86).
  2. Download and install Perl http://strawberryperl.com/ (32 bit).
  3. Download jom http://qt-project.org/wiki/jom.
  4. Download and extract the Qt sources from http://qt-project.org/downloads.
    • You need to find the zip for windows, it’s below all the installer downloads.qtdownload
  5. Copy the jom contents to the Qt sources directory.
  6. Make sure python and perl are accessible everywhere from the commandline by testing “python –version” and “perl -v”.qtcheck

Compiling the Qt sources

  1. Open the Visual Studio 2012 command prompt.
    • It should be located in Start >¬†Microsoft Visual Studio 2012 > Visual Studio Tools >¬†VS2012 x86 Native Tools Command Prompt.
    • Be sure you are opening the x86 version and not the x64 version!
  2. Navigate to the Qt sources directory by using “cd <directory here>”.
  3. Type “configure -developer-build -opensource -opengl desktop -nomake examples -nomake tests”
    • It should ask you to accept the terms of the license.
    • Then it should start configuring the Qt sources for compilation, this will just take a minute.
  4. Finally type “jom” to compile Qt.
    • This can take a long time, around 30-60 minutes depending on how fast your computer is. It took me 50 minutes to compile…

Integrating Qt with Visual Studio 2012

After you compiled the Qt libraries you can install the Visual Studio 2012 Add-in for Qt to make life easier. You can get the Add-in from the downloads page under “Other downloads”:¬†http://qt-project.org/downloads.

In Visual Studio 2012 I pointed Qt to the qtbase directory in the Qt sources directory by going in the Qt Options. Qt5 > Qt Options. Add a new Qt version and select the qtbase directory, pick a suitable name and press OK. Use this Qt version in your project and you should be able to compile for 32-bit platforms.

References

  1. http://zulis.blogspot.nl/2013/03/compiling-qt-501-with-visual-studio-2012.html
  2. http://stackoverflow.com/questions/15826893/downloading-and-integrating-qt5-with-visual-studio-2012
  3. http://qt-project.org/downloads

Create something like the Widget Box as in the Qt Designer

Widget Box

Widget Box

Widget Box

There is something called the Widget Box in the Qt Designer. It contains a list of widgets separated by categories. Each category button can be clicked in order to expand and collapse the list below the button.

You might want something like the Widget Box for one reason or another, to display your own collapsible list of items.

Tree Widget

In my approach in recreating the Widget Box I used a Tree Widget because it already has the basic idea of expanding and collapsing items in the list to display and hide its child widgets. The gist of turning the Tree Widget into a Widget Box is adding buttons as top level items and adding a frame with a layout as the child of those top level items. The button should then expand and collapse its own Tree Widget Item when clicked to display and hide the contents of a category. The button will require a custom class inheriting the QPushButton class in order to expand and collapse the Tree Widget Items.

You might want to disable root decoration and set indentation to 0 on the Tree Widget to turn it into a flat list. The custom button type is named QtCategoryButton in this snippet.

MyApplication::MyApplication(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	ui.treeWidget->setRootIsDecorated(false);
	ui.treeWidget->setIndentation(0);

	// First category
	{
		QTreeWidgetItem* pCategory = new QTreeWidgetItem();
		ui.treeWidget->addTopLevelItem(pCategory);
		ui.treeWidget->setItemWidget(pCategory, 0,
			new QtCategoryButton("Category 1", ui.treeWidget, pCategory));

		QFrame* pFrame = new QFrame(ui.treeWidget);
		QBoxLayout* pLayout = new QVBoxLayout(pFrame);
		pLayout->addWidget(new QPushButton("Btn1"));
		pLayout->addWidget(new QPushButton("Btn2"));

		QTreeWidgetItem* pContainer = new QTreeWidgetItem();
		pContainer->setDisabled(true);
		pCategory->addChild(pContainer);
		ui.treeWidget->setItemWidget(pContainer, 0, pFrame);
	}

	// Second category
	{
		QTreeWidgetItem* pCategory = new QTreeWidgetItem();
		ui.treeWidget->addTopLevelItem(pCategory);
		ui.treeWidget->setItemWidget(pCategory, 0,
			new QtCategoryButton("Category 2", ui.treeWidget, pCategory));

		QFrame* pFrame = new QFrame(ui.treeWidget);
		QBoxLayout* pLayout = new QHBoxLayout(pFrame);
		pLayout->addWidget(new QPushButton("Btn1"));
		pLayout->addWidget(new QPushButton("Btn2"));

		QTreeWidgetItem* pContainer = new QTreeWidgetItem();
		pContainer->setDisabled(true);
		pCategory->addChild(pContainer);
		ui.treeWidget->setItemWidget(pContainer, 0, pFrame);
	}
}

Custom button

The custom button is a quite simple class, all it does it catch the pressed() signal and expands or collapses the Tree Widget Item it is bound to.

class QtCategoryButton : public QPushButton
{
	Q_OBJECT
public:
	QtCategoryButton(const QString& a_Text, QTreeWidget* a_pParent,
		QTreeWidgetItem* a_pItem);

private slots:
	void ButtonPressed();

private:
	QTreeWidgetItem* m_pItem;
};

 

QtCategoryButton::QtCategoryButton( const QString& a_Text,
		QTreeWidget* a_pParent, QTreeWidgetItem* a_pItem )
	: QPushButton(a_Text, a_pParent)
	, m_pItem(a_pItem)
{
	connect(this, SIGNAL(pressed()), this, SLOT(ButtonPressed()));
}

void QtCategoryButton::ButtonPressed()
{
	m_pItem->setExpanded( !m_pItem->isExpanded() );
}

Finally

Custom Widget Box

Custom Widget Box

If everything went well you should get something like this. You can of course use any other widgets, not just buttons.