View Full Version : Cube map access in fragment shader

Zulfiqar Malik
09-01-2005, 03:39 AM
I have a world with lots of different objects where each object is in object space and i translate it directly to eye space using glTranslatef(...) etc. etc.

Also some of my objects can use a reflection material and their fragment program will access a cube map passed by the application for reflection texel lookup. The reflection vector is calculated per-vertex and interpolated value is used in fragment shader. Now the problem is that a cube map lookup vector must be in world space (at least thats whats documented, since a cube map is in world space) and the input positions of my vertexes are in object space whereas output vertexes are in clip space. I need to have vertexes in world space so that i can do proper cube map lookups and that's not available in OpenGL. I want to know whether there is a way to have proper cube map lookups using a reflection vector in eye space (as that's available in GL), or do i have to create a world space matrix logic in application? But by doing so i would be transferring load to the CPU which i don't want!

Are there any other work arounds because cube maps are fairly common these days, and lots of games out there use them. Thanks in advance.

09-01-2005, 11:05 AM
heres the basic method of finding a reflection with glsl. cubemaps dont have any position they just store directions

vec3 reflectColor = textureCube( environmentMap, reflect( view_dir, normal ) )

Zulfiqar Malik
09-01-2005, 07:57 PM
That's the whole problem! Since they (cube maps) are in world coordinates therefore they give the proper texel if the lookup vector is in world coordinates!

I know how to do cube map lookups in a fragment shader. The code snippet that you gave will give you the proper texel if "view_dir" is in world space, whereas the lookup will be incorrect if its in eye space. That's precisely what i wanna know, that whether its possible to do proper cube map lookups in eye space too? If yes, then please give me a method.

Zulfiqar Malik
09-02-2005, 12:35 AM
I am terribly sorry for posting this on Advance coding forum, i wanted to post it on GLSL forum. Its a mistake and consider this thread closed over here. I am posting a new thread on GLSL forum (moving/closing a thread is reserved for admins).