Manual cube texture sampling

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

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 on other sites
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 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 on other sites
thanks for the snippet swift, works perfectly :)

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• Forum Statistics

• Total Topics
633271
• Total Posts
3011160
• Who's Online (See full list)

There are no registered users currently online

×