dafhi / 2016-FreeBASIC-path-tracer

only spheres. no accel. spaghetti code
2 stars 2 forks source link

Good work! And future suggestion... #1

Open erichlof opened 4 years ago

erichlof commented 4 years ago

@dafhi Hi David, Great job so far! I am impressed this is all coded in BASIC! To show my age a little, my first love of computer programming came from typing in BASIC source code that was listed in the monthly 'Compute!' magazines for my Commodore 64! I was just 11 or 12 at the time, so I really didn't have a firm grasp of what I was typing, especially POKE and PEEK statements (lol), but I have fond memories of creating my own little silly textual programs back in the 80's. I went off on a journey that took me through Pascal, C, C++(just a little), Javascript, and now heavy into GLSL shader programming. I am so rusty on BASIC though that I can only read a little of your source code and actually know what's going on, ha.

As far as the scenes go, they are really cool looking! I especially like the 3 spheres scene with its blueish, purplish colors and with the spheres' different reflection properties. I noticed that you have some fireflies here and there on each of the 3 scenes which brings me to my suggestion for maybe future exploration.

I couldn't tell from the source, but are you importance sampling your material BRDF's? I don't know how much you already understand about path tracing sampling, so forgive me if you already know this, but the scenes will converge much more quickly if you send reflection rays only in important directions, based on the specular lobe of the material roughness. So, starting with the easiest BRDF, a mirror or perfectly smooth metal surface, you would just send rays in the perfect mirror reflection direction and you get instant convergence. Then usually at some point programmers add perfectly diffuse surfaces (painted room walls, chalk, etc..), and for those BRDF's, you want to sample the CosineWeighted lobe, due to the Lambert Diffuse law, which states that light reflectance is strongest in the direction of the surface normal and falls off from there as a product of the cosine of the angle between the reflected ray and the surface normal.

Then lastly, we have every material in between these 2! (ha) -like your glossy ground material in the 3 sphere scenes. These are the trickiest, because a little more math work is involved in finding the important directions to send the reflected rays. It turns out there is something called sampling a powered (pow) cosine specular lobe. This is ideal for classic Blinn-Phong type materials that can have both roughness and a highlight at the same time. The sampling function has some of the same elements as the earlier ideal diffuse sampling (the cosWeighted sampling), however it adds a pow() function to sharpen the lobe of possible reflected directions according to how rough you want the material to be. The higher the exponent, the sharper the reflection, all the way up to the perfect mirror - the lower the exponent, the more blended or diffuse all the rays become, until you reach all the way back to perfectly diffuse (cosWeighted).

You can even go further and make a function for importance sampling a more modern microfacet material such as the SmithGGX specular, Beckman, Oren-Nayar diffuse, etc. that are all the rage these days in PBR workflows (both supported by Unity and Unreal engines for example). They actually sample the mathematical distribution of the tiny microfacets. These are really hefty mathematically though, and I have yet to try implementing real-time sampling functions for those (there are a couple of tutorials out there on the web, but they are really complex, still over my head, lol).

To see the powered cosine specular lobe sampling in action, check out my Material_Roughness demo Choose 'Copper Metal' from the material presets menu.

Also a great example that I don't fully understand yet because it involves the earlier mentioned microfacet material sampling is this brilliant guy's ShaderToy demo
It converges even better than mine (I'm jealous, ha ha). Click on the BufferA tab to see all the hefty BRDF math behind this awesome demo.

If you're interested, or get stuck somewhere with all this BRDF importance sampling stuff, I am glad to try and help (as much as I understand myself), and I also have some educational links floating around my browser bookmarks (which number in the 1000's now, ha).

Edit: forgive me, I missed these lines in your source: https://github.com/dafhi/2016-FreeBASIC-path-tracer/blob/master/pathtracer%20x.bas#L841-L859 so you obviously are aware of some of my earlier points. Hope you enjoy the ShaderToy demo though - that would look great on the ground below your 3 spheres!

Thanks for sharing your project on GitHub and keep up the good work! -Erich

dafhi commented 4 years ago

Wow cool write-up! Seriously I am flattered you'd take the time to check out this thing out.

