开发者

Opengl Refraction ,Texture is repeating on ellipsoid object

I have a query regarding refraction.

I am using a texture image for refraction(refertest_car.png).

But somehow the texture is getting multiplied and givinga distorted image(Refer Screenshot.png)

i am using following shader.

attribute highp vec4 vertex;
attribute mediump vec3 normal;

uniformhighp mat4 matrix;
uniformhighp vec3 diffuse_color;
uniformhighp mat3 matrixIT;

uniformmediump mat4 matrixMV;
uniformmediump vec3  EyePosModel;
uniformmediump vec3  LightDirModel;
varyingmediump vec4 color;

constmediump float  cShininess = 3.0;
constmediump float  cRIR = 1.015;

varyingmediump vec2   RefractCoord;

vec3 SpecularColor= vec3(1.0,1.0,1.0);

voidmain(void)
{
     vec3 toLight = normalize(vec3(1.0,1.0,1.0));

     mediump vec3 eyeDirModel = normalize(vertex.xyz -EyePosModel);
     mediump vec3 refractDir =refract(eyeDirModel,normal, cRIR);

     refractDir = (matrix * vec4(refractDir, 0.0)).xyw;

     RefractCoord = 0.5 * (refractDir.xy / refractDir.z) + 0.5;

     vec3 normal_cal = normalize(matrixIT *normal );
     float NDotL = max(dot(normal_cal, toLight), 0.0);

     vec4 ecPosition = normalize(matrixMV * vertex);
     vec3 eyeDir = vec3(1.0,1.0,1.0);
     float NDotH = 0.0;
     vec3 SpecularLight = vec3(0.0,0.0,0.0);

     if(NDotL > 0.0)
     {
         vec3 halfVector = normalize( eyeDirModel + LightDirModel);
         float NDotH = max(dot(normal_cal, halfVector), 0.0);
         float specular =pow(NDotH,3.0);

         SpecularLight = specular * SpecularColor;
     }

    color = vec4((NDotL * diffuse_color.xyz) + (SpecularLight.xyz)  ,1.0);

    gl_Position = matrix * vertex;
}

A开发者_JAVA百科nd

varyingmediump vec2 RefractCoord;
uniformsampler2D  sTexture;
varyingmediump vec4 color;

voidmain(void)
{
      lowp vec3 refractColor = texture2D(sTexture,RefractCoord).rgb;
      gl_FragColor = vec4(color.xyz + refractColor,1.0);
}

Can anyone let me know the solution to this problem?

Thanks for any help.

Sorry guys i am not able to attach image.


It seems that you are calculating the refraction vector incorrectly. Hovewer, the answer to your question is already in it's title. If you are looking at ellipsoid, the rays from the view span a cone, wrapping the ellipsoid. But after the refraction, the cone may be much wider, reaching beyond the edges of your images, therefore giving texture coordinates larger than 0 - 1 and leading to texture being wrapped. So we need to take care of that as well.

First, the refraction coordinate should be calculated in vertex shader as follows:

vec3 eyeDirModel = normalize(-vertex * matrix);
vec3 refractDir = refract(eyeDirModel, normal, cRIR);

RefractCoord = normalize((matrix * vec4(refractDir, 0.0)).xyz); // no dehomog!

RefractCoord now contains refracted eye-space vectors. This counts on "matrix" being modelview matrix (that is not clear from your code, but i suspect it is). You could possibly skip normalization if you wish the shader to run faster, it shouldn't cause noticeable errors. Now a little bit of modification to your fragment shader.

vec3 refractColor = texture2D(sTexture, normalize(RefractCoord).xy * .5 + .5).rgb;

Here, using normalize() makes sure that the texture coordinates do not cause the texture to repeat.

Note that using 2D texture for refractions should be only justified by generating it on the fly (as e.g. Half-Life 2 does), otherwise one should probably use cube-map texture, which does the normalization for you and gives you color based on 3D direction - which is what you need.

Hope this helps ... (and, oh yeah, i wrote this from memory, in case there are any errors, please comment).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