Mini-Ray Raytracer

miker1264 · 6694

Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #30 on: July 05, 2021, 05:30:35 AM
I don't want to create confusion about what you are doing, so maybe you can examine the archives I posted on it just for the purpose of learning or if you are easier to take a compile, skulpt3d does not need MUI anyway, it has always requested enough enough Accelerated or with 060 or with PPC included Warp3D, we have the power of X86 and WAZP3D :)



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #31 on: July 05, 2021, 09:50:14 AM
I don't want to create confusion about what you are doing, so maybe you can examine the archives I posted on it just for the purpose of learning or if you are easier to take a compile, skulpt3d does not need MUI anyway, it has always requested enough enough Accelerated or with 060 or with PPC included Warp3D, we have the power of X86 and WAZP3D :)

The Skulpt3D sources would be great for reference. But it is written in C++ as are many 3d rendering softwares.

Thanks for the links.

I'm trying every avenue of approach to solve the problem of rendering the polygon mesh using triangles.

I'm studying the Moeller-Trumbore Algorithm to find out if the raytracer code needs to be revised. It may be possible to optimize the render code by using a bounding box and hit test.

It currently imports a txt file that is a modified OBJ file in text format. That's another thing that could be improved upon - reading OBJ files.

I'm getting closer to a solution for the rendering problem. The raytracer requires 2 pieces of information. It needs the results of hit test for intersection from both sphere and triangle. But it also requires distance. For ray_sphereIntersection the distance values vary accross the image with an average double value of 0.000085 and the spheres render correctly.

The raytracer application uses distance to determine if the intersection is in front of or behind the background. If the distance is closer the ray hits the surface. If farther away it hits the background. Rendering the polygon mesh with triangles only returns background colors. But the distance remains constant at 0.000001 and that seems odd. Given a 3D Surface there are high points and low points. We expect the distance to vary accross the scene. But for ray_triangleIntersection it doesn't change. So the pixel is always behind the background.

So it seems the distance calculation for triangles is incorrect. 
« Last Edit: July 05, 2021, 01:00:23 PM by miker1264 »



Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #32 on: July 05, 2021, 01:36:21 PM
I understood indeed I see that you are proceeding by degrees and this is a good thing, I hope you can solve the problem :-\



Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #33 on: July 05, 2021, 01:40:54 PM
But some posts before you were talking about code from MacOSX, you found something interesting?



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #34 on: July 05, 2021, 02:13:33 PM
But some posts before you were talking about code from MacOSX, you found something interesting?

The raytracer code that I ported from Mac OS (x-code) is what I'm working with now. It renders spheres ( built-in) scenes using ray_sphereIntersection. But in order to render a polygon mesh of vertices and triangles that it loads from txt file it uses ray_triangleIntersection. Somewhere in that function there is an error. It renders spheres but it won't render triangle meshes.

Rendering spheres and triangles is the core of the raytracer so it needs to work correctly.

salvo,
I have to fully understand the Moller-Trumbore Algorithm before I can re-write the core of the raytracer. Lots of Vector Math. Here is the algorithm:

bool rayTriangleIntersect(
    const Vec3f &orig, const Vec3f &dir,
    const Vec3f &v0, const Vec3f &v1, const Vec3f &v2,
    float &t, float &u, float &v)
{
#ifdef MOLLER_TRUMBORE
    Vec3f v0v1 = v1 - v0;
    Vec3f v0v2 = v2 - v0;
    Vec3f pvec = dir.crossProduct(v0v2);
    float det = v0v1.dotProduct(pvec);
#ifdef CULLING
    // if the determinant is negative the triangle is backfacing
    // if the determinant is close to 0, the ray misses the triangle
    if (det < kEpsilon) return false;
#else
    // ray and triangle are parallel if det is close to 0
    if (fabs(det) < kEpsilon) return false;
#endif
    float invDet = 1 / det;
 
    Vec3f tvec = orig - v0;
    u = tvec.dotProduct(pvec) * invDet;
    if (u < 0 || u > 1) return false;
 
    Vec3f qvec = tvec.crossProduct(v0v1);
    v = dir.dotProduct(qvec) * invDet;
    if (v < 0 || u + v > 1) return false;
 
    t = v0v2.dotProduct(qvec) * invDet;
 
    return true;
#else
    ...
#endif
}
« Last Edit: July 05, 2021, 02:52:02 PM by miker1264 »



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #35 on: July 05, 2021, 06:53:37 PM
salvo,

