Take the 2-minute tour ×

I want to texture a sphere with a cube map. So far my research has thrown up many many results on Google involving making OpenGL auto generate texture coordinates, but I want to generate my own coordinates.

Given an array of coordinates comprising the vertexes of an imperfect sphere (height mapped but essentially a sphere) centered on 0,0,0, how would one generate texture coordinates for a cube map?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Are you doing this via GLSL? In that case textureCube accepts a vec3 as texture coordinate, which is a unit vector on a sphere. In your case you would take the coordinate of your fragment with respect to the center of the sphere, normalize it and pass it as a coordinate. No need to worry about the internal representation as six two-dimensional textures.

uniform samplerCube cubemap;
varying vec3 pos; // position of the fragment w.r.t. the center of the sphere
/* ... */
vec4 color = textureCube(cubemap, normalize(pos).stp);

It works like that also in fixed-pipeline OpenGL.

By the way, here is how the coordinates are used internally: the largest coordinate in absolute value is used to select which one of the six textures is read from (the sign selects positive or negative). The other two coordinates are used to lookup the texel in the selected map, after being divided by the largest coordinate.

share|improve this answer
slaps head thankyou! I'm working in plain C++ atm but eventually moving to GLSL once I know more about it –  Tom J Nowell Feb 11 '09 at 11:15
add comment

Your Answer


