Friday, November 19, 2010

Faster and More Responsive Desktop in Linux

Faster Linux Desktop - Changing the scheduling paradigm to increase responsiveness for desktop useage

Came across an interesting article about speeding up the desktop usage in linux by altering the threading and process scheduling algorithm.

More details can be found here :

My question is would this change performance for servers that have no graphical user interface running IE xorg? It looks like it won't!

Postgres unable to start with shmget. SHMMAX too small

Error Starting PostgreSQL, Could not create shared memory segment errors

If you have a large PostgreSQL configuration that supports many concurrent requests, you may find yourself starting it as you have increased the shared_buffers parameter or max_connections parameter, and it fails to start.

The error message will say something like could not create shared memory segment, Failed system call was shmget. This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. SHMMAX too small.

Here is the exact error message I got:

debian-testing:~# /etc/init.d/postgresql-8.3 start
Starting PostgreSQL 8.3 database server: mainThe PostgreSQL server failed to start. Please check the log output: 2010-11-17 13:53:34 EST FATAL: could not create shared memory segment: Invalid argument 2010-11-17 13:53:34 EST DETAIL: Failed system call was shmget(key=5432001, size=147382272, 03600). 2010-11-17 13:53:34 EST HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce the request size (currently 147382272 bytes), reduce PostgreSQL's shared_buffers parameter (currently 16384) and/or its max_connections parameter (currently 503). If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for. The PostgreSQL documentation contains more information about shared memory configuration. failed!

PostgreSQL Debian increase SHMMAX

In order to increase the maximum amount of shared memory a linux program can access, we need to adjust kernel parameters. Since we are using Debian they are available in /etc/sysctl.conf. This should be similar for many linux distributions.

We simple edit this file:

sudo vim /etc/sysctl.conf

or if you have GNOME running

sudo gedit /etc/sysctl.conf

And either search for the line or add the line (most likely will have to add) :

kernel.shmmax = 147382272

Note the number here is the same as "size" in this system call, shown in my error message above: shmget(key=5432001, size=147382272, 03600)

Now we have to put these settings into effect by running the command

sysctl -p

And now we start PostgreSQL up without the error:

debian-testing:~# /etc/init.d/postgresql-8.3 start
Starting PostgreSQL 8.3 database server: main.

Thursday, November 18, 2010

Intel OpenCV Open Source Computer Vision Software

Face Tracking, Motion Tracking, OpenCV - Free Computer Vision Library

Was looking into how to do face tracking and possibly motion tracking. With face tracking I was hoping I could find a way to track facial expressions, ie the eyebrow position, mouth position, as is done with many CG characters nowadays. OpenCV is an open library by Intel that allows you to carry out various computer vision tasks. Facial tracking is one of the demos that comes with OpenCV.

How OpenCV's facial tracking works can be described in great detail

You can find out more about how OpenCV's facial tracking works here :

Check out Intel's OpenCV library - computer vision

Check out the a wiki for more detailed information here :

Face Tracking OpenCV - Cool demo on youtube

Tuesday, November 16, 2010

Mozilla's HTML 5 Audio API Draft

HTML 5 - Mozilla's Draft for the Audio API

Defining an Enhanced API for Audio (Draft Recommendation) by Mozilla

Was looking into writing a html 5 script that can generate tones given a frequency (ie 440 hz for 'A') . Alas, it seems out of my reach for now as Mozillas Audio Writing API does not appear to work in my version of firefox. Cmon people lets get moving!

Cool Examples

Toy Piano
Javascript text-to-speech engine

Let me know if any of these work for you, because they don' work for me :(

Monday, November 8, 2010

Site Launch

New site has been launched. Lets watch the SEO value... lots of competition out there

New! Speed of Flow tabs by The Rodeo Carburettor

Monday, September 13, 2010

Load Balancing Web Servers using ldirectord

Web Server Load Balancing


Using ldirectord to set up load balancing in apache, using debian linux as my distro, should also work on ubuntu or any other distro for that matter (bsd, red hat, etc). See below for links to the documentation. This setup is done using ldirectord's masq mode, which is basically ip masquerading function using iptables.

Let's Begin

Im just going to take a few mins to explain my load balancing set up. What we have is 1 router, the default gateway, connected to the internet, and also connecting to an internal network. Standard set up really.

What we want to do is set up load balancing with 2 (or more, our case we have 4) apache servers.