I remember typing in some C64 code for the SID chip and out came a modulated, blitzy, fading sound effect. That's the extent of my early programming career. Well then I had some apple basic in high school ('90 -'91) where I learned about quicksort & shellsort, LOGO .. quicksort kind of blew me away but I didn't have a clue about how it worked. In college I took a c++ course but the instructor didn't approve of my lackluster efforts and gave me an F which left me with a bad taste for c++ in general. Also I don't like semicolons

But path tracing .. now there's something that has got me off my c++ butt and I attempted getting something to work. I got Kensler's pt to work .. I can't remember how. There was another one recently but I changed some setting in that IDE from 2016 and now nothing works.

That's why I like FreeBASIC. Have been considering JS for a couple years but just can't find the vibe. Think I'm just used to my development process. Mostly I'd like to get a new PT up; it's taking me ages to complete just the first of the 3 Peter Shirley's. In the example where he shows emulating a hollow glass sphere using a negative radius - didn't work for me. Didn't think too much, kept going up to the example with scene slightly zoomed out, and my image was way off so I thought it must be my vector class so I started over. Didn't feel like peeking over boilerplate and don't mind re-starting a good book. I'm on my third attempt because of floating point errors which "didn't exist" in my first attempt. But now I see the noise in that first attempt (Shirley's version) and realize that I was compiling GCC back then.

So now I'm trying to get a handle on why Snell's law works.

13 or so years ago I spent hundreds of hours trying to figure out why Euler angles work, which I did. Pretty much groundwork for being able to construct new angles. So it looks like there is much in common with my scattering function, and Snell's law, except I still have trouble b/c he (or she) doesn't use Sin or Cos. Total badass.

But I did figure out my scatter function, and it's totally ideal, but perhaps not as advanced as this "lobe" thing you mentioned. More on my scattering function later.

As far as fireflies go, you can really see them pop .. around line 968 change MULTI_TRACE to "1." The run-of-the-mill Russian Roulette simply creates firelies, not necessarily b/c of bright light sources, which is interesting, but the whole concept of fireflies in general led me to really try and tackle extreme lights, which is what "test scene" is about. And that's where my weight randomizer comes in. With the techniques I know at least.

"so forgive me if you already know this, but the scenes will converge much more quickly if you send reflection rays only in important directions"

Yeah I'm clueless here =) I may look it up, or you may feel like elaborating.

So my importance map is pretty basic. Line 999 is "dcol" function which simply returns the delta between 24 bit rgb. So when there's a big change in pixel brightness, well there you have it. Combine that with my "star filter," the caustics get spread a tiny amount.

