PDA

View Full Version : cubic reflection map in glsl

def
11-26-2004, 01:29 AM
Hi,

I need some help with calculating the correct texture coordinates for a cubic reflection map.
This works well in an ARB_vertex_PROGRAM but I have trouble with a GLSL vertex shader.

I need to multiply the reflection vector (in view space) with the inverse modelview matrix to gain the cubemap texture coordinates in world space:

vTexCoord = reflVec * gl_ModelViewMatrixInverse;

The glsl compiler returns the following error:

'invalid operands to "mul"'

How is this done in GLSL?

Thanks for any hints.

eyebex
11-26-2004, 02:31 AM
Taken from the Orange Book (http://www.3dshaders.com/) , chapter 9.7:

vec3 RelectionMap(in vec3 pos,in vec3 normal) {
float NdotU,m;
vec3 u;
u=normalize(pos);
return reflect(u,normal);
}Where "pos" is the vertex position and "normal" the vertex normal for which to calculate the reflection texture coordinate.

def
11-26-2004, 03:30 AM
Thanks, but the reflection vector is not the problem! It's getting it to index into the world cubemap correctly which is giving me problems.

Why can't I multiply a vector with the gl_ModelViewMatrixInverse???

eyebex
11-26-2004, 03:55 AM
Ah, sorry. AFAIK gl_ModelViewMatrixInverse is not yet available in all implementations. But you don't need that anyway to index the cubemap. Just do something like:

// The number of the texture unit which contains the cubemap goes into this unifrom variable.
uniform samplerCube env;

void main(){
// ...
vec4 color=textureCube(env,reflVec);
// ...
}I hope I got your question right this time.

def
11-26-2004, 04:15 AM
Actually, I'm doing the texture lookup in the fragment shader, so I am storing a texture coordinate tripplet per vertex.
When doing the reflection calculation in eye space, the reflection is "almost" right, but it is still view direction dependent (and upside down). Using the inverse modelview matrix solved this in the ARB program...
I am actually trying to convert an ARB program to GLSL; could also be called reverse engineering
:)

jra101
11-26-2004, 02:09 PM
vTexCoord = reflVec * gl_ModelViewMatrixInverse;

The glsl compiler returns the following error:

'invalid operands to "mul"'You are most likely getting this error because reflVec is a vec3 instead of a vec4. You need to use a vec4 when multiplying a vector by a mat4.

def
11-30-2004, 02:09 AM
The vector format is not the problem, I checked that first of all. If "gl_ModelViewMatrixInverse" is not implemented in current NVidia drivers, which seems to be the case, I am looking for an alternative way to do this.
I haven't seen any cubemap reflection demos done in GLSL up to now...

Relic
11-30-2004, 02:21 AM
Originally posted by def:
The vector format is not the problem, I checked that first of all. If "gl_ModelViewMatrixInverse" is not implemented in current NVidia drivers, which seems to be the case, I am looking for an alternative way to do this. These release notes http://download.nvidia.com/developer/GLSL/GLSL%20Release%20Notes%20for%20Release%2060.pdf
say explicitly that gl_ModelViewMatrixInverse is implemented.

def
11-30-2004, 03:14 AM
Thanks Relic, I got rid of the error message this way:

vTexCoord = vec4(reflection, 0.0) * gl_ModelViewMatrixInverse;

I guess I should have "tripple checked" on the vector format issue...