After comparing the ray_triangleIntersection funtion in the raytracer to the two samples of the Moller-Trumbore Algorithm there were some inconsistencies. Most notably the value for EPSILON was wrong in the raytracer function. Also, some of the formulas for Vector3D values were slightly incorrect, missing values. Now I must revise the triangle function and verify it.  :)

The two samples of the original algorithm I'm using for comparison are from Wikipedia and Scratchapixel.com



Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #36 on: July 06, 2021, 05:19:40 AM
I understand miker indeed mostly seems to me a bit complex for me but I think it will come out a great job :)



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #37 on: July 06, 2021, 01:45:22 PM
I understand miker indeed mostly seems to me a bit complex for me but I think it will come out a great job :)

I'm making progress. Here's my new theory as to why the polygon mesh loaded from text file refuses to render correctly.

After substantially changing the ray_checkTriangleIntersection function and comparing results I have concluded that other than the value of EPSILON the function works as expected. What was unexpected was that even the built-in scene "spheres" has a background composed of vertices and faces. Since the spheres scene renders correctly then rendering triangles also works correctly. So what is the problem?

I suspect the problem with rendering the polygon mesh loaded from file is not a problem with the vertices or faces or the way it is rendered. But rather it is a problem with the loading process.

To prove that rendering triangles works I will attempt to convert the sample polygon mesh into a built-in scene which means I will need to hand edit all the hardcoded values for thousands of vertices and faces. If I could find a sample polygon mesh of only a few hundred vertices and faces that would be much easier.




miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #38 on: July 06, 2021, 02:23:28 PM
If I open "marbvase.3ds" in 3DS R4 and identify the annular ring at the top of the vase I can find the corresponding vertices and faces in the exported object file which is just a text file. That will probably be just a few hundred vertices and faces to hand edit.

Then I will make a built-in scene of that to render the triangles.



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #39 on: July 06, 2021, 03:26:06 PM
Wow! I had no idea Windows 10 had a built-in 3D Object Viewer!

I was opening a text file and I accidentally tried to open the obj file by mistake. But it opened instead in the 3D Viewer. Nice!  :)



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #40 on: July 06, 2021, 11:40:26 PM
Don't laugh. I know it's not my best work.

It's supposed to be a blue three-sided pyramid. But the coordinates are off. So it's looking more like the "leaning pyramid of piza" or something like that.  :)

At least it proves that the raytracer is rendering triangles.

This 3D object has four vertices and four faces (triangles). You can see that it's 3-dimensional because it is casting a shadow.
« Last Edit: July 07, 2021, 12:01:20 AM by miker1264 »



Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #41 on: July 07, 2021, 06:06:30 AM
no miker you are doing an excellent job, I don't mean 3D graphics, perhaps when the program will be ready to light some guide or buy books :)



aGGreSSor

  • Member
  • ***
    • Posts: 184
    • Karma: +25/-0
    • russian transit
Reply #42 on: July 07, 2021, 08:38:52 AM
Wow! I had no idea Windows 10 had a built-in 3D Object Viewer!
Unfortunately, there is no such datatype for Amiga. :( I was also surprised when I found out that files with the STL extension (which contained models for a 3D printer) easily viewed in Windows 10 without additional software.


miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #43 on: July 07, 2021, 11:45:30 AM
Wow! I had no idea Windows 10 had a built-in 3D Object Viewer!
Unfortunately, there is no such datatype for Amiga. :( I was also surprised when I found out that files with the STL extension (which contained models for a 3D printer) easily viewed in Windows 10 without additional software.

The OBJ file extension could be associated with the raytracer using a datatype descriptor to view the 3D Object Files in AROS, when we get to that point.



miker1264

  • Legendary Member
  • *****
    • Posts: 1827
    • Karma: +84/-6
Reply #44 on: July 09, 2021, 11:48:10 AM
First rendered polygon mesh imported from text file.  :) :) :)

Now that reading vertices from file has been fixed it's time to optimize the raytracer algorithms, remove hidden lines and surfaces, improved lighting, softer shadows, depth of field, improved materials, background images, and texture mapping. There is also a need to improve reflection and refraction and reduce the overall rendering time for large polygon mesh files.

After all that the focus will be importing Object Files and 3DSFiles, and SCN Scene Files directly into the raytracer.

Just a few little things...
« Last Edit: July 09, 2021, 12:06:22 PM by miker1264 »