# Refraction using ray tracing in opengl 3.3

Hi, I have been trying work on refraction using ray tracing using the framework provided in following tutorial: (http://www.arcsynthesis.org/gltut/). I tried to use the glm::refract method to compute the refracted ray.

``````
//defining the ray
struct Ray{

glm::vec3 ray_o;
glm::vec3 ray_d;
float tMin;
float tMax;
}ray_r;

struct Bound
{
glm::vec3 minBound;
glm::vec3 maxBound;
}B, nB,wB;

// create a Triangle
typedef struct
{
glm::vec3 p1;
glm::vec3 p2;
glm::vec3 p3;
glm::vec2 texCoord1;
glm::vec2 texCoord2;
glm::vec2 texCoord3;
unsigned int matIndex1;

}triangle;
vector<triangle> T;
// Computing the refraction
Ray Compute_refraction(int hitId, float t)
{
const float airIndex = 1.0003f;
const float glassIndex = 1.5000f;
const float refractiveIndex = airIndex/glassIndex;
Ray refracted;
glm::vec3 v1 = T[hitId].p2 - T[hitId].p1;
glm::vec3 v2 = T[hitId].p3 - T[hitId].p1;
glm::vec3 tri_norm = -      glm::normalize(glm::cross(v1,v2));
glm::vec3 PI = ray_r.ray_o + t*ray_r.ray_d;
refracted.ray_o = PI;
refracted.ray_d = glm::normalize(glm::refract(ray_r.ray_d,tri_norm,refractiveIndex));
refracted.tMin = 0.0f;
refracted.tMax = 1000000.0f;

return refracted;
}

``````

I’m trying to compute the color with transparent effect after I test for the ray model intersection.

``````

void Refract_Color(Ray ray, GLubyte *tex_data, int index, unsigned tX, unsigned tY,unsigned tW,float *fb, float tri_t, int hitId)
{
const int e = 2;
glm::vec3 Point_I = ray_r.ray_o + tri_t*ray_r.ray_d;
float path_dist = glm::distance(Point_I,ray_r.ray_o);

glm::vec4 aborbance,transparency,ray_color(0.0f);

glm::vec3 v1 = T[hitId].p2 - T[hitId].p1;
glm::vec3 v2 = T[hitId].p3 - T[hitId].p1;
glm::vec3 tri_norm = glm::cross(v1,v2);

float ndotl = glm::dot(tri_norm, ray.ray_d);

if (ndotl < 0.0f) ndotl = 0.0f;

ray_color.r = ndotl/255;
ray_color.g = ndotl/255;
ray_color.b = ndotl/255;
ray_color.a = 0.0f;

//density of glass in grams/cubic meter = 2.579
fb[index+3] = 0.0f;
aborbance.r = -(fb[index + 0] * tri_t *2.579)/10000000000.0f;//path_dist*2.579)/1000000000.0f;
aborbance.g = -(fb[index + 1] * tri_t *2.579)/10000000000.0f;//path_dist*2.579)/1000000000.0f;
aborbance.b = -(fb[index + 2] * tri_t *2.579)/10000000000.0f;//path_dist*2.579)/1000000000.0f;
aborbance.a = fb[index +3];

transparency.r = pow(e,aborbance.r);
transparency.g = pow(e,aborbance.g);
transparency.b = pow(e,aborbance.b);
transparency.a = fb[index+3];
//fb[index + 0] = tex_data[(tX + tY * tW) * 4]/256.0f is for the original texture of the model

fb[index + 0] = tex_data[(tX + tY * tW) * 4]/256.0f  + ray_color.r*transparency.r;
fb[index + 1] = tex_data[(tX + tY * tW) * 4 + 1 ]/256.0f + ray_color.g*transparency.g;
fb[index + 2] = tex_data[(tX + tY * tW) * 4 + 2]/256.0f + ray_color.b*transparency.b;
fb[index + 3] = 0.0f;
}

``````

I also try to test the refracted to see whether it hits the wall behind the model.

``````

bool isRefract(Ray refracted, int hitId, float a, float b, float *wall_T)
{
//intersect with the wall
//float wall_T = 0.0f;
bool wall_test = ray_box_intersection(refracted, wB, refracted.tMin, refracted.tMax, wall_T);
//if a true intersection
if(wall_test)
{
refracted_texCoord = T[hitId].texCoord1 + a*(T[hitId].texCoord2 - T[hitId].texCoord1)
+ b*(T[hitId].texCoord3 - T[hitId].texCoord1);

refracted_tex.s = refracted_texCoord.x;
refracted_tex.t = refracted_texCoord.y;
refracted_tex.s = refracted_tex.s - floor(refracted_tex.s);
refracted_tex.t = refracted_tex.t - floor(refracted_tex.t);

//set the materialIndex for the current triangle

refracted_tex.MatIndex = T[hitId].matIndex1;

return true;

}
return false;
}

``````

Here I’m trying to compute a transparency factor and add it to the original color/texture. But i happen to see a white color and it fill the model(attached a screenshot of the output. I have done them in CPU havent tried Gpu computing yet. Could tell me how the transparency effect can be achieved by using ray tracing.

Thanks
Cintya

By looking at the attached image, it seems that there is a problem in computing the direction of the refracted light that leaves the surface.

Also, the transparency effect can be achieved in ray tracing using Fresnel factors(Implementation is available in OpenGL Shading language). Shortly, this factor determines the amount of light thats reflected and refracted on the surface of an object…