2

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, cube...); 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.

Follows below my current 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;
}
  • Possible duplicate of [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/questions/42876586/reflection-and-refraction-impossible-without-recursive-ray-tracing) – Spektre Feb 27 '18 at 09:11
  • 1
    see the duplicate QA ... as recursion is not allowed in GLSL you need to workaround it. Anyway without any info about your code no one will look at it. As we simply do not know how are you representing and passing geometry to ray tracer, what kind of ray trace you have and much much more .... – Spektre Feb 27 '18 at 09:14
  • Hi @Spektre, I guess it is not a duplicate case here by two reasons: I don't need to process the ray in a recursive way, because I would like to compute the ray intersection for one reflection; latter, I don't need to handle the refraction effect, as presented in the other QA. – Rômulo Cerqueira Feb 27 '18 at 21:13
  • In my case, the scene (containing all objects with different shapes) are described in OpenSceneGraph. The GLSL is used to compute the normal and depth information from the viewpoint for any scene. – Rômulo Cerqueira Feb 27 '18 at 21:15
  • I have searched about this topic and all examples using GLSL need to know about each object's position and shape (as I discussed in the topic: spheres, cubes, cylinders...). My point is how to calculate the first intersection between a ray with defined position and direction in any shape presented in the scene. – Rômulo Cerqueira Feb 27 '18 at 21:17
  • well that is the point the ray tracer need to know whole geometry for each ray. That means you can not pass it by VBO/VAO instead encode it to texture like I do or use other means. If you do not need recursion then just do not use it (set layers to 1 and disable refraction by define ...) – Spektre Feb 28 '18 at 08:46
  • IF you have multiple shapes then store all of them as a set of triangles and do ray-triangle intersections ? – gallickgunner Mar 01 '18 at 09:51
  • Thanks for all tips, Spektre and @wandering-warrior. I will try to store the objects as a set of triangles. Any questions or progress I update here. – Rômulo Cerqueira Mar 02 '18 at 00:29

0 Answers0