Okay, yeah, love the whole idea of microfacets and all that. Great shadertoy link btw, and thanks for sharing your link as well. Nice Cook-Torrance [(https://www.shadertoy.com/view/XsXXDB)]

Also, [https://www.youtube.com/watch?v=KlEiztyfgHU&t=2575s]

Back to the subject of "microfacets," my .. well all PT's should .. but my scattering technique is novel, I think. In fact I emailed Peter Shirley and he replied back =)

In case my in-code comments don't fill all voids, I'll try an illustration.

So just a normal reflection, if you move the ray 5 degrees, the reflection also moves 5 degrees. But if you move the normal, the reflection moves 10 degrees. This isn't necessarily what I was thinking at the time, but it's a good way of explaining the 90 degree cone; there's your 180 degree scatter.

See, a ray coming straight down, meeting a perturbed 45 degree normal (as Peter Shirley puts it) shoots parallel to the ground.

I'm going to make quick changes to "3 spheres" and then look for maybe some interesting weights.

Thanks for all the suggestions / search terms. If you feel like going more in-depth with some of these concepts, by all means do!

erichlof commented 4 years ago

@dafhi Hi again! Thanks for your backstory and the PT clarifications! From your history, we might be around the same age, I am 46. My first experience with a home-style PC was at school also (although my experience was in elementary grades), with a LOGO program running on it. We only had 1 computer in the whole darn school and we had to share - it was in the 'science' room. With some kids, me included, you had to practically pry our fingers off the keys if you wanted your own time on the classroom computer, lol. This was in 2nd grade. Then one day my Dad came home with a brown paper grocery bag (he was not the shopper for the family so I was puzzled). He pulled out of that bag a used Atari 2600 with 2 joysticks that he got for a really good price at the local pawn shop. It came with 2 or 3 cartridges like Pitfall, Pong, and the 2600 version of Donkey Kong. I was glued to that thing for weeks, lol. We later got the Commodore 64 when I was in Junior High, which really solidified my interest for computers, because you could not only play games, you could make the computer do your bidding with BASIC! (Bwaa-haa cue maniacal laughter).

I was sorry to hear about your negative experience with that college teacher and C++. You know all it takes is 1 really good teacher (like my high school Algebra teacher, who got us all fired up about the potentially most boring math and variables - but he somehow made it pop) - the flipside is that like your unfortunate case, all it takes is 1 dispassionate teacher to leave a bad taste in your mouth about a subject (like my Physics teacher who quit mid-semester), a subject which could be an exciting subject in the hands of other teachers that really care and who are passionate.

My humble suggestion is that if you want to go down the road of C++ once more, actually start by learning your way around C first. I really enjoyed my time spent practicing C - it's a language where you get exactly what you put into it. And Javascript looks a lot like C - it was comfortable for me to ease into Javascript with a little C syntax knowledge. I never really got on the C++ object-oriented band wagon, it just doesn't make sense to me. However, it is possible that you can use some C++ features like operator overloading and modern string operations (which are a pain in old-style C with pointers and such) by just saving the project as .cpp instead of .c - but you can still program in bare-bones C style. The compiler doesn't care, ha ha. If you're interested in an older ray tracing book that uses minimal but helpful C++ features, while retaining old-school C basic principles, definitely check out Kevin Suffern's Ray Tracing from the Ground Up Since it's not in print anymore and quite expensive, there are some online PDF versions floating around the internet if you do some digging. ;) Also, here's a great resource for learning general PT/Ray tracing subjects like Snell's Law, Fresnel reflectance, BRDF sampling, etc. It's a website for students of Jacco Bikker (of Brigade PT fame, which is now owned by the Octane renderer company OTOY) and his colleagues at a European university: class link Scroll a ways down until you reach the Downloads section, and the Lecture Slides section in particular. Here you will find 16 different PT Lecture presentations in slide format (some of which I used in my project, and other parts solidified my understanding of some tricky concepts). These will do a much better job explaining than I can here with just text.

Enjoy, and good luck with the updates to your PT! -Erich

P.S. Thanks for the Cook-Torrance Shadertoy link - I actually had that one bookmarked as well! Ha ha And that video clip was awesome - very inspiring! Thanks

dafhi commented 4 years ago

Oh man. That first paragraph was riveting. I'll be 46 in March. In my school we had about 18 decent Apple II's .. at least 1 GS and a Mac

The C++ professor in question was actually very good and quite brilliant and I completely respect his decision. He had a Ph.D. in .. math maybe, I forget. But he worked for IBM at one point and gave 1 guest lecture in my differential equations class about .. FFT's I think. My diff equations professor was another Ph.D. He was one of the greats.

I'm not much of a reader but Shirley's course contains enough bite-sized elements and has an elegance that beckons. I'm learning new programming styles this way. One FreeBASIC community member in particular has an interest in programming models. So my new PT is going to be more maintainable. After I complete Shirley's first of three I'm not sure where i'll move on to - (hopefully) picking up on JS, or the next two books.

[edit:] Jacco's course looks exciting!

erichlof commented 4 years ago

@dafhi Hey, that's pretty cool that we are almost the same age! Wow your school was way more technologically advanced than mine! lol I think by Junior High is when we got a couple (like 3 maybe) Apple II's . One of my favorite games on that was Artillery, where 2 players take turns on a randomly generated mountain landscape to try and knock one another out, guaging wind direction, wind speed, cannon angle, and cannonball velocity. I might make a path traced version in the future, I liked that game so much!

Sorry I missed the point/circumstances of your story about your C++ professor. Yes I have had teachers like that as well. Knowledge is sometimes a double-edged sword I guess - the teacher can be almost too brilliant where you don't feel that you can ever be up to the task as his/her pupil. Speaking of C++, to tell you the truth, it's not as necessary these days as it was 10 or 15 years ago. The whole world it seems is moving toward languages (some reluctantly, ha) that work well on the Web and work on a lot of different platforms with minimal friction. Javascript happens to fit the bill in almost every situation. Although it gets criticized for being created in only 10 days, it is surprisingly expressive and robust. I have yet to find a project that can't be done using pure Javascript. It is reasonably fast now with Node/v8 as the backend, and it is flexible enough to allow the programmer to code in an imperative style (my preferred method), an object-oriented style, or a functional style. Not many languages can claim that. There are tons of getting started videos and free classes on YouTube if you ever want to go down that road. Best of all, semicolons are optional! lol

That C/C++ ray tracing from the ground up book I mentioned is loaded with inspiring, artistic renderings as well as helpful little code snippets, even more so than Shirley's texts. If you want a gentle intro to C ray tracing, I can't think of a better book. I don't know who said it, but there's this quote:

"If you want to learn a new programming language, write a simple ray tracer in it. A ray tracer's compact form needs everything that you can get out of a language."

This is so true, to get a ray tracer going you need basic variables, different size/precision options of those variables, arrays, most math operators, basic looping, maybe recursion, subroutines/functions, a way for input and screen output, etc., etc. Like you, I am more of a visual person than a dense-textbook kind of person. So I think you'll enjoy those PT class slides that I linked to - they are professionally presented, with helpful diagrams and beauty renders along the way to keep the class students interested, ha ha!

dafhi commented 4 years ago

I looked at a few of the chapters - also a very readable style. Programmer at FreeBASIC forum who's into programming models likes the 'SOLID' technique .. the first character, for Single-Use principle looks like it is adhered to there. Basically it means "keep a class uncluttered by functionality you potentially don't need"

So for example to initialize a graphics window, .set_dimensions w, h .set_background_color RGB(0,0,0)

In FreeBASIC you have a bunch of parameters on one line. A matter of taste I suppose but I can see where certain viewpoints have merit.

I'm definitely interested in JS. I'm so wanting to get a new PT off the ground I just kinda want to stick to my current platform, but I think next on the list after .. (all 3 of Shirley's initial books?) .. might be a quick look over the slides from Jacco's material .. I noticed he has a zip file containing framebuffer access. Trying to plot a pixel in some kind of c++ project space has been one bane of my existence.

But anyway yeah I'm definitely going to check your project out. It's just intimidating at the moment b/c I'm used to very small projects.

I have a couple more stories.

I remember the artillery game you were talking about, Carmen Sandiego and Oregon Trail.

One of my friends had a C128 which was pretty sexy. About 1990 my dad and I went to one of his friend's house I believe, and his son had .. an Amiga ..

I couldn't believe what I was seeing. Well, in a way I could, because I'd also seen the NES, but this was next level. Anyway it wasn't long after, my dad was also pretty sold on the thing - we could both feel it in MY future, I had one. An A500. Later I got an upgraded ram mod, an Amiga 3000, "Turbo Silver" and later "Imagine" by Impulse. They had the funniest newsletters.

Haha right now I'm listening to chip music.

I brought my A3000 to college and made a few animations for one computer class. Someone in the computer lab some time later had Caligari loaded on one of the PC's. With the Amiga I made a 3D animated text intro for one of the dance promo videos. (hosted by Sophomores? I forget, but I was in the group)

The promo was shown on the big screen, the whole campus (3-4K) saw it, and when the 3d stuff came on there was a hushed "wow" from the audience, like "we've officially entered the future."

I have a friend I grew up with who also went there for 2 years (me: 4) - we both wanted to work for Disney, but I believe he went into the animation industry, sent me an animation made with SoftImage (super expensive program at the time) on VHS - I still have it.

Another story:

My first job was at a computer store. I assembled PC's, tested, shipped, did RMAs. One day someone had me vacuum the toner out of a printer so I took it in the back room, turned on the vacuum, let it whir up, then started on the toner. There was some crackling as the printer, vacuum, and myself got hit by static. I quickly turned off the vacuum.

Well have a good one

erichlof commented 4 years ago

Cool stories! Thanks for sharing. That's so awesome that you had an Amiga. Those were one of the best home computers (if not THE best) of all time. Just about the time those were popular, the IBM clone wars was in full effect (ha), and since my parents had a small business and because I had to occasionally type out a school paper, we ended up getting a boring IBM clone with a green monochrome monitor and a simple word processor as our household computer (bleh). I still hung on to my Commodore but it was starting to show its age (late 80's early 90's). My younger brother got a slightly better PC with DOS that had the ability to do color graphics and DOS games, but by then I was off to college, '91-'96 to study music (I am a professional musician and music teacher btw, don't think I mentioned that). Fast forward to after college , '97-'98 and now my parents' and brother's PCs were much more capable and I sort of got back into programming as a hobby. Been doing it ever since (sometimes more involved some months than others along the way), but it has been my passion hobby I guess you could say.

That was a funny story about your first job, ha ha. That's cool that you know your way around computers and parts and such - that's a good skill to have. I just sort of turn mine on and try to get it to do my bidding by entering code, lol.

I'm going to search around for that PDF of the Ground Up book I mentioned, and if I find it I'll send you a link. Kind of knowing what scenes and images you prefer, I'm sure you'll enjoy this classic book. Every single image is made with the Ray/Path tracer from scratch. All the way from a black and white plane, to a downtown building scene with reflections, refractions, and GI, towards the last chapter. Even the simple sphere on plane scenes in the earlier chapters are really artistic and inspiring.

Talk to you later!

dafhi commented 4 years ago

Haha ..

you know, what I remember about building PC's as being funny is the idea that being an electrical device, you have to be careful with the innards. But quite often a card would really need to be worked into a slot, and the motherboard would bend. A lot.

That's awesome you're a music teacher. I wish I could find the code for that sound effect. Recently I came up with an algorithm with a similar sort of modulated complexity. I call it period modulation" .. https://www.freebasic.net/forum/viewtopic.php?f=7&t=27061&p=252934

I'm gonna see if I can find that C64 code

erichlof commented 4 years ago

Yes very true about PCs needing extra care with electricity! - ha ha. My younger brother likes to build his own PCs and he told me the story of his best friend in high school (I guess late 90's) who paid thousands of dollars for the latest greatest computer parts (back then). He had all the parts fresh out of the boxes and packages that he had mail ordered, he put everything together meticulously, then when it was time for the motherboard installation, he had walked across his room's carpet to pick it up and when he went to pick it up, it made a big shock and spark. When he later turned his fancy homemade computer on and tried to boot it up for the first time, it was dead as a door-nail. Luckily, he was well-to-do, so he ended up just buying another motherboard. But to this day, he wears rubber gloves, lol!

Here is the book's website: Ray Tracing From the Ground Up Resources where you can find all the higher res images used in the original print book (some of them are quite artistic), texture images, even full motion raytraced videos made with the ray tracer!, as well as C++ source code for each of the chapters, including all the 'headers' (.h files) that are needed to get started (stuff like output to PPM image text files, displaying gamma corrected colors, basic IO, math operations like cross and dot, etc.).

dafhi commented 4 years ago

got it! very thoughtful, thank you.

Just finished a camera class based upon Shirley's but converted to a style Suffern would use. Now I needs to debug it

erichlof commented 4 years ago

Great! Deleting that part of my old comment... I'm glad you can use some of the new materials! Hopefully you will be inspired by some of the cool images and diagrams. If anything, they will provide a more thorough understanding of the vast amount of ray tracing subjects. For me, there's so much still yet to learn, and I've been at my project since 2015, lol! Hope you get some useful tidbits, enjoy! I'll check out your updates soon, gotta go to work! Will be back later tonight ;-)

dafhi commented 4 years ago

Think I'm moving to JS. Three has some amazing examples. Probably stick to 'offline rendering' for a while

erichlof commented 4 years ago

@dafhi Cool! There's a ton of quick get-up-and-running Javascript tutorials on YouTube. I think you'll like working with it.

Just a quick note, the most confusing thing for me coming to the language for the first time was that whole functions with all their statements and parameters and returns, can be thought of as a single object. Functions get first-class seating in this language. Also since they are considered to be just variable objects, they can be passed around by their name as parameters inside the arguments for another bigger function. A prime example is the notion of a 'callback' function in Javascript. It's a function that is passed in along with the other parameters of the bigger outside function, and it is only executed when you call the outside bigger function, or when something happens on the webpage asynchronously like random user input such as key presses or mouse clicking, or a file finishes downloading. That concept of treating functions like variables is what took the longest for me to wrap my head around (in a way I'm still trying to come to grips with it fully, ha). But just to get started writing Javascript, you're not going to see or need that kind of language features yet.

Best of luck on your JS journey!