Sign in to follow this  

Manual cube texture sampling

Recommended Posts

Im writing a simple image manipulation class, and at the moment I am writing various functions that allow one to sample a color from the image. Iv got the 2D image and 3D image cases working just fine, but Im stuck on cube images. Obviously in a shader, you can sample from a cube texture using a 3D vector. Now, im pretty sure that the hardware doesnt actually use this 3D vector directly. I figure that it probably figures out which face its pointing at, then figures out UV coordinates to sample from the face. How might this conversion from (3D sampling vector -> 2D UV coords. & face) work? Does hardware blend between two/three faces? (for example when the sampling vector is (1, 1, 1)) Thanks, Chris

Share this post

Link to post
Share on other sites
Ripped directly from my own code, this function returns the face index (matches OpenGL's enumeration of faces), and retunrs the s and t coordinates by reference (already scaled to the range [0,1]). This almost matches the OpenGL spec, except that my t coordinate may run the opposite way. The manual page for the OpenGL Cubemap extension provided the face orientations.
int CubeCacheModule::GetCubeFace(const vector3f &v, float &s, float &t) const
float _c[6] = { v.x,-v.x, v.y,-v.y, v.z,-v.z,};

float _s[6] = {-v.z, v.z, v.x, v.x, v.x,-v.x,};
float _t[6] = {-v.y,-v.y, v.z,-v.z,-v.y,-v.y,};

int max = 0;
for (int i = 1; i < 6; i++) {
if (_c[i] > _c[max])
max = i;

s = _s[max]/(_c[max]*2) + 0.5;
t = _t[max]/(_c[max]*2) + 0.5;

return max;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this