Basic raytracer in 30min C++

Поделиться
HTML-код
  • Опубликовано: 4 апр 2016

Комментарии • 119

  • @MarcusMathiassen
    @MarcusMathiassen  7 лет назад +31

    Might be a bit hard to follow, so here's the full code.
    github.com/MarcusMathiassen/BasicRaytracer30min

    • @offbeatstuff8473
      @offbeatstuff8473 3 года назад

      You could just have put that in description. Try adding time markers to the video like 9:30 Something

  • @nintendoblood4835
    @nintendoblood4835 4 года назад +67

    In case anyone is confused, he is using pure C++ with fstream library (file stream - useful for creating and working with files of all types) to create a .ppm (portable pixel map / a very inefficient and uncompressed image filetype) file containing information of a sort of "snapshot" created using ray tracing techniques. The console is not a renderer, so when you compile this code do not expect to print the image. The code compiles and generates the .ppm file from source, which you can view with something like gimp or photoshop or fileviewer or most text editors.

    • @domsau2
      @domsau2 3 года назад +3

      PPM is a very simple and easy ASCII format. I love it!

    • @yugiohonline26
      @yugiohonline26 3 года назад

      How would you print out the file in Windows?

    • @nintendoblood4835
      @nintendoblood4835 3 года назад

      @@yugiohonline26 What do you mean by print out?

    • @nintendoblood4835
      @nintendoblood4835 3 года назад

      @@yugiohonline26 Imagine this file type as a huge 2d array of color values. This code is programmatically generating the ppm file with a raytracing algorithim.
      If you want to view the file, open it with some software that can view ppm files.

    • @yugiohonline26
      @yugiohonline26 3 года назад

      @@nintendoblood4835 Ok when I run g++ test.out or a.out it doesn't create a ppm file at all. Could it be because I'm on Windows?

  • @simracing8055
    @simracing8055 3 года назад +10

    This is very impressive. If you could make a video explaining line by line, that would be awseom

  • @abhishek.chakraborty
    @abhishek.chakraborty 5 лет назад +4

    Thanks for sharing the code and such an illustrative video 😊
    Hope you'll keep sharing such amazing stuff 👍

  • @SoulssWeaver
    @SoulssWeaver 5 лет назад +36

    The silence is killing me! But awesome video!!

    • @zuheyr1
      @zuheyr1 3 года назад

      Maybe he is mute, no tongue? He could have shared the code and explain it rather than going "silent life"

  • @LuRybz
    @LuRybz 7 лет назад +41

    WOW!
    Never tought that from pure and clean code would be possible to render something.
    This is really awesome!

    • @titanarmy4116
      @titanarmy4116 7 лет назад

      not really, cameras do it with physical light, easier than this

    • @addvector4918
      @addvector4918 7 лет назад +3

      Games usually use baked lighting where the vertexes of a mesh control how light is distributed across an object. As oppose to ray tracing which is a real time lighting option, but not the only solution.

    • @over_complicated542
      @over_complicated542 4 года назад

      @@maxbd2618 I think L means that no graphics libraries are used

  • @younghsiang2509
    @younghsiang2509 4 года назад +1

    This looks so good! Thanks for the tutorial...

  • @carlosj1888
    @carlosj1888 6 лет назад +2

    Marcus these are amazing videos, thanks for helping

  • @comvisblog4825
    @comvisblog4825 5 лет назад

    Thank you... great

  • @IoriBernstein
    @IoriBernstein 5 лет назад +1

    Thank you very much man

  • @darthvader8469
    @darthvader8469 4 года назад +2

    Eres ua bestia amigo :3 Wou justo yo buscando cosas sobre Ryatracong y tu pones este video. Perdon si no hablo ingles xd

  • @lanchanoinguyen2914
    @lanchanoinguyen2914 Год назад

    Real time ray tracing is what we want,ray tracing is simple.

  • @TheFozze
    @TheFozze 4 года назад +1

    How would you go about making a bounding box? i cant figure it out for the life of me and all the other "tutorials" ive seen use much more complex and messier code making it hard for someone whos new to the whole raytracing thing to follow along and actually understand whats going on.

  • @_i_n_it_3099
    @_i_n_it_3099 7 лет назад

    Thank you
    Here my calcul of intersection who is a bit more precise
    if (discriminant < 0) // no intersection
    return (0);
    else if (discriminant == 0) // 1 intersection
    t = - 0.5 * b;
    else // 2 intersection
    {
    discriminant = sqrt(discriminant);
    t0 = (-b - discriminant)/2;
    t1 = (-b + discriminant)/2;
    ray->t = (t0 < t1) ? t0 : t1;
    return (1);
    }
    return (0);

  • @bassbatterer
    @bassbatterer 5 лет назад

    If you're having an issue where this line: frameBuffer[y][x] = (blue + white * ft); causes the file format to not work but this: frameBuffer[y][x] =white; worked then the issue is that the file format may not be openable (with you're image viewer) if it has floats in it, so to fix it where you're writing the colours to the out file EG:( out

  • @user-kc2eb1ib7e
    @user-kc2eb1ib7e 4 года назад

    How located coordinate axises? X-axis to viewer, Y-axis - right, Z-axis - up?

  • @rayman0326
    @rayman0326 5 лет назад +4

    27:27 so creepy that eye

  • @rawwars
    @rawwars 4 года назад

    if you dont comment whats the difference from simply read the completed code?

  • @losdel21
    @losdel21 7 лет назад +2

    Nice video!
    For some reason, with the same parameters for the light and sphere, i'm getting different lighting. It's just a grey to white gradient. Any idea what this might be?

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +3

      Make sure the output file extension ends with ".ppm" and that it writes "P3" at the beginning of the file. You may have "P2" and or ".pgm" which is grayscale.

    • @losdel21
      @losdel21 7 лет назад +2

      Thank you for the fast reply. I was unclear with my question. The colors are allright. I meant i'm getting a different lighting pattern for the same light position and sphere. I'm using different code for the vector operations, but i doubt that is the problem, they produce the same outcome.

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +2

      Could you link me a screenshot of what you're seeing?

    • @losdel21
      @losdel21 7 лет назад +1

      imgur.com/a/2OFPB

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +1

      Is that the right screenshot? It doesnt have any gray to white gradient and no pattern. It´s solid red.

  • @jpalacios117
    @jpalacios117 3 года назад

    "Lemme just feed this object into this shit ton of linear algebra"

    • @particlecat3142
      @particlecat3142 3 года назад

      Pretty much describes most low level graphics programming.

  • @oussamamoussaoui7131
    @oussamamoussaoui7131 2 года назад +1

    One thing I didn't understand which is why you made the direction vector of every ray constant (0,0,1), I think it must be calculated as the direction from the camera towards the pixel. I'm I right or wrong?

    • @MarcusMathiassen
      @MarcusMathiassen  2 года назад +1

      You’re right. I think I made some type of faux orthographic raytracer but honestly I don’t remember.

    • @oussamamoussaoui7131
      @oussamamoussaoui7131 2 года назад

      @@MarcusMathiassen okay, thanks!

  • @rj-roblox7626
    @rj-roblox7626 2 года назад

    RTX ON

  • @sruthisrinivasan3622
    @sruthisrinivasan3622 2 года назад

    I ran the code in visual studio code and a ppm file "out.ppm" got generated. But it has nothing in it. It shows 0 bytes. Could you please tell what might be the error?

  • @FlyToTheEarlyGame
    @FlyToTheEarlyGame 5 лет назад +2

    Discriminant formula is wrong, should be : disc = b*b-4*a*c not disc = b*b-4*c and therefore "a" should be : a = dot(d,d); But using dot product is a great idea, was thinking about how I could possibly write those long formulas for "a","b" and "c". And shouldn't t0 be (-b - sqrt(disc))/(2*a) and t1 be (-b + sqrt(disc))/(2*a) instead of just (-b +- sqrt(disc)) ?

    • @diamondglitter205
      @diamondglitter205 5 лет назад +4

      a = dot(d,d) = 1. it's not gonna make a difference because d the direction vector is normalized and it's length is just 1. but the other formula should be divided by 2 because 2*a = 2;

  • @israelRaizer
    @israelRaizer 8 лет назад

    Very cool! How difficult would it be to implement a GUI for this?

    • @MarcusMathiassen
      @MarcusMathiassen  8 лет назад +2

      Using xcode´s interfacebuilder it shouldnt be too hard.

    • @adam7868
      @adam7868 7 лет назад +2

      not "childs play" on wiondows

    • @adam7868
      @adam7868 7 лет назад

      +Marcus Mathiassen did you use external libraries to get this to work because I saw that you called a ray and vec object

    • @adam7868
      @adam7868 7 лет назад

      +Adam7868 my bad I rewatered it

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +1

      Yeah, no it's all there. No external libraries.

  • @sk-ov2pq
    @sk-ov2pq 4 года назад

    When calculating the discriminant, isn't the formula (-b +- sqrt(b^2 - 4ac))/2? If so, why do you not divide by two in Sphere::intersect?

    • @inlandish
      @inlandish 4 года назад +3

      Oh he just remembered a little bit of stuff from others videos and made a "tutorial" out of it, the dude does not actually understand what he is typing

  • @adamtaylor5859
    @adamtaylor5859 7 лет назад +1

    Around the 14 minute mark when you first get the sphere rendered, for some reason my intersection is always returning true, resulting in a completely white image, any ideas?

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +1

      hard to say without seeing the code. Could you link it?

    • @adamtaylor5859
      @adamtaylor5859 7 лет назад

      I've checked it over and I'm 90% it's the exact same as you have at that point, but I've copied it here: pastebin.com/2nNdbVgE thanks :)

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +1

      line 9 : Vec3(double a, double b, double c) { x = a, y, b, z = c; }
      You forgot to set 'y' :) should be y = b.
      Also if you're using visual studio you should have gotten a warning telling you about it.

    • @adamtaylor5859
      @adamtaylor5859 7 лет назад +2

      You are an absolute legend! Somehow I didn't get that warning :/ Thanks though! I can continue with the tutorial now :D

    • @adamtaylor5859
      @adamtaylor5859 7 лет назад

      This is so frustrating! I get up to the 23 minute mark and it happens again! It looks like my code matches yours but I have different output! pastebin.com/XqAZuM24
      Please help!

  • @CookedChicken1
    @CookedChicken1 Год назад +1

    Hmm why my .ppm file does not exist anywhere, please help

  • @jean-naymar602
    @jean-naymar602 7 лет назад +5

    There's something I don't understand.
    You wrote : t = b-disc.
    Isn't this supposed to t = (b-disc)/2 ? Well it is, i'm sure about that, but is that an error you made ? Or is it on purpose ? Because it seems to work.

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад +3

      You're right! That was an error. You're supposed to divide by two.

    • @jean-naymar602
      @jean-naymar602 7 лет назад

      So does that mean that the sphere appears to be farther than it should ?

    • @TheEddzifyz0r
      @TheEddzifyz0r 5 лет назад

      It's an error but the result is the same because t0 < t1 is equivalent to t0/2 < t1/2.

  • @nmmm2000
    @nmmm2000 7 лет назад +6

    Recently for a project I checked 2-3 "crap" ray-tracers. This is the simplest / best one I am going to use.
    I ported everything to C++11.
    Put lots of consts and also optimized a bit.
    Get rid of C style pass by pointer and changed it to pass by reference.
    Get rid of the std::vector as well.
    I can see some other optimizations with:
    double const dt = dot(L.normalize(), N.normalize());
    But I guess the optimizer will take care of it.
    I got the source from one of the other comments from Izmael.
    gist.github.com/nmmmnu/65f370578d9f7c8002b88bbd1ac693ea

    • @adamtaylor5859
      @adamtaylor5859 7 лет назад

      Please can you send me that code?

    • @nmmm2000
      @nmmm2000 7 лет назад +1

      sorry link was not pasted, I edited the reply
      gist.github.com/nmmmnu/65f370578d9f7c8002b88bbd1ac693ea

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад

      This is probably the simplest, but it's also a hack. The "camera" is flat and has no perspective (that's why it's simple). So if you really want to use it for a bigger project you should look up camera rays.

    • @nmmm2000
      @nmmm2000 7 лет назад

      I noticed this already :)
      I want to render this: www.flickr.com/photos/nmmmnu/32615496144/
      In order to avoid creating millions of objects (this current image have 200K or so objects), it needs to change the code of the ray-tracer.

    • @Lmao-ke9lq
      @Lmao-ke9lq 4 года назад

      @@nmmm2000 are you stil active in this project?

  • @PauloDutra
    @PauloDutra 4 года назад

    Shouldn't the ray origin be 0,0,0 and ray direction x,y,1 ?

    • @MarcusMathiassen
      @MarcusMathiassen  4 года назад +1

      I think this is more of an orthographic raytracer, so you can use your own "camera" if you'd like. Though there really are better videos than this. Use it more as a guide than anything.

  • @robanator1617
    @robanator1617 7 лет назад

    I'm following this video, and for some reason the lighting on the top of the sphere is reversed... any ideas?

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад

      Check the getNormal function. It should be "(pi-c)/r" and not "(c-pi)/r".

    • @robanator1617
      @robanator1617 7 лет назад

      Now its the other way around!
      gyazo.com/1e9da70aaa9b28a255946783cb402ad9

    • @MarcusMathiassen
      @MarcusMathiassen  7 лет назад

      Looks like the RGB values are going over 255. Just cap them between 0-255.

    • @robanator1617
      @robanator1617 7 лет назад

      Now I'm getting this gyazo.com/cfc184cbeab8a07f5c38824a206daea8

    • @robanator1617
      @robanator1617 7 лет назад

      If it has anything to do with t, turns out t is always t1. Here's my code for that
      pastebin.com/65BXHpec

  • @farefouse
    @farefouse 5 лет назад

    basic raytracer in 30min and 4 years of learning programming.

    • @MarcusMathiassen
      @MarcusMathiassen  5 лет назад

      This was right after id taken 101 C++. So about 8 months.

    • @farefouse
      @farefouse 5 лет назад

      @@MarcusMathiassen ok 8 months and 30 minutes.

  • @nabelwangwe2192
    @nabelwangwe2192 4 года назад

    Hey Marcus, i followed through the video but when i run it, it generates an O file which cant be opened on Win 10.
    I did it on Code::Blocks. Please help.

    • @MarcusMathiassen
      @MarcusMathiassen  4 года назад

      Nabel Wangwe .o file? As in Object file? Pass it to «cl» to generate an .exe

  • @victornaut
    @victornaut 7 лет назад

    Which color scheme is this?

  • @user-fu1ie8dj8k
    @user-fu1ie8dj8k 7 лет назад +13

    NORM ERROR

  • @realcygnus
    @realcygnus 5 лет назад

    anyone know hot to do this on windows 10 ? I got cygwin & gcc installed but you when run the exe nothing happens

    • @MarcusMathiassen
      @MarcusMathiassen  5 лет назад

      realcygnus it should work on windows. How do you compile it?

    • @realcygnus
      @realcygnus 5 лет назад

      @@MarcusMathiassen Thanks for the quick reply. Actually it worked like a champ. At 1st I didn't even realize I was looking for the ppm file, & I had even looked right at it a few times but gimp hadn't automatically associated itself with the format until I used it once, hence no icon. Thanks, useful stuff especially perhaps along with ffmpeg.

    • @MarcusMathiassen
      @MarcusMathiassen  5 лет назад

      realcygnus Yeah, i used ffmpeg to stitch together 1000s of ppm files back then since i didnt know how to use OpenGL. Incredibly inefficient creating gigabytes of data for just a few frames of me zooming in on a mandelbrot.

    • @realcygnus
      @realcygnus 5 лет назад

      @@MarcusMathiassen Yea incredibly inefficient ! If you only need non interactive clips of a scene that your system can't render in real time, then I suppose it could fall under "useful" . Or at least my Integrated Intel graphics almost had me convinced. lol

  • @alva3413
    @alva3413 5 лет назад +1

    this video has no sound??

  • @titanarmy4116
    @titanarmy4116 7 лет назад +2

    So its an orthographic tracer. Why not just put a view frustrum in and call it a day?

    • @MarcusMathiassen
      @MarcusMathiassen  6 лет назад +1

      Because i didn't know how at the time.

    • @songlovers3426
      @songlovers3426 5 лет назад +1

      to understand the fundamentals. :)

    • @abandoned7501
      @abandoned7501 5 лет назад +3

      I'm already tracer

    • @1495978707
      @1495978707 5 лет назад

      Titan Army what do you mean by "put in a view frustum"? I understand using a projection to map a perspective frustum into the canonical viewing volume, but I don’t see how this is equivalent to an orthographic tracer, so I feel like I’m missing something. What would be different to do a perspective tracer

    • @nikeshpurohit7288
      @nikeshpurohit7288 5 лет назад

      "iamverysmart"

  • @alexanderskladovski
    @alexanderskladovski 5 лет назад +2

    Coding drunk?

  • @electrodeyt3491
    @electrodeyt3491 6 лет назад +1

    I made this:
    a) Output to a SFML Window (In Real Time!)
    b) Make this render Video
    c) Support many Spheres
    Still quite shit, but its better i guess.

  • @raulguerreroflores1460
    @raulguerreroflores1460 Месяц назад

    apple sucks !!!

  • @cheri758
    @cheri758 4 года назад

    who the fuck uses mac ?????????????????????????