10.8 C
New York
Friday, March 14, 2025

opengl – Does anybody know the way the intersection of BVH containers can decide whether or not or not a triangle seems?


I am programming a Ray Tracer utilizing Rust and OpenGL, I’ve already made Ray Tracing work within the fundamentals by changing the information to an SSBO and sending it to the Shader in a fundamental means… The issue is that the second I made a decision to implement a BVH tree (I haven’t got a lot expertise with these constructions), the code obtained actually buggy! Particularly:

The 3D mannequin is at place (0,0,0) in 3D house, for some purpose after I look from one of many angles, there are triangles that don’t seem… However they seem after I take a look at the mannequin from the opposite aspect of the X angle:

enter image description here

At first, I believed it was an error in the best way the triangles had been organized inside the BVH, earlier than it solely learn one of many triangle’s vertices to establish whether or not or not it was inside a field… I did this considering that sooner or later, as I’d use fashions with a greater distribution of triangles, I would not want containers smaller than the triangles… However then I modified to the system by which I discover out what the AABB of the triangle is and examine if any a part of it’s contained in the field.

However that did not change something, then I believed it could possibly be an error within the offset of the scale of the containers, as a result of when a field goes to be divided, as a substitute of constructing an ideal division of the field, I made it divide and broaden the field a little bit to the aspect it reduce with a small offset… Whatever the worth I put, the error nonetheless persists.

However that did not change something. Then I believed it could possibly be an error within the offset of the scale of the containers, as a result of when a field goes to be divided, as a substitute of constructing an ideal division of the field, I made it divide and broaden the field a little bit to the aspect it reduce with a small offset… Whatever the worth I put, the error nonetheless persists.

Then lastly I believed that the error have to be within the operate that I exploit contained in the shader to examine if the ray hits the field… And even so, I could not make it work.

enter image description here

I feel the error will not be within the operate that determines the AABB as a result of after I made the shader simply draw the containers, they all the time come out with the right format:

float rayIntersectsAABB(vec3 rayOrigin, vec3 rayDir, vec3 boxMin, vec3 boxMax){
    vec3 invDir;
    invDir.x = 1.0 / (abs(rayDir.x) < 1e-6 ? 1e-6 : rayDir.x);
    invDir.y = 1.0 / (abs(rayDir.y) < 1e-6 ? 1e-6 : rayDir.y);
    invDir.z = 1.0 / (abs(rayDir.z) < 1e-6 ? 1e-6 : rayDir.z);
    vec3 tMin = (boxMin - rayOrigin) * invDir;
    vec3 tMax = (boxMax - rayOrigin) * invDir;
    vec3 t1 = min(tMin, tMax);
    vec3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    if(tNear <= tFar && tFar > 0.0) return tNear;
    return -1.0;
}

However I feel there’s something mistaken with the best way I course of the BVH and the rendering:

        float mrtd = 1e30;
        // Ray Tracer
        int stack[1024];
        int stackPtr = 0;
        stack[stackPtr++] = 0;
        
        whereas(stackPtr > 0){
            int nindex = stack[--stackPtr];
            if(rayIntersectsAABB(ro,rd,pontomenor[nindex].xyz,pontomaior[nindex].xyz) != -1.0){
                proc += 1.0;
                if(pontomenor[nindex].a != -1.0){ // Nó Folha com Triângulos
                    for(int j = int(pontomenor[nindex].a); j < int(pontomenor[nindex].a)+int(pontomaior[nindex].a); j++){
                        float rtd = rayTriangle(ro,rd,posicao[j*3].xyz,posicao[(j*3)+1].xyz,posicao[(j*3)+2].xyz,0.00001);
                        proc += 1.0;
                        if(rtd > 0.0 && rtd

Word that after I do a easy rendering (with out utilizing BVH) the mannequin is rendered completely, however it is rather heavy. So the mannequin knowledge is right, what I did in my code for many who are unsure, is as a result of I loaded the 3D mannequin, created a BVH tree primarily based on the house that the mannequin’s vertices occupy, then I distributed the triangles throughout the BVH and on the identical time I despatched to the shader an SSBO std430 with solely 2 vec4, one with the smallest level of the field and the opposite with the most important level of the field, however the 4th worth of the primary vec4 is the variety of triangle indices to be ignored, the 4th worth of the second vec4 is the variety of triangles to be checked, I rearranged the mannequin’s triangles in order that it may examine the information shortly. If a field reveals nothing, it receives a -1.0 in each values. However so far as I do know, I’ve checked this knowledge loads and it appears to be right, after I roll the triangle rely contained in the BVH, none of them are repeated, the quantity inside it’s the identical because the mannequin earlier than it was positioned there… So I feel the error is in the best way I course of the rendering. There are 5850 triangles in whole, does anybody on the market who has programmed a ray tracer know what is going on on?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles