|
Post by inventordave on Mar 31, 2020 17:36:36 GMT
Hi,
I stopped working on the raytracer for a while, and came back to it today. I have implemented the ability to import an .obj file mesh, and generate a group of triangles from it, to render. My raytracer is written in vanilla Javascript rendering to a Canvas in the browser window.
The problem is, my code is unoptimised, and even in my "preview" option of 150x84, it is taking over 1000ms to render each pixel, so about 2.5 mins per row, of 84 rows. As I have MCI, and my noggin's taken a floggin', I don't even know if I've positioned the camera right to get the teddy bear in view [EDIT: I was tired, I've had a nap, ignore this awkward nugget about the camera]... I know how to program, all my tests have passed, and I've been happily playing with my raytracer, but I obvs need to do some serious optimisations, any suggestions? I don't want to wait ~200 mins to see if I get a suitable image....
Thanks!
Lee David O'Dea.
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Mar 31, 2020 19:31:29 GMT
Hi,
Inverting a matrix is very slow and you have to do that very often. So keep the result to save some time, this the most efficient optimisation you can start with.
|
|
|
Post by inventordave on Apr 1, 2020 0:59:35 GMT
I was very tired at the time of posting, I've had a nap, and now I'm thinking clearly about the camera. Don't know what I was thinking... [EDIT: Just noticed my stupid comment about the preview size of the image, I actually insinuated the per-pixel render time is affected by it, blimey being tired really shuts my brain down...]
I tried optimising the inverse matrix part, but I know how to do it better, so I'll try that.
A bounding box around the object (a teddy bear) won't do much good, but nearly 3 hrs is not cool.
|
|
|
Post by inventordave on Apr 1, 2020 1:18:01 GMT
I now cache the inverse matrices and it positively rockets....
I shall probably inline as much code as possible, eliminate fatuous function calls...
|
|
|
Post by inventordave on Apr 1, 2020 3:10:41 GMT
Sorted. Now I think I will implement a bounding box optimisation....
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Apr 1, 2020 6:40:11 GMT
If you want to inline some code, maybe you could have a look at the triangle intersect local method. Each call creates up to 3 new vectors in memory just to check the value of some dot products.
For instance, in my code, I replaced : var dirCrossE2 = ray.Direction * E2;
by this: var dirCrossE2_X = rayDir.Y * E2.Z - rayDir.Z * E2.Y; var dirCrossE2_Y = rayDir.Z * E2.X - rayDir.X * E2.Z; var dirCrossE2_Z = rayDir.X * E2.Y - rayDir.Y * E2.X;
So I don't have to create an object in memory (a slow operation).
|
|
fremag
Junior Member
Posts: 73
|
Post by fremag on Apr 1, 2020 6:42:56 GMT
|
|
|
Post by vorpal on Jan 21, 2023 15:28:55 GMT
Sorted. Now I think I will implement a bounding box optimisation.... View AttachmentWhat was the transformation you used to get the teddy bear where you have it? I am trying everything and I just keep getting further and further away from where I want to be.
|
|
|
Post by vorpal on Jan 21, 2023 15:35:24 GMT
Sorted. Now I think I will implement a bounding box optimisation.... View AttachmentBTW, what is your implementation written in? Did you incorporate multithrreading? Most of the scenes so far take less than a minute (often less than 30 seconds) for me to render in Kotlin.
|
|