hi,
Our engine lets you generate texture coordinates using Sphere mapping.
When using hardware acceleration the code looks something like this…
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
Nothing too exciting there.
Now I am trying to write a software implementation of this, as there are some situations where I need to manipulate the generated texture coordinates. It also will provide a handy fallback in cases where broken drivers produce bad results.
I started with the OpenGL reference implementation for Sphere mapping, but I end up with slightly different texture coords to the ones generated on the hardware (the image looks almost right, but is clearly wrong in places). Here is the source I am using.
// generate the texture coordinates for every vertex
for (i=0; i<r.output.numverts; i++)
{
// transform normal to eye space : n
// transform vertex to eye space : u
// normalise vertex (gets eye to vertex direction) : u
// reflected vector r = u - 2(u.n)n
// m = 2 * sqrt(rxrx + ryry + (rz+1)*(rz+1))
// s = (rx / m) + 0.5
// t = (ry / m) + 0.5mat4_RotateVec3(r.modelview_inverse_transpose, normals[i], n); mat4_TranslateRotateVec3(r.modelview, r.xyz[i], u); vec3_Normalise(u); twonu = 2.0f * vec3_dotproduct(n, u); rx = u[0] - twonu * n[0]; ry = u[1] - twonu * n[1]; rz = u[2] - twonu * n[2]; m = 2 * com_sqrt(rx*rx + ry*ry + (rz+1)*(rz+1)); if (m) { dst->s = rx / m + 0.5f; dst->t = ry / m + 0.5f; } else { dst->s = 0.5f; dst->t = 0.5f; } dst++; }
Can anyone spot anything that seems to be wrong with this?