About OpenGL parallax mapping
Posted: Mon May 07, 2012 2:27 pm
I have a question about the parallax mapping in OpenGL driver version.
In COpenGLParallaxMapRenderer class, the vertex shader codes are as follows
The codes about calculating the eye vector :
"# normalize eye vector \n"\
"DP3 TempEyeVector.w, TempEyeVector, TempEyeVector; \n"\
"RSQ TempEyeVector.w, TempEyeVector.w; \n"\
"MUL TempEyeVector, TempEyeVector, TempEyeVector.w;\n"\
"MUL TempEyeVector, TempEyeVector, {1,-1,-1,1}; # flip x \n"\
I can't figure out why should flip x for the eye vector.
And if I try to skip this step, the parallax offset direction for normal texture is incorrect.
Can anyone explain this? or any hint?
In COpenGLParallaxMapRenderer class, the vertex shader codes are as follows
Code: Select all
// Irrlicht Engine OpenGL render path parallax map vertex shader
// I guess it could be optimized a lot, because I wrote it in D3D ASM and
// transferred it 1:1 to OpenGL
const char OPENGL_PARALLAX_MAP_VSH[] =
"!!ARBvp1.0\n"\
"#input\n"\
"# 0-3: transposed world matrix;\n"\
"#;12: Light01 position \n"\
"#;13: x,y,z: Light01 color; .w: 1/LightRadius^2 \n"\
"#;14: Light02 position \n"\
"#;15: x,y,z: Light02 color; .w: 1/LightRadius^2 \n"\
"#;16: Eye position \n"\
"\n"\
"ATTRIB InPos = vertex.position;\n"\
"ATTRIB InColor = vertex.color;\n"\
"ATTRIB InNormal = vertex.normal;\n"\
"ATTRIB InTexCoord = vertex.texcoord[0];\n"\
"ATTRIB InTangent = vertex.texcoord[1];\n"\
"ATTRIB InBinormal = vertex.texcoord[2];\n"\
"\n"\
"#output\n"\
"OUTPUT OutPos = result.position;\n"\
"OUTPUT OutLightColor1 = result.color.primary;\n"\
"OUTPUT OutLightColor2 = result.color.secondary;\n"\
"OUTPUT OutTexCoord = result.texcoord[0];\n"\
"OUTPUT OutLightVector1 = result.texcoord[1];\n"\
"OUTPUT OutLightVector2 = result.texcoord[2];\n"\
"OUTPUT OutEyeVector = result.texcoord[3];\n"\
"\n"\
"PARAM MVP[4] = { state.matrix.mvp }; # modelViewProjection matrix.\n"\
"TEMP Temp;\n"\
"TEMP TempColor;\n"\
"TEMP TempLightVector1;\n"\
"TEMP TempLightVector2;\n"\
"TEMP TempEyeVector;\n"\
"TEMP TempTransLightV1;\n"\
"TEMP TempTransLightV2;\n"\
"\n"\
"# transform position to clip space \n"\
"DP4 OutPos.x, MVP[0], InPos;\n"\
"DP4 OutPos.y, MVP[1], InPos;\n"\
"DP4 Temp.z, MVP[2], InPos;\n"\
"DP4 OutPos.w, MVP[3], InPos;\n"\
"MOV OutPos.z, Temp.z;\n"\
"MOV result.fogcoord.x, Temp.z;\n"\
"\n"\
"# vertex - lightpositions \n"\
"SUB TempLightVector1, program.local[12], InPos; \n"\
"SUB TempLightVector2, program.local[14], InPos; \n"\
"\n"\
"# eye vector \n"\
"SUB Temp, program.local[16], InPos; \n"\
"\n"\
"# transform the light vector 1 with U, V, W \n"\
"DP3 TempTransLightV1.x, InTangent, TempLightVector1; \n"\
"DP3 TempTransLightV1.y, InBinormal, TempLightVector1; \n"\
"DP3 TempTransLightV1.z, InNormal, TempLightVector1; \n"\
"\n"\
"# transform the light vector 2 with U, V, W \n"\
"DP3 TempTransLightV2.x, InTangent, TempLightVector2; \n"\
"DP3 TempTransLightV2.y, InBinormal, TempLightVector2; \n"\
"DP3 TempTransLightV2.z, InNormal, TempLightVector2; \n"\
"\n"\
"# transform the eye vector with U, V, W \n"\
"DP3 TempEyeVector.x, InTangent, Temp; \n"\
"DP3 TempEyeVector.y, InBinormal, Temp; \n"\
"DP3 TempEyeVector.z, InNormal, Temp; \n"\
"\n"\
"# normalize light vector 1 \n"\
"DP3 TempTransLightV1.w, TempTransLightV1, TempTransLightV1; \n"\
"RSQ TempTransLightV1.w, TempTransLightV1.w; \n"\
"MUL TempTransLightV1, TempTransLightV1, TempTransLightV1.w;\n"\
"\n"\
"# normalize light vector 2 \n"\
"DP3 TempTransLightV2.w, TempTransLightV2, TempTransLightV2; \n"\
"RSQ TempTransLightV2.w, TempTransLightV2.w; \n"\
"MUL TempTransLightV2, TempTransLightV2, TempTransLightV2.w;\n"\
"\n"\
"# normalize eye vector \n"\
"DP3 TempEyeVector.w, TempEyeVector, TempEyeVector; \n"\
"RSQ TempEyeVector.w, TempEyeVector.w; \n"\
"MUL TempEyeVector, TempEyeVector, TempEyeVector.w;\n"\
"MUL TempEyeVector, TempEyeVector, {1,-1,-1,1}; # flip x \n"\
"\n"\
"\n"\
"# move light and eye vectors out\n"\
"MAD OutLightVector1, TempTransLightV1, {0.5,0.5,0.5,0.5}, {0.5,0.5,0.5,0.5}; \n"\
"MAD OutLightVector2, TempTransLightV2, {0.5,0.5,0.5,0.5}, {0.5,0.5,0.5,0.5}; \n"\
"MAD OutEyeVector, TempEyeVector, {0.5,0.5,0.5,0.5}, {0.5,0.5,0.5,0.5}; \n"\
"\n"\
"# calculate attenuation of light 1\n"\
"MOV TempLightVector1.w, {0,0,0,0}; \n"\
"DP3 TempLightVector1.x, TempLightVector1, TempLightVector1; \n"\
"MUL TempLightVector1.x, TempLightVector1.x, program.local[13].w; \n"\
"RSQ TempLightVector1, TempLightVector1.x; \n"\
"MUL OutLightColor1, TempLightVector1, program.local[13]; # resulting light color = lightcolor * attenuation \n"\
"\n"\
"# calculate attenuation of light 2\n"\
"MOV TempLightVector2.w, {0,0,0,0}; \n"\
"DP3 TempLightVector2.x, TempLightVector2, TempLightVector2; \n"\
"MUL TempLightVector2.x, TempLightVector2.x, program.local[15].w; \n"\
"RSQ TempLightVector2, TempLightVector2.x; \n"\
"MUL OutLightColor2, TempLightVector2, program.local[15]; # resulting light color = lightcolor * attenuation \n"\
"\n"\
"# move out texture coordinates and original alpha value\n"\
"MOV OutTexCoord, InTexCoord; \n"\
"MOV OutLightColor1.w, InColor.w; \n"\
"\n"\
"END\n";
"# normalize eye vector \n"\
"DP3 TempEyeVector.w, TempEyeVector, TempEyeVector; \n"\
"RSQ TempEyeVector.w, TempEyeVector.w; \n"\
"MUL TempEyeVector, TempEyeVector, TempEyeVector.w;\n"\
"MUL TempEyeVector, TempEyeVector, {1,-1,-1,1}; # flip x \n"\
I can't figure out why should flip x for the eye vector.
And if I try to skip this step, the parallax offset direction for normal texture is incorrect.
Can anyone explain this? or any hint?