## Got side tracked...again

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Got side tracked...again

With all the commotion over real time ray tracing and a few of the members of the board have taken a crack at it I figured I'd take a stab at it for shits and giggles. I haven't gotten too far, just copy/pasted some Ray/Triangle intersection code and some code for calculating rays from the camera. I have thought about this a few times when a couple others shared their ray-tracer code here.

The common approach is seemingly to calculate the rays each frame. This means millions of calls to the sqrt function depending on screen resolution. The reason for this is because of the difference between how the transformations are handled in rasterization and ray-tracing. It seems that in all of the ray-tracing code I could find, you move the camera through the world as well as the near plane or screen ( so rotating the camera rotates the near plane ) and you then cast the rays in the new direction the camera is facing. This differs from the way we usually handle it in DirectX where you take the inverse transformation of the camera to make the camera the reference point or origin. Which means the camera doesn't actually move per se, you are actually moving the world toward or away from the camera, seems trippy to think of it like that.

To me, this would be far more efficient as the rays could then be precalculated because the view never changes. So that's what I have for a starting point. I mostly do the same steps as I would in D3D or in this case it's actually closer to the 3D Fundamentals framework. I use a vertex buffer to transform all the vertices using a world and view matrix. Because of how the rays work, you don't need a projection matrix.

The code I chose to copy uses counter clockwise winding for it's vertices which I am not as familiar with since the depth is -Z is forward facing like in OpenGL. I'll have to play around with it and see if I can make the necessary changes to get it working the other way round if only for my sanity.

After reading through some surface level material, it seems the next step would be to partition the scene into a grid of sorts and sort the triangles into the grid. This is where my idea kind of breaks down I suppose. If the camera is the thing that moves, you have to recalculate the rays yes, but the triangles can be presorted into this grid. If the world moves, the rays can be precalculated, but the triangles will need to be sorted each frame.

I'll just have to keep going to see what happens. One of the benefits of ray-tracing is suppose to be scaling. In rasterization, the more complex the scene or more triangles there are the slower the render whereas with ray-tracing, the more pixels you have the slower the render. The rays are limited to the number of pixels you display times the number of rays you send out per pixel. In a level or scene you could have millions of triangles so sorting them still should ends up limiting the performance based on triangle count thus losing the benefit ray-tracing would bring.

Oh well, down the rabbit hole I go.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Got side tracked...again

Reading up on some acceleration structures which are ways of reducing the number of ray-triangle intersection tests. It seems to me that if you have a static scene and as mentioned in the previous post moved the camera instead of using the camera as a reference point, these would be pretty useful. However, building most of these structures seems pretty time consuming in itself and would take up more time than just checking each triangle in the scene.

I'm beginning to see why ray-tracers move the camera and recalculate the rays instead of dealing with the geometry. First off, using a "vertex shader" to transform all the geometry to use the camera as the origin still means each frame you must transform ALL vertices in the scene which could out number even the number of primary rays being create ( the primary rays determine which triangles are visible, then you have shadow rays, reflection rays and light rays for instance ).

Perhaps using a bounding volume hierarchy would work where you precalculate the bounding volumes once. For each frame transform the bounding volumes. As they are transformed to be in the camera's frame of reference, test the intersection of the ray and the bounding volume. If the bounding volume has no children, then you'd only need to transform the vertices stored in the bounding volumes and run the ray-triangle intersection tests against those within the volume. The question then becomes, what about other rays like reflection? Should the same process be done to put the BVHs in the frame of reference of the new ray? I'm guessing so as this would still allow quicker lookup and fewer transformations overall possibly.

Interesting stuff...to me anyway.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Got side tracked...again

If anyone is interested in DirectX Raytracing in DX12: Introduction to DirectX Raytracing
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Got side tracked...again

I have realized why I get side tracked or lose interest in whatever project I'm working on, or at least have a better understanding of my faults.

I think I spend too much time in front of the computer, seeing as how I don't have a job and living off disability. I guess what I'm getting at is lack of focus and then there's fatigue. Whether it be eye strain or lack of exercise or sleep deprivation or just late nights. I'm not treating my body nor my mind well. I'm always tired with the occasional days of clarity where I do get a fair amount of programming done.

Anyway, I just need to set a schedule for sleep to start, get some exercise and maybe start a journal of sorts to write down ideas that pop in from time to time. I'm so not motivated to do any of that, but that's what I think needs to happen. Currently in a catch 22, I'm overweight so I'm tired and lazy and don't want to exercise, but without the exercise, I won't lose the weight ( any time soon anyway ).

I am such a child even though I'm almost 40 years old.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm

### Re: Got side tracked...again

Take a break dude. I do it all the time from coding and such. I took up archery for a while and it consumed me and my spare time. I'm fortunate to live out in the country on a large piece of land so i can do that but the end result is the same. I come back to code with a fresh mind and new ideas. exercise is a great way to clear the mind. I work at a hard labor demanding job (farming for 25+ years). I'm 51 now and distractions are a great way to clear my mind. It works for me but not for all.
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Got side tracked...again

Did a few exercises today, but here I am at 4:30 in the morning, doh.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

cyboryxmen
Posts: 180
Joined: November 14th, 2014, 2:03 am

### Re: Got side tracked...again

Try walking a marathon. That's a good start.
Zekilk

albinopapa
Posts: 3973
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Got side tracked...again

cyboryxmen wrote:
July 17th, 2019, 12:27 pm
Try walking a marathon. That's a good start.
"The marathon is a long-distance race with an official distance of 42.195 kilometres (approximately 26 miles 385 yards), "

Yeah, that doesn't sound like a GOOD start lol. I walk about 2.5 miles per hour, that would be a 10 hour walk for me plus breaks. The farthest I've walked in my life is 10 miles and my legs went numb by the end, but hated me all the next day. I've ridden a mountain bike 25 miles and it took me about 3 hours, again legs hated me.

Nah, I need to start off with about 4 miles walking or 10 miles riding. One of my issues is being an albino I burn real easily, sunscreen may work for about an hour at a time and there's no way I'm wearing long sleeves and pants and a hat while exercising during the summer. I have a pool, not big enough to do laps, but I can do a sort of water aerobics like jogging around the inner perimeter. Lots of resistance and light on the joints. I'll be able to do this after 6pm when the trees start shading the pool.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com