First step - download ldirectord (ldirectord) is a daemon that will route traffic to a certain host based on whatever load balancing algorithm you choose. Some examples of algorithms are round robin, where it selects the next host every time, or least connections, route to the host with the least number of open connections. There are a few other options that are covered in the ldirectord documentation (man page) and specifically for the algorithms is in the IPVSADM man page under -s (scheduler) located here (

Note that ldirectord is now a sub project of linux HA project called Heartbeat. It is available as a part of that package available here

The reason for this is ldirectord is used as a part of that package. It is also useable on its own as I will show you.

Ldirectord set up

What we have to do next is set up ldirectord after we manage to get it compiled. Here is an example of a /etc/ha.d/

# Global Directives

# Virtual Server for HTTP
real= masq
real= masq
real= masq
real= masq
receive="Test Page"

As you can see, we have set this ldirectord program up on the host located at which is a physical machine. Using Heartbeat you can set up a virtual IP address and have 2 or more machines take over that address when the main one goes down, but that is for another article. We are strictly focusing on setting up ldirectord here.

Setting up the load balanced machines

On each of the load balanced machines (, 22, 23, and 12) we need to set the default gateway to (the ip address of the load balancer) Please adjust accordingly to your network. This can be done using the following command on debian linux and most other types

route add default gw

Next we have set up our ldirectord to check each machine by loading the page "ldirectord.html" shown above. So really it will be trying to access the page We have set up a regular expression as well above saying "Test Page". As long as this url returns this string somewhere, the host will be considered "up" by ldirectord. After we have set everything up, it should be good to go.

Running ldirectord

Please make sure your config file is in /etc/ha.d/ because the script will be searching there for it. Next run ldirectord by typing

/usr/bin/perl /usr/sbin/ldirectord start

You should make a init.d script for this. After you have run ldirectord, typing

ipvsadm -L

should give you some information on its status.

s5:/etc/ha.d# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP wlc
-> Masq 1 6 2245
-> Masq 1 5 2275
-> Masq 1 4 2563
-> Masq 1 6 2074

Here it shows that all hosts are up (their "Weight" is each 1) and have active connections running to them.

Setting up external routing

Now just have your router point all web traffic to and you should have full load balancing. Note there are other problems such as how are you going to make sure they are all synchronized. You can use tools such as rsync, or use NFS to mount a share that contains all your files on each of the slave servers (our approach). The NFS approach seems to work nicely, however if you are hosting webservers for file transfers it may become a bottleneck so make sure your internet network is fast (1000 mbit) and tweaking NFS settings to your liking.

Thank you, your comments are welcome!

Parsing Wavefront .obj using Python

Wavefront OBJ File Format Parsing in Python

I thought I would share some simple parsing information about the wavefront .OBJ file format using python. The thing I like about this format is that it is stored in plain text, and easy to use if you are writing simple 3D game engines, or just 3D modeling programs.

You can use this parser to load wavefront files using python, and possibly to view the wavefront obj file.

Overview of the wavefront .OBJ file format

Based on

Basically, our approach is to go line by line through the file. If the line starts with a "v", we are dealing with a vertex. If the line starts with a "vt" then we are dealing with a texture coordinate (u, v, optionally w). "n" means normal. "f" means its a face index. These are a bit special, but not too difficult to grasp. Our exported models that are from blender will all have normal vectors, and texture coordinates (make sure you specify the texture coordinates in blender or there will be none). The "f" lines will look like this:

f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3

where "v1" is the vertex array index, "vt1" is the texture coordinate index, and "vn1" is the normals array index. This particular face is a triangle. I recommend storing triangles, and quads in 2 different arrays. They can both reference your vertex/texture coordinate/normals array.

Very important note before we begin

The index format in the .OBJ Wavefront file format is 1 based, not 0 based. Thus, we should subtract 1 from the actual number in order to get a 0 based index, and make it compatible with python lists or any array type.

#do the loading of the obj file
def load_obj(filename) :
V = [] #vertex
T = [] #texcoords
N = [] #normals
F = [] #face indexies

fh = open(filename)
for line in fh :
if line[0] == '#' : continue

line = line.strip().split(' ')
if line[0] == 'v' : #vertex
elif line[0] == 'vt' : #tex-coord
elif line[0] == 'vn' : #normal vector
elif line[0] == 'f' : #face
face = line[1:]
if len(face) != 4 :
print line
#raise Exception('not a quad!')
for i in range(0, len(face)) :
face[i] = face[i].split('/')
# OBJ indexies are 1 based not 0 based hence the -1
# convert indexies to integer
for j in range(0, len(face[i])) : face[i][j] = int(face[i][j]) - 1

return V, T, N, F

Please not this program will give you string representations. You should loop through these arrays again and convert them to the proper format (int, float, etc).

Please leave your comments

Thursday, August 26, 2010

Compiling boost C++ development libraries in OSX

Compiling boost c++ libraries and development headers for Mac OSX

Hi Everyone,

So I am starting to play with OpenCL. And as you all know I do love python. I have been playing with pyOpenCL package for awhile and wanted to get it set up on my Mac using OSX.

It depends on an installation of Boost C++ Libraries

first we set up a directory, and in a terminal we need to download the boost files:


bzip2 -d boost_1_44_0.tar.bz2

tar -xvf boost_1_44_0.tar

cd boost_1_44_0

Configuration and Compilation

Great now we have extracted it, lets attempt to configure, compile, and install this package. Since I have build many applications on my MacOSX, I already have lots of build tools/libraries installed (eg. gcc, xorg, wine).

I was able to compile by typing at the console:



And then I waited... Its a shame there is no configure and make so I could use "make -j 4" and utilize all 4 of my cores for compiling. Maybe bjam has that option, I am not sure and too lazy to look into the documentation. Also I believe that is a bit out of the scope of this post for now.

After waiting for it to finish, we install the libraries by typing

sudo ./bjam install

And then we wait some more...

Finally after it is installed we can continue to try and install pyOpenCL.

Note: openCL unfortunately is not supported on my mac (Mac OS X 10.5.8 Leopard) and no amount of wishing will make it so :( However hopefully you are able to install this package (pyOpenCL) using Linux or Windows, or even Snow Leopard. Good luck!

Thursday, July 29, 2010

Using Canvas in Firefox and Safari for Graphics in your Browser

Using the Canvas Tag

About the Canvas Tag

The canvas tag is a 2d "canvas" that can be used to draw various vector shapes. It has a fully integrated javascript API. It somewhat resembles the graphical aspects of flash. Since adobe and flash have taken some flack lately, we begin to wonder what is the future of web graphics programming APIs? Apple seems to be in a space where they want to drop support for flash. (See iPad discussions). A lot of people are behind this movement. Another example is android, which does support flash, however many people are reporting that performance is bad.

Why Not Use the Canvas Tag?

Well, we can't replace flash with canvas. Flash has many other aspects such as full networking libraries, native video playback, audio, etc. Some of these things are coming along as separate modules for each browser but since there is no unified front on these modules, it seems to be lagging behind in terms of features, and even being available in the first place.

Another problem is we are not seeing much support for canvas using internet explorer (one of the most widely used browsers at the moment). Firefox, Safari, Chrome and Opera all fully support it.

Personally I would like to see canvas more widely used. And I think market forces may give it a bit of a push in the near future.

Demonstration of using the canvas tag. Works in Firefox and Safari

Hi Everyone,

Today I am going to demonstrate the use of the canvas tag in order to output graphics. Here is our template we will use in order to get started. This is a short script that will draw a circle at coordinates 200, 200 with a radius of 50. Below that is this code at work. It should be drawing a large blue circle if its working properly.

<title>Canvas Test</title>

<script type="application/javascript">
var ctx = null;

function draw() {
//get our canvas element
var canvas = document.getElementById("canvas");
//create our context
ctx = canvas.getContext("2d");
//set out fill color, red = 0, green = 0, blue = 200, alpha = 0.5
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
//begin our arc
//draw an arc, starting at 200, 200. 50 radius, angle starts at 0, and continues to 360 degrees (a cicle), and uses counter clockwise (false)
ctx.arc(200, 200, 50, 0, (Math.PI/180)*360, false);
//end our path
//fill in the result



<body onload="draw()">
body {
margin: 0px;
padding: 0px;
canvas {
border: solid 1px;
<canvas width="800" height="600" id="canvas"></canvas>

Here is the example at work:

Please leave your comments below!

Wednesday, June 23, 2010

Lambda functions in Python

Using lambda functions in python. Defining a lambda function

Lambda functions in python can be somewhat confusing. Here is a brief introduction

Lambda functions are handy functions that can be defined in 1 line, and thrown away afterwards. Here is a small example of what the syntax for a normal function looks like, and what the syntax for a lambda function looks like:

def f(x): return x * x

And here is the same function using the lambda syntax:

f = lambda x : x * x

Lambda functions really come in handy when used with python functions such as filter(), map() and reduce(). Here are some examples.

First we have filter. It will return a copy of a list where the function you pass returns True. This example will return only even numbers (because any number mod 2 will be equal to 0 if it is even).

array = [1, 2, 3, 4, 5, 6] #create an array of integers
print filter(lambda x: x % 2 == 0, array)


[2, 4, 6] # a list containing all even numbers

Next we will show map. It is basically a mapping of one list to another using a function (or a lambda function):

array = [1, 2, 3, 4, 5, 6] #create an array of integers
print map(lambda x: x * x, array)


[1, 4, 9, 16, 25, 36] # a list containing squares of 1, 2, 3, etc

And finally we have reduce. What this will do is convert the list into a single value. Our function will add element 0 and element 1. That result is then added to element 3, that result is then added to element 4, etc. Eg. (((((1+2) + 3) + 4) + 5) + 6) = 21

array = [1, 2, 3, 4, 5, 6] #create an array of integers
print reduce(lambda x, y: x + y, array)



And that concludes our short tutorial on creating and using lambda functions

Python: Defining a function.

Defining a function in python

Python function definitions are simple. Defining a function in python is very similar to many other languages out there. Lets try it out:

def hello(x) :
print "Hello", x


Output is as follows

Hello Bill

Notice we used print with a comma. This allows concatenation of strings in a single line statement.

An interesting property of functions in python is that you are able to assign them to variables.

variable1 = hello # assigns the variable "variable1" to the function we created above "hello"

Will output

Hello Sarah

That is all for now. Thanks and please add comments.

Monday, June 21, 2010

Technology Behind Facebook OSS Open Source

Facebook and OSS server software and hardware setup

The software and hardware behind facebook

Love it or hate it, facebook has some great, open source software behind it. It has also contributed to the open source movement quite a bit. Here is an article I found interesting.

Monday, June 14, 2010

Facebook Chat in Empathy Ubuntu Linux

Using Facebook Chat in Ubuntu on Empathy

Facebook Chat Server Settings:

To set up the configuration:

Click edit->accounts, then click the Add button.

  • Account Type: Jabber

  • Reuse existing account

  • Create

  • Login ID: <username> (Note you must create a facebook username if you have not already)

  • Under Advanced - Server:

Then you are finished. Make sure SSL is disabled (which should be the default). Please leave some comments! Thanks!

Tuesday, April 20, 2010

Javascript foreach equivalent

Javascript foreach equivalent

Javascript has no foreach statement. Instead, we use the following syntax.

var letters = ["a","b","c"];

for(var i in letter) {

Note the variable i, is an index into the array, and not a reference to the array element.

Javascript FOR loop

A for loop in Javascript

A for loop in java or javascript is similar to a Java or C for loop

for(i = 0; i < 3; i++){

the variable i is the counter variable. It starts at 0. This loop runs, which will alert 0. Next it increments i, then returns to the start.

Javascript HTML tags

Javascript HTML

In order to use javascript in a html document, you need a HTML file. A typical HTML file looks like this

This is a HTML document


You can either put your script in the html document like so

alert('Hello World');
This is a HTML document


or in an external file names script.js

<script language="JavaScript" src="script.js"></script>
This is a HTML document

Thursday, April 15, 2010

64 Bit OS for Intel

Found an interesting site about a new 64 bit operating system that is purely 64 bit. Seems to be an interesting project

Sadly there is a blurb about the GPU and how it won't be supported. This of course makes me saddest of all! Interesting 3D graphics tho in the video on the site. Wonder if the rasterization code was written from scratch?

Although I wont be loading this onto all of my 64 bit servers anytime soon, it does look like an interesting beginning.

Wednesday, April 14, 2010

Depth texturing in OpenGL GLSL Shader Program

Depth Texturing in GLSL

This GLSL shader program is used to enable depth texturing. The depth texture is located under texture unit 5 as shown:

uniform sampler2DShadow texture_5;

Note we had to pass the light's translation matrix in gl_TextureMatrix element 0 as shown:

ShadowCoord = gl_TextureMatrix[0] * gl_Vertex;

Here are the shaders

Vertex Program

varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec4 ShadowCoord;

void main()
ShadowCoord = gl_TextureMatrix[0] * gl_Vertex;

/* first transform the normal into eye space and
normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that
according to the OpenGL specification, the light
is stored in eye space. Also since we're talking about
a directional light, the position field is actually direction */
lightDir = normalize(vec3(gl_LightSource[0].position));

/* Normalize the halfVector to pass it to the fragment shader */
halfVector = normalize(gl_LightSource[0];

/* Compute the diffuse, ambient and globalAmbient terms */
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Fragment Program

varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec4 ShadowCoord;
uniform sampler2D texture_0;
uniform sampler2DShadow texture_5;

void main()
vec3 n,halfV;
float NdotL,NdotHV;
diffuse = texture2D(texture_0, gl_TexCoord[0].xy);
ambient = vec4(0.0, 0.0, 0.0, 0.0);

vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w;
shadowCoordinateWdivide.z += 0.0001;
float distanceFromLight = shadow2DProj(texture_5, shadowCoordinateWdivide).a;

vec4 tc = shadowCoordinateWdivide;
MultiSample depth texture
float s = 0.00005;
//float s = 1.0 / 10.0;
//float d = distanceFromLight;
float d1 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g+s, tc.b, tc.a)).a;
float d2 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g-s, tc.b, tc.a)).a;
float d3 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g+s, tc.b, tc.a)).a;
float d4 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g-s, tc.b, tc.a)).a;
float d5 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g, tc.b, tc.a)).a;
float d6 = shadow2DProj(texture_5, vec4(tc.r+s, tc.g, tc.b, tc.a)).a;
float d7 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g, tc.b, tc.a)).a;
float d8 = shadow2DProj(texture_5, vec4(tc.r-s, tc.g, tc.b, tc.a)).a;*/

