• Advertisement
Sign in to follow this  

Manual cube texture sampling

This topic is 3480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
I believe it uses the largest component to determine which face to sample from, and then scales the other two components to the [0,1] range to come up with the 2D UV coordinates. At least I *think* this is how it works.

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 > _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
Sign in to follow this  

  • Advertisement