romulogcerqueira

02-27-2018, 01:28 PM

I'm looking for to simulate the reflection effect using ray tracing on GLSL, however I could not find good references, examples or tutorial related to this topic. When I got some interesting data, the method is limited for specific object's surfaces (e.g. sphere, box, cylinder...) and it needs to know object's position and radius; it is not my case. I also know the GLSL does not support recursive functions, but as far as I know the ray tracing can be done iteratively.

My goal is to simulate the reverberation process for an acoustic sensor as follows: primary reflections by rasterization; and secondary reflections by ray tracing. When a ray hits the object's surface, the distance and normal values are measured.

My scene with all objects is modeled using OpenSceneGraph, and I have used GLSL to handle the normal and position data from the viewpoint on GPU.

Follows below my current GLSL code. At this moment, I am able to calculate the ray parameters (world position and direction vector values, for each pixel), however I do not know how to calculate the data when a ray hits a surface.

Thanks in advance. Any help is very much welcome.

Vertex shader:

#version 130

uniform mat4 osg_ViewMatrixInverse;

out vec3 positionEyeSpace;

out vec3 normalEyeSpace;

uniform vec3 cameraPos;

// ray definition, with an origin point and a direction vector

struct Ray {

vec3 origin;

vec3 direction;

};

void main() {

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

// world space

mat4 modelWorld = osg_ViewMatrixInverse * gl_ModelViewMatrix;

vec3 positionWorldSpace = vec3(modelWorld * gl_Vertex);

vec3 normalWorldSpace = mat3(modelWorld) * gl_Normal;

// eye space

positionEyeSpace = vec3(gl_ModelViewMatrix * gl_Vertex);

normalEyeSpace = gl_NormalMatrix * gl_Normal;

// calculate the reflection direction for an incident vector

vec3 I = normalize(positionWorldSpace - cameraPos);

vec3 N = normalize(normalWorldSpace);

vec3 reflectedDirection = normalize(reflect(I, N));

}

Fragment shader:

#version 130

in vec3 positionEyeSpace;

in vec3 normalEyeSpace;

uniform float farPlane;

uniform bool drawNormal;

uniform bool drawDepth;

out vec4 out_data;

void main() {

vec3 nNormalEyeSpace = normalize(normalEyeSpace);

vec3 nPositionEyeSpace = normalize(-positionEyeSpace);

float linearDepth = sqrt(positionEyeSpace.x * positionEyeSpace.x +

positionEyeSpace.y * positionEyeSpace.y +

positionEyeSpace.z * positionEyeSpace.z);

linearDepth = linearDepth / farPlane;

// output the normal and depth data as matrix

out_data = vec4(0, 0, 0, 1);

if (linearDepth <= 1) {

if (drawNormal) out_data.z = abs(dot(nPositionEyeSpace, nNormalEyeSpace));

if (drawDepth) out_data.y = linearDepth;

}

gl_FragDepth = linearDepth;

}

My goal is to simulate the reverberation process for an acoustic sensor as follows: primary reflections by rasterization; and secondary reflections by ray tracing. When a ray hits the object's surface, the distance and normal values are measured.

My scene with all objects is modeled using OpenSceneGraph, and I have used GLSL to handle the normal and position data from the viewpoint on GPU.

Follows below my current GLSL code. At this moment, I am able to calculate the ray parameters (world position and direction vector values, for each pixel), however I do not know how to calculate the data when a ray hits a surface.

Thanks in advance. Any help is very much welcome.

Vertex shader:

#version 130

uniform mat4 osg_ViewMatrixInverse;

out vec3 positionEyeSpace;

out vec3 normalEyeSpace;

uniform vec3 cameraPos;

// ray definition, with an origin point and a direction vector

struct Ray {

vec3 origin;

vec3 direction;

};

void main() {

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

// world space

mat4 modelWorld = osg_ViewMatrixInverse * gl_ModelViewMatrix;

vec3 positionWorldSpace = vec3(modelWorld * gl_Vertex);

vec3 normalWorldSpace = mat3(modelWorld) * gl_Normal;

// eye space

positionEyeSpace = vec3(gl_ModelViewMatrix * gl_Vertex);

normalEyeSpace = gl_NormalMatrix * gl_Normal;

// calculate the reflection direction for an incident vector

vec3 I = normalize(positionWorldSpace - cameraPos);

vec3 N = normalize(normalWorldSpace);

vec3 reflectedDirection = normalize(reflect(I, N));

}

Fragment shader:

#version 130

in vec3 positionEyeSpace;

in vec3 normalEyeSpace;

uniform float farPlane;

uniform bool drawNormal;

uniform bool drawDepth;

out vec4 out_data;

void main() {

vec3 nNormalEyeSpace = normalize(normalEyeSpace);

vec3 nPositionEyeSpace = normalize(-positionEyeSpace);

float linearDepth = sqrt(positionEyeSpace.x * positionEyeSpace.x +

positionEyeSpace.y * positionEyeSpace.y +

positionEyeSpace.z * positionEyeSpace.z);

linearDepth = linearDepth / farPlane;

// output the normal and depth data as matrix

out_data = vec4(0, 0, 0, 1);

if (linearDepth <= 1) {

if (drawNormal) out_data.z = abs(dot(nPositionEyeSpace, nNormalEyeSpace));

if (drawDepth) out_data.y = linearDepth;

}

gl_FragDepth = linearDepth;

}