/* The ambient term will always be present */
vec4 color = ambient;

/* a fragment shader can't write a varying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

/* compute the dot product between normal and ldir */
NdotL = max(dot(n,lightDir),0.0);

if (NdotL > 0.0) {
color += diffuse * NdotL;
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += gl_FrontMaterial.specular *
gl_LightSource[0].specular *
pow(NdotHV, gl_FrontMaterial.shininess);

gl_FragColor = color;
gl_FragColor.a = diffuse.a;

float shadow = 1.0;
if(ShadowCoord.w > 0.0) {
shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.2 : 1.0 ;

gl_FragColor.rgb = shadow * gl_FragColor.rgb;


Sunday, April 11, 2010

Fable the last chapters under wine

Hi Everyone,

So I am attempting to set up fable using wine v 1.1.37. During the install, I get the error message "Error loading the PID generator DLL. The DLL could not be found!".

To fix, get the winetricks script.


Then, we need to run the script to install MFC42 package.

winetricks vcrun6

And voila, the installer runs no problem

Saturday, April 10, 2010

Oblivion in Wine : Bypass Securom

Installing Oblivion under linux wine Ubuntu

Attempting to install oblivion in linux to run under wine causes some problems for me. The problem is securom, or saferom. When you attempt to install oblivion under wine (I am using Ubuntu) the installer fails. We need to find a method to bypass securom so we can use wine to play the game.

Since the install files are ultimately just tucked away inside some cab files, installation without using the actual installer is possible. All you have to do is use a tool called "Unsheild".

Users who are using playonlinux should still be able to use this guide, and install the game. Later use playonlinux to actually run the game.

What you will need to set up Oblivion

You will only need an open terminal and an ISO version of the disk. You could try this method with the DVD itself in the drive.

Mounting the ISO

I had to try this method in my attempt to install oblivion from some ISOs I had saved on my hard drive. What I ended up doing was mounting the ISO to my cdrom mount point:

sudo mount -o loop /path/to/oblivion.iso /media/cdrom

replacing /path/to/oblivion.iso with the path to the actual ISO file.

Setting up Unsheild

Then you need to install "Unsheild"

sudo apt-get install unsheild

Installing Oblivion

Then you run the command to "install" the game

unshield -d /path/to/install/to x /media/cdrom/

replacing /path/to/install/to with the path you wish to install the game. This will extract all the cab contents to the specified folder.

Running the Game

Next we run the game

wine /path/to/install/to/Oblivion/OblivionLauncher.exe


Wednesday, April 7, 2010

PHP's case sensitivity in function names

Hi Everyone,

Just thought I'd toss out a random tidbit. I've found a program that tried to define a function name that conflicted with a global function name, but not like you would expect.

They used camel case for the function name, when the system defined function didn't. Unfortunately function names in PHP are NOT case sensitive, as shown in the following experiment:


function test_func() {
echo 'test_func';

function Test_Func() {
echo 'Test_Func';

Running this script gives:

bill-hamiltons-imac:~ bill$ php test.php

Fatal error: Cannot redeclare test_func() (previously declared in /Users/bill/test.php:4) in /Users/bill/test.php on line 9

So there you have it. PHP function names are not case sensitive which some of you may already know but this is obviously the proof.


Thursday, April 1, 2010

Programming Games in Python

Programming Linux Games - Prototyping in Python

Hi Everyone,

Its been awhile since I've posted on topics related to game programming. Basically I am here to talk about programming games in Python programming language. I have programmed in many languages including C and C++ and even developed graphics engines in these languages. Eventually I get to a point where the code is large and basically I sit pondering where to go next in order to add features, and also get lost in semantics of the languages and how I will accomplish these goals.

Next I started trying out python, in order to prototype new features. I have written rather large programs in python before, and the one thing I got out of it is that its very fast for writing programs, and also very type safe. So if you try to compare and int with a float, it will raise an exception. This sounds like a headache, but really its a dream for debugging later on!

Some problems you may be thinking is 1) Python is slow. Right you are, python is rather slow compared to languages like C++ and C, but only in specific cases. For example, iterating through arrays can be much faster in C and C++. Memory usage is also less because you are working with more primitive data types. Also python does constant checking of variable types, etc.

When it comes to games, I feel that today's processors are very fast. Not only that, but many of the heaviest tasks are getting put onto the GPU, which has limited choice in programming languages, and they are mostly low level. Tasks like physics, rasterization are now completely done on the GPU, and now the trend seems to be even using the GPU for general processing tasks (GPGPU).

Back to the point, python's speed limitations no longer apply considering we are pushing all the data to the GPU and allowing it to do all the work with its languages. There are some things left to the CPU, like event handling, playing sounds, and some basic math (debatable as to perform on GPU or CPU) which I think my quad core can handle quite easily thank you!

Further more, threading in python is very easy however you still need to be very careful as to how you set up variable access etc.

As I will show in future blog posts, using python for game development should become a reality because its so type safe, it allows you to program things quickly (functionally) and then allows you to push all the data to the GPU very easily. Currently I am working on a GPGPU processing program written in python, and it should come in under 1000 lines of code!

Wow that is a large starbuck's coffee

Just found a link to starbucks doing some market research. Im glad they are listening to their customers but I think this size "Plenta" is a little over the top. I mean seriously you should not need that much coffee, what is that an entire pot?!? Check out the link. This is real folks!!

Happy april fools I guess haha!

Wednesday, March 3, 2010

Using GLSL programs in python pyopengl

Here is an easy straightforward example of using GLSL shader programs with pyopengl.

To use this you will need to import the following:

from OpenGL.GL import *
from OpenGL.GLU import *

Thus we will need pyopengl package installed.

##compile and link shader
vs = fs = 0

vs = glCreateShader(GL_VERTEX_SHADER)
fs = glCreateShader(GL_FRAGMENT_SHADER)

vs_source = """
void main(void) {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fs_source = """
void main (void) {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
glShaderSource(vs, vs_source)
glShaderSource(fs, fs_source)

log = glGetShaderInfoLog(vs)
if log: print 'Vertex Shader: ', log

log = glGetShaderInfoLog(fs)
if log: print 'Fragment Shader: ', log

prog = glCreateProgram()
glAttachShader(prog, vs)
glAttachShader(prog, fs)


glShaderSource(shader_id, text) is a handy python function that will load our shader for us into opengl.

I also have a handy shader class I use in my engine program. Here it is:

class shader(node) :
def __init__(self, filename):
fh = open(filename)
self.source = {'vertex': '', 'fragment':''}
write = None
for line in fh :
if line == '[[vertex-program]]\n' :
write = 'vertex'
elif line == '[[fragment-program]]\n' :
write = 'fragment'
else :
self.source[write] += line

self.draw = self.init

def init(self):
##compile and link shader
self.vs = self.fs = 0

self.vs = glCreateShader(GL_VERTEX_SHADER)
self.fs = glCreateShader(GL_FRAGMENT_SHADER)

glShaderSource(self.vs, self.source['vertex'])
glShaderSource(self.fs, self.source['fragment'])

log = glGetShaderInfoLog(self.vs)
if log: print 'Vertex Shader: ', log

log = glGetShaderInfoLog(self.fs)
if log: print 'Fragment Shader: ', log

self.prog = glCreateProgram()
glAttachShader(self.prog, self.vs)
glAttachShader(self.prog, self.fs)

self.draw = self.use

def use(self):

def end(self):

Wavefront Obj file format, opengl vertex arrays format, and uv texture coords

Loading Wavefront files using Python

Loading .obj files into numpy arrays, then using OpenGL to draw them

Note: for information on parsing wavefront .obj format click here

Our task here is to load wavefront .obj files into numpy arrays and use them in OpenGL, or pyOpenGL of course.

I am here to discuss something that has always bothered me in openGL. The fact that the vertex arrays and glDrawElements uses only 1 pointer to draw the elements, when in reality, it should use 2 (at least). This could be solved by writing special GLSL vertex shaders and using vertex textures, but that is overly complicated.

Understanding the problem:

The problem with indexing a vertex is the texture coordinate. If you are going to have UV islands, you will see that each vertex at that point, will have more than 1 texture coordinate. This means that there is no 1:1 mapping between a vertex, and a texture coordinate, and this breaks the 1 array indexing multiple verticies in an array, and also breaks the glDrawElements method.

The solution?

We basically unravel the arrays, reusing as many vertex:uv mappings as we can, but create new indexes when they differ.

Visual Examples:
v1 v2 v3
--- ---
| | | - 2 quads connected
--- ---
v4 v5 v6

v1 v2 v2 v3
--- ---
| | | | - UV coordinates NOT connected
--- ---
v4 v5 v5 v6

In this example may you see that v2 and v5 don't share UV coordinates, so they will need to have 4 indexies into our index array (because the index array indexes both UV coordinates and Vertex coordinates)

How will we take care of this?

What we are going to do is write a python parser for OBJ files, parse it into the traditional array structure that is laid out in the file. Then we are going to unravel it and create secondary arrays that will work with the glDrawElements.


from numpy import *
import random

#util to unravel
indexies = dict()
counter = -1
def get_index(key) :
global indexies, counter
if key not in indexies :
counter += 1
indexies[key] = counter
return [False, counter]
else :
return [True, indexies[key]]

#do the loading of the obj file
def load_obj(filename) :
V = [] #vertex
T = [] #texcoords
N = [] #normals
F = [] #face indexies

fh = open(filename)
for line in fh :
if line[0] == '#' : continue

line = line.strip().split(' ')
if line[0] == 'v' : #vertex
elif line[0] == 'vt' : #tex-coord
elif line[0] == 'vn' : #normal vector
elif line[0] == 'f' : #face
face = line[1:]
if len(face) != 4 :
print line
#raise Exception('not a quad!')
for i in range(0, len(face)) :
face[i] = face[i].split('/')
# OBJ indexies are 1 based not 0 based hence the -1
# convert indexies to integer
for j in range(0, len(face[i])) : face[i][j] = int(face[i][j]) - 1

#Now we lay out all the vertex/texcoord/normal data into a flat array
#and try to reuse as much as possible using a hash key

V2 = []
T2 = []
N2 = []
C2 = []
F2 = []

for face in F :
for index in face :
#print V[index[0]], T[index[1]], N[index[2]]
key = '%s%s%s%s%s' % (V[index[0]][0], V[index[0]][1], V[index[0]][2], T[index[1]][0], T[index[1]][1])
idx = get_index(key)

if not idx[0] :
V2.append([float(V[index[0]][0]), float(V[index[0]][1]), float(V[index[0]][2])])
T2.append([float(T[index[1]][0]), float(T[index[1]][1])])
N2.append([float(N[index[2]][0]), float(N[index[2]][1]), float(N[index[2]][2])])
C2.append([random.random(), random.random(), random.random()])


print len(V) * 3 * 4, 'bytes compared to', len(V2) * 3 * 4, 'bytes'

#return numpy arrays
return [
array(V2, dtype=float32),
array(T2, dtype=float32),
array(N2, dtype=float32),
array(C2, dtype=float32),
array(F2, dtype=uint32)

Wednesday, February 24, 2010

Funny Web Accidents

Good News Everyone,

Terrible news everyone... haha. Thought I would post something funny I have found, MAN Google can be fun. Check out site site:

Now answer the BONUS QUESTION: what does "phpects" mean?

Hint (spoiler alert), ever tried replacing ASP with PHP?


Tuesday, February 23, 2010

Compiling wine with processor optimizations

Hi everyone,

Just thought I would show you all a quick trick to compile wine to be as fast as it can be!

You need to look into your compiler (GCC) and which flags it will support for your processor. Since I have an AMD Phenom II, I used -march=amdfam10 for GCC.

Here is a link where you can find out which -march to use :

Note that link is for AMD not sure where intel info is.

CFLAGS="-march=amdfam10 -O2 -pipe -fomit-frame-pointer"
export CFLAGS
./configure --disable-test
make depend && make
sudo make install

And we're done!

Thursday, February 18, 2010

Using Facebook API and using FQL

Using FQL Facebook Query Language


So you've made a facebook application, and you want to start using FQL to query all sorts of interesting things from Facebook's massive database of users

Facebook FQL Programming APIs

There are many API's available for facebook to access the FQL language. Some are for PHP and there are some for python (my favourite).

Aparently there is also one available using HTTP at the following url:

We will be using PHP. You must download the Facebook PHP client library. In our PHP script, we include the main file as shown:

include_once '/path-to-api/facebook-platform/php/facebook.php';

Diving In

Next, we create our instance of our facebook object:

$facebook = new Facebook('API_KEY','SECRET_KEY');
$fb_user = $facebook->require_login(); //require that we log in, because we need the user's id

Make sure you replace the API_KEY and SECRET_KEY with the proper strings, or also you could use php's define. Now what I wanted to do with FQL is generate a list of all the "Pages" that I have created under my account. I created well over 100 so I needed an easy way to get that list. Here is the example FQL query:

page_id IN (SELECT uid, page_id FROM page_admin WHERE uid = {$uid})

Note: FQL does not support standard SQL like JOIN, thus we use the subquery shown and the IN operator.

We use the client fundion "fql_query" in order to return the results of this query. Note I have {$uid} in there, that must also be replaced with the user's ID who created all those pages. Here is the PHP code I used for this:

$uid = $facebook->api_client->user; // get the user id
$query = "SELECT page_id, name, page_url, fan_count FROM page WHERE page_id IN (SELECT uid, page_id FROM page_admin WHERE uid = {$uid})";
$res = $facebook->api_client->fql_query($query);

There you have it! For more info on FQL see the official API documentation here:

FQL Documentation
List of FQL Tables available for query
Sample FQL queries

Wednesday, February 17, 2010

GLSL Shader Examples Vertex and Fragment

Hi Everyone,

Here is a quick example of a GLSL shader I use in my c++ 3d engine. Just thought I'd share some of the GLSL syntax with the internet community. I will fill in more information soon!

Vertex Shader

varying vec3 normal;

void main() {

normal = normalize(gl_NormalMatrix * gl_Normal);

//gl_Position = gl_ModelViewMatrix * gl_Vertex;

/*vec2 test = vec2((gl_MultiTexCoord0.xy - 0.5) * 2.0);
test.x *= 1.01;
test.y *= 1.01;
gl_Position.xy = test.xy;
gl_Position.z = 0.0;
gl_Position.w = 1.0;*/

gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;

Fragment Shader

uniform sampler2D texture_0;
uniform sampler2D texture_1;
varying vec3 normal;

void main() {
vec4 color = vec4(texture2D(texture_0, gl_TexCoord[0].xy));
float NdotL,NdotHV;
vec3 lDir = vec3(0.0, 0.0, 1.0);

if(color.r + color.g + color.b == 0.0)
color = vec4(1.0, 1.0, 1.0, 1.0);

if(color.r < 0.85)
color.r = 0.0;

color.g = color.r;
color.b = color.r;
//color.a = color.r;

vec3 n = normalize(normal);
NdotL = max(dot(n,lDir),0.0);
color.rgb *= NdotL;
gl_FragColor = color;

Thursday, February 11, 2010

Mass Effect 2 Runs in Wine in Ubuntu Linux

Mass Effect 2 using Linux in Wine

Get Wine

So I got mass effect 2 working in wine. Man is it a great game. To get it to work in wine:

go here and download wine (latest version as of now 1.1.38)

(note I would recommend 1.1.38 if you want to use the patches below)

extract and enter the directory in the terminal. Now we need to run the configure script:


Or to speed up compilation:

./configure --disable-tests

Which will skip compiling the wine test suite which generally you won't need.


Two patch files are located at: for the mouse warping for crashes (I didnt seem to need this patch)

Next, you will need to apply some patches. There is one for crashing, and another to fix the mouse warping. I don't have these on hand at the moment. I couldn't get my mouse warping to work properly, so I edited mouse.c under dlls/dinput/ and made it so mouse button 3 would toggle mouse warp (on / off) which is a pain but makes the game more playable. To apply patches to a source, under the source tree, type:


Afterwards you can compile as shown below.


Ensure that you have XML support compiled in, as well as openal. You can determine this through the final output of the configure script. If you don't have the xml and openal dev packages, install them with your favourite package editor.

sudo make install

Again, an interesting way to speed up compilation on a multi core machine is to use:

make -j 4
sudo make install

You can replace "-j 4" with the number of processors you have (I have a quad core).


now run "winecfg" and set up a virtual desktop (I have to using nvidia latest binary driver geforce 9600).

Also, we need to install packages with winetricks to get this to work

sh winetricks d3dx9 d3dx10 vcrun2005 physx

Now run the installer, and finally run the game. I am able to play on full settings with 1900x1080 resolution :D