## Rotating a vector by a quaternion in GLSL

Posted in How To, Programming with tags , , , , on 2013/02/11 by Adam Griffiths

I’ve found numerous code samples which produce erroneous results in my shaders.

The functions I’ve found to work are the following:

``` vec4 multQuat(vec4 q1, vec4 q2) { return vec4( q1.w * q2.x + q1.x * q2.w + q1.z * q2.y - q1.y * q2.z, q1.w * q2.y + q1.y * q2.w + q1.x * q2.z - q1.z * q2.x, q1.w * q2.z + q1.z * q2.w + q1.y * q2.x - q1.x * q2.y, q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z ); }```

``` ```

```vec3 rotate_vector( vec4 quat, vec3 vec ) { vec4 qv = multQuat( quat, vec4(vec, 0.0) ); return multQuat( qv, vec4(-quat.x, -quat.y, -quat.z, quat.w) ).xyz; } ```

And this version which is optimised:

``` vec3 rotate_vector( vec4 quat, vec3 vec ) { return vec + 2.0 * cross( cross( vec, quat.xyz ) + quat.w * vec, quat.xyz ); } ```

## Using gl_VertexID without any VBOs.

Posted in How To, Programming with tags , , , on 2013/02/03 by Adam Griffiths

Normally, gl_VertexID is the index of the currently rendered vertex element.

When you provide indices (GL_ELEMENT_ARRAY_BUFFER), gl_VertexID should match the value provided. Ie, the gl_VertexID value can repeat if you render the same element multiple times.

I say “should”, because this is NOT correct when you haven’t provided any VBO data.

This means, that if you are rendering using nothing but a shader and a GL_ELEMENT_ARRAY_BUFFER, the gl_VertexID will NOT match the indices you provide, and will instead be a sequence from 0 -> N.

The solution is to use a single VBO and use glDrawArrays.

Personally, I consider this a bug.

## Adobe Air hanging when using SDK but not when packaged

Posted in How To with tags , , , , , on 2013/01/30 by Adam Griffiths

I just had an issue during the development of an Adobe AIR Application.
When using Flash Pro CS 6, the application hang in certain situations for 5 odd seconds.

I found a few peculiar quirks:

• Packaged builds didn’t have the problem.
• Moving the project to a different directory fixed the problem.
• Clean checkouts of the source didn’t have the problem
• Re-creating the project in Flash Pro CS 6 didn’t fix the problem.

The console was printing the following messages
``` Breakpoint not set; No executable code at line ### ```

It turns out the problem was old breakpoints that don’t exist anymore.
There’s not trivial way to remove all the breakpoints (the button was greyed out!).
The easiest way I found (on OS-X) was to do the following:

``` vi ~/Library/Application\ Support/Adobe/Flash\ CS6/en_US/Configuration/Debugger/AsBreakpoints.xml ```

Remove all the breakpoint entries in the XML.
The file should look similar to this:

``` <?xml version="1.0"?> <flash_breakpoints version="1.0"> </flash_breakpoints> ```

This resolved the issue.

## Compiling Python on Ubuntu / Linux

Posted in Twisted Pair with tags , , , , on 2012/09/25 by Adam Griffiths

I’ve been having problems installing Python on my Ubuntu machine because of errors with setup tools.

During installation you get the following error:

```Installing distribute into /home/ting/.pythonbrew/pythons/Python-2.7.3
ERROR: Failed to install setuptools. See /home/ting/.pythonbrew/build.log to see why.
Skip installation of setuptools.
```

And if you try and use pip / easy_install, you get this:

```ImportError: No module named setuptools
```

It turns out the answer is simple, we’re missing some libraries required to compile it.

```sudo apt-get build-dep python2.7
```

BAM! Pythonbrew works!

## Getting Cocos 1.x / Kobold2D to work with the latest CocosBuilder

Posted in Development, How To, Programming, Rant with tags , , , , , on 2012/03/30 by Adam Griffiths

CocosBuilder is a brilliant tool that helps you rapidly develop Cocos2D applications.

Some of us are stuck with Cocos 1.x for the time being. So let’s figure out how to get things going.

## Install GCC with XCode 4.3

Posted in How To with tags , , , , on 2012/03/21 by Adam Griffiths

XCode 4.3 removed GCC from the default installation. To get it back simply do the following:

1. Run XCode
2. Select XCode -> Preferences -> Downloads -> Command Line Tools -> Install

Voila! GCC is now installed and libraries such as PIL will once again install.

## Installing Kivy on OS-X from PIP and Homebrew

Posted in How To, Programming with tags , , , , , on 2012/03/19 by Adam Griffiths

Begin by following this guide to get your python environment setup.

If you aren’t using virtualenv, then feel free to ignore those commands (mkvirtalenv, cdvirtualenv).

Remember to run ‘brew’ commands as the current user and not root.

Install SDL

```brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi
```

```brew install mercurial
```

Create a virtual environment to work in

```mkvirtualenv kivy
cdvirtualenv
```

Install our Kivy dependencies and then finally, Kivy itself.

```pip install cython
pip install pil
pip install hg+http://bitbucket.org/pygame/pygame</pre>
pip install kivy
```

If you don’t install PIL or PyGame, you will get errors such as this

```(kivy-test)Vibur:kivy-test adamgriffiths\$ python src/main.py
[INFO   ] Kivy v1.1.1
[INFO   ] [Factory     ] 102 symbols loaded
[WARNING] [Image       ] Unable to use <pygame> as loader!
[WARNING] [Image       ] Unable to use <pil> as loader!
[WARNING] [Text        ] Unable to use <pygame> as textprovider
[WARNING] [Text        ] Associated module are missing
[WARNING] [Text        ] Unable to use <pil> as textprovider
[WARNING] [Text        ] Associated module are missing
[CRITICAL] [Text        ] Unable to find any valuable Text provider at all!
Traceback (most recent call last):
File "src/main.py", line 2, in <module>
from kivy.uix.button import Button
File "/Users/adamgriffiths/Workspace/VirtualEnvs/kivy-test/lib/python2.7/site-packages/kivy/uix/button.py", line 38, in <module>
from kivy.uix.label import Label
File "/Users/adamgriffiths/Workspace/VirtualEnvs/kivy-test/lib/python2.7/site-packages/kivy/uix/label.py", line 94, in <module>
from kivy.core.text import Label as CoreLabel
File "/Users/adamgriffiths/Workspace/VirtualEnvs/kivy-test/lib/python2.7/site-packages/kivy/core/text/__init__.py", line 520, in <module>
Label.register('DroidSans',
AttributeError: 'NoneType' object has no attribute 'register'
```