Original Post
I've made a dynamic cubemap for a refraction shader, it works fine, and I can move the model (with the shader) around in a scene and it refracts different things just as it should. But when I rotate my model, the refraction moves with the sides as if first the shading is done and then the rotation is performed. I just had a similar problem with another shader and that was because the vectors where in different spaces. My view vector is calculated using world coordinates, but the result is a vector which I believe is in model space(?) just as my normals. (Also, I tested to calculate the view vector in model space, but then my refractions are bad when I just move my model.) What spaces should my vectors be in when I pass them to the Cg function "refract"? Is it enough that they are in the same spaces? And if anyone have time too look through my shadercode (in Cg), here it is:
struct VertIn
{
float4 pos : POSITION;
float3 normal : NORMAL;
};
struct VertOut
{
float4 pos : POSITION;
float3 viewVec;
float3 normal;
};
struct FragOut
{
float4 col : COLOR0;
};
//vertex shader
VertOut main(VertIn IN, uniform float3 worldPos,
uniform float3 camPos,
uniform float4x4 projectionMatrix,
uniform float4x4 modelViewMatrix,
uniform float4x4 modelMatrix) //all transformations of the current object
{
VertOut OUT;
//view vector = (transform vertex to world space) - (camera position in world space)
OUT.viewVec = mul(modelMatrix, IN.pos).xyz - camPos;
//model space normal
OUT.normal = IN.normal;
OUT.pos = mul(projectionMatrix, mul(modelViewMatrix, IN.pos));
return OUT;
}
//fragment shader
FragOut mainF(VertOut IN, uniform samplerCUBE skyBox : TEXUNIT1)
{
FragOut OUT;
float3 normal = normalize(IN.normal);
float3 viewVec = normalize(IN.viewVec)
float3 refractVec = refract(viewVec, normal, 1.0/1.1);
float3 refrCol = texCUBE(skyBox, refractVec);
OUT.col = float4(refrCol, 1.0);
return OUT;
}