Archive for January, 2009

Visual Studio warnings

January 21, 2009

I found it useful to change some of the warnings generated by Visual Studio.

For instance, this statement:

if (num = 10) { doSomething(); }

Generates a warning, but I cannot see any case where this would not be a bug. So I change this warning and some others to errors: like this:


#pragma warning (error: 4715)
#pragma warning (error: 4706)
#pragma warning (error: 4553)    // '==' : operator has no effect; did you intend '='?
#pragma warning (error: 4150)    // deletion of pointer to incomplete type
#pragma warning (error: 4390)    // empty controlled statement found;
#pragma warning (disable: 4996)  // openf is dangerous (not!)

Now, there’s no way you can ignore it. By the way, there is a bug in Visual Studio, sometimes it won’t flag warning mentioned above at all.

Some old performance tricks

January 8, 2009

The documentation for the iphone’s VFP processor says that all floating point instructions take 1 cycle, apart from divide and square root, which take 15 cycles.

This leads to a very obvious optimisation, to cache your divide operations.

In other words if you have code like this:

float fl = 1.0007f;
for (i = 0; i < 16; i++)
matrix.Set(i, matrix[i]/fl);

you can increase performance by doing the divide outside the loop:

float fl = 1.0f/1.0007f;
for (i = 0; i < 16; i++)
matrix.Set(i, matrix[i]*fl);

I measured this and found that it does indeed make a big improvement.

Also, I thought it would be interesting to try out the old Quake inverse square root trick.

float InvSqrt(float x){
   float xhalf = 0.5f * x;
   int i = *(int*)&x; // store floating-point bits in integer
   i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
   x = *(float*)&i; // convert new bits into float
   x = x*(1.5f - xhalf*x*x); // One round of Newton's method
   return x;
}

This code works on the iPhone, but the results are less accurate.

float fl = 2.0f;
result = 1.f / sqrt ( fl );   // gives 0.707106769
result = InvSqrt(fl);   // gives 0.706930041

the actual value should be 0.70710678

I measured the performance and found that it’s a touch faster, but not much.

OpenGL Features Removed from iPhone

January 8, 2009

Full screen anti-aliasing is a feature that greatly improves the graphic quality of any game and is supported by the powervr chip. Unfortunately, you don’t have access to the feature, because the iPhone does not use EGL. Instead it uses the framebuffer extension to bind to a surface and in order to enable anti-aliasing you have to call eglChooseConfig with the EGL_SAMPLE_BUFFERS parameter and this is not possible.

Funnily, enough I found a project that appears to use EGL with the iPhone: http://code.google.com/p/iphone-dj/

However, looking a bit close I see that the egl.h file does not even exist in the SDK, so the code in that project should not even compile (pretty wierd that).

This also means that the pbuffer isn’t supported, as that is enabled via eglChooseConfig as well. And to top this off I also heard that the GL_IMG_vertex_program extension is disabled (I think this prevents you from using bump mapping and cartoon rendering – as demoed in the powervr sdk)

Edit:

I reciently discovered that there exists an unofficial iPhone SDK, which includes egl. This is probably what iphone-dj was using. So, you probably can get anti-aliasing working if you don’t mind using an unofficial SDK.

Rendering models with transparent textures

January 8, 2009

I was having problems rending low polygon trees in my scene. It was because they used transparent textures which are always problemic. I found this artical that helped me greatly:

http://www.sjbaker.org/steve/omniv/alpha_sorting.html

The solution was to render all opaque objects first, then do:

glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_ALPHA_TEST);
and now every thing renders perfectly