Texture Mapping & Polygon Rasterizing Tutorial (1/2) [C++20]

  Рет қаралды 103,331

Bisqwit

Bisqwit

Күн бұрын

Textured polygons are the foundation of nearly all 3D games in existence. Used before even 3D-capable GPUs were a thing, they were rendered using nothing but software. How was that achieved? Let’s explore an easy and intuitive method. We create an extensible 3D polygon rasterizer using nothing but standard C++20 (and libSDL for 2D graphics).
Become a member: kzbin.infojoin
My links:
Twitter: / realbisqwit
Liberapay: liberapay.com/Bisqwit
Steady: steadyhq.com/en/bisqwit
Patreon: / bisqwit (Other options at bisqwit.iki.fi/donate.html)
Twitch: / realbisqwit
Homepage: iki.fi/bisqwit/
You can contribute subtitles: kzbin.info_vide...
Downloads:
bisqwit.iki.fi/jkp/polytut/
Music list (s = SPC-OPL conversion):
- Energy Breaker - Golden-colored Wind (s)
- Final Fantasy V - World 1+2 (s)
- Star Ocean - Past Days (s)
- Energy Breaker - Heroes’ Adventure (s)
- Final Fantasy Mystic Quest - Focus Tower (s)
- Final Fantasy Mystic Quest - Shrine (s)

Пікірлер: 342
@Bisqwit
@Bisqwit 4 жыл бұрын
The editor used in this video is Joe, and it is being run in _That Terminal._ DOSBox was not used, neither was _That Editor._ The compiler used is GCC 10.0.1, and run in Debian GNU/Linux. The source code is now up at bisqwit.iki.fi/jkp/polytut/ . You will need GCC 10, but with small changes it can be compiled with as early as GCC 7. ERRATA: The compile-time for-loop is not C++11, but C++20. It depends on fold expressions (c++17), std::index_sequence (c++14), and lambda templates (c++20). *A reminder: Do not reply to **_this post_** if you want me to see your comment. Post your words as a new comment, not as a reply, unless you are addressing the contents of **_this comment_** specifically. KZbin does not show creators new replies, it only shows new comments. If you reply here **_I will not see it_** unless I manually check for it.* If you are addressing a comment someone wrote, then you _should_ reply it, though.
@yakovdavidovich7943
@yakovdavidovich7943 4 жыл бұрын
Nice! I couldn't quite place it, but now you're triggering memories of a friend I had who was a big fan of Joe. It's nostalgic to see it again. I should go check it out again, and see how older-me feels about it!
@m1lkweed
@m1lkweed 4 жыл бұрын
I love your naming conventions
@TorstenSeemann
@TorstenSeemann 4 жыл бұрын
I still use joe for all my coding. I learnt the wordstar key bindings back in the Turbo Pascal days!
@ancientapparition1638
@ancientapparition1638 4 жыл бұрын
hahaha I love how people still ask
@chetana9802
@chetana9802 4 жыл бұрын
This game seems like what your audience might be interested in store.steampowered.com/app/1024890/Sectors_Edge/
@homeroni
@homeroni 4 жыл бұрын
20 years of knowledge condensed into 20 minutes of video.
@arduinoguru7233
@arduinoguru7233 4 жыл бұрын
Human brain ability on it peak .
@CT--od7ux
@CT--od7ux 3 жыл бұрын
thats true
@martysh1226
@martysh1226 2 жыл бұрын
Written in C++20
@dertechl6628
@dertechl6628 4 жыл бұрын
I really appreciate that you are up-to-date with current C++.
@pw1169
@pw1169 4 жыл бұрын
absolutely, it's so much easier to read /s
@ducksoop.x
@ducksoop.x 3 жыл бұрын
@@pw1169 I don't get whats so bad it about it honestly, the common complaint I hear is from Go/Rust fanatics who just keep complaining about C++'s use of iterators vs theirs.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 3 жыл бұрын
@@ducksoop.x I'm not really a Go/Rust fanatic, but I gotta admit C++ is getting pretty hard to read, I'd say more than Java even (yeah). constexpr __noexcept virtual auto fn(args) -> return_val {}, etc. (this is not correct code cuz I haven't coded in C++ in a loong time, but you get the point). It might be easy if you have been following it since a long time, but jumping in the middle, it is really overwhelming. Atleast its learning curve is much better than Rust's.
@echoptic775
@echoptic775 3 жыл бұрын
@@pw1169 found the redditor
@lolnyanterts
@lolnyanterts 4 жыл бұрын
Bisqwit made me rethink my entire life in two minutes when he said circles do not exist and are merely cylinders.
@petacreepers23
@petacreepers23 4 жыл бұрын
when you think you know a bit of c++ comes this guy and teach you 20 new things that completly blow your mind
@seanjackson6474
@seanjackson6474 3 жыл бұрын
The way you reduced the interpolation formula down to a constant increment is just genius.
@user-kn2wq9zl4y
@user-kn2wq9zl4y 4 жыл бұрын
14:41 my heart literally stopped beating for those 3 seconds
@Jackson-yr9ih
@Jackson-yr9ih 4 жыл бұрын
Винни Пух ты понял, зачем он ищет самую короткую сторону?
@josealvim1556
@josealvim1556 4 жыл бұрын
“Ultramarine blue”, 10/10
@jamesbalajan3850
@jamesbalajan3850 4 жыл бұрын
Great to see a new series start. I'm excited for part 2.
@Waccoon
@Waccoon 4 жыл бұрын
21:07 - "Note: A comma is not permitted here. It is a curious inconsistency, but the actual reason is: What I explained previously was a fold expression, while this is a pack expansion. These are two different features with slightly different rules. C++ is great, but it is not perfect..." What's also not perfect is when communities gloss over the strange syntax and kludges in modern languages, hoping not to scare away newcomers. This usually just causes novices to tear their hair out in frustration when they DO encounter such strangeness. I wish I had known at the start of my career that almost all language features are not the "right" way of doing things, but just clever hacks to force compilers to do things they were never designed to do, using a minimum of syntax changes and code rewrites. A forewarning (and apology) every now and then is a good thing. Thank goodness I don't do web development anymore. Modern Javascript makes my eyes bleed.
@Adolf1Extra
@Adolf1Extra 4 жыл бұрын
C++ itself is nothing compared to setting up a build system for C++. I don't know what that last paragraph has to do with anything, but JS is both simpler, easier to learn and build scripts are much much much easier to write.
@modeco80
@modeco80 3 жыл бұрын
Is 5-10 lines of CMake reeeeally hard enough to whine about? I think not. For simple projects, especially if you only want them to run on *Nixen easily, Makefile or even a shell script that glob's all cpp and c files in src/ is ok (albeit, since I actually try to write portable code, a major code smell right off the bat).
@araarathisyomama787
@araarathisyomama787 2 жыл бұрын
@@modeco80 But, nobody is getting paid for projects that only require 5-10 lines of cmake
@modeco80
@modeco80 2 жыл бұрын
​@@araarathisyomama787 That was an extreme example anyways. For most actual projects, there is a bit more (especially for setting C++ standards/things) but the point is that you don't need to start with huge flashy template projects which have at least 50% CMake and are multiple hundred kilobytes for the initial commit. Building things up as you need them is better anyways, because it lets you decide "Hey, do I really need ?" and it's a lot easier to get rid of it if you don't instead of having to chase around a bunch of code. CLion and VS's CMake tooling (which I consider the most flaming hot garbage after using CLion) pretty much do the rest (CLion even offers to give you boilerplate code to set C++ standards when creating a new project), and even offer to add files to any add_* commands (CLion and VS both do this at least, although CLion is a bit more respectful about formatting) and regenerate automatically. I still wouldn't complain profusely about CMake, mostly because the alternatives either aren't as good (do NOT tell me SCons is good) or fairly early in development (DDS has been on my eyes for a while, but it's still in development and there are some conventions DDS enforces I personally don't like all too much. I'd be willing to let it slide probably, but for right now CMake is something more people will have and I'd rather use it at the moment).
@digitalconsciousness
@digitalconsciousness 4 жыл бұрын
Man, I can't tell you how much I love these kinds of videos. I have tried to make a basic 3D engine just for fun, but could never figure it out, so I love stuff like this. Please make more of them. You have knowledge many of us don't.
@NonTwinBrothers
@NonTwinBrothers 4 ай бұрын
18:29 Years later and this still my go-to source for compile-time for loop. I still remember 2 years ago when it was all just symbols to me :D
@sporff
@sporff 4 жыл бұрын
This brings back lots of late night memories writing my own software engines. Except it was usually in C and inline assembly. No one talks about this stuff anymore since 3d hardware took over. I look forward to the rest of this series.
@pissmilker2313
@pissmilker2313 4 жыл бұрын
I remember making a 3d software rasterizer a good while ago. I found the projection matrix to be the hardest part to figure out. Can't wait to see your solution.
@Bisqwit
@Bisqwit 4 жыл бұрын
3D projection will be in part 2! Hope you will still watch this video :-)
@pissmilker2313
@pissmilker2313 4 жыл бұрын
@@Bisqwit Sure will.
@cleverking2003
@cleverking2003 4 жыл бұрын
That surely looks interesting! Looking forward to seeing more.
@MrFedX
@MrFedX 2 жыл бұрын
Such a clear and informative video on a subject that can easily get confusing! Nothing redundant, just exactly what I needed to learn to do this stuff. The sign of a true master teacher.
@Sturmtreiben
@Sturmtreiben 4 жыл бұрын
Great video. Thanks for the idea of the compiletime for loop. Good work and kinda easy to follow.
@transientwaveform1475
@transientwaveform1475 4 жыл бұрын
Good god, your videos are so dense with information. I love watching your stuff.
@glewfw7989
@glewfw7989 4 жыл бұрын
Bisquit explained 10 books and my entire course of computer graphics with that intro and I understood better thia video than the other ones I mentioned. Thank you so much! Please take your time for explaining some SDL functions!
@xemuth1643
@xemuth1643 4 жыл бұрын
God, so interesting I'm glad you take time to provide this kind of quality content. I thinks C++ beginner as me are lucky to have found your channel.
@mrshodz
@mrshodz Жыл бұрын
Loving your video already. This is the first one I have watched, I will watch a few more.
@Junior7setti
@Junior7setti 4 жыл бұрын
Please do more on videos about this! I really enjoy your videos about computer graphics. You make all those scientific articles about it much easier to understand
@gayusschwulius8490
@gayusschwulius8490 4 жыл бұрын
As someone who knew most of the things in this video already and has developed simple rendering engines himself, I still very much enjoyed your video. This would've been tremendously helpful back when I first learned all of this, and still today it is a very nice presentation of the core aspects of computer graphics. Also, I like the way you casually use C++20 in your projects, few people really do that.
@danielf.7151
@danielf.7151 2 жыл бұрын
this video made me try C++ again, I'm amazed at all the crazy things you can do with modern C++
@donmelaraya2218
@donmelaraya2218 4 жыл бұрын
I loved the "inefficient way" section of the video haha. I was doing the same thing when I was learning to program a software renderer until I heard about scanline rendering. Then everything was so much easier hehe.
@RunTheTape
@RunTheTape 4 жыл бұрын
Amazing thought process and explanation.
@GiveAcademy
@GiveAcademy 4 жыл бұрын
amazing work, always look forward to your videos! keep at it! and keep safe!
@gustavlouw3362
@gustavlouw3362 4 жыл бұрын
your graphics videos are the best you upload. please do more!
@serjicus
@serjicus 4 жыл бұрын
Polygon rasterization has always fascinated me. Nice Mystic Quest music.
@procactus9109
@procactus9109 4 жыл бұрын
Excellent, I'd love to see a lot more videos like this one, graphics and sound coding. In its elements.
@dakatailiev6811
@dakatailiev6811 4 жыл бұрын
Great video! Can't wait for the next one.
@Aleaths
@Aleaths 4 жыл бұрын
I absolutely love your videos. Keep it up!
@Ikkepop
@Ikkepop 4 жыл бұрын
Great video as usual, I missed your quality videos
@DanielWesleyKCK
@DanielWesleyKCK Жыл бұрын
Final Fantasy Mystic Quest is a nice touch.
@WarpRulez
@WarpRulez 4 жыл бұрын
Just skipping drawing eg. the rightmost pixels of the triangle is an easy way to avoid overlapping pixels of adjacent triangles. However, it doesn't give the nicest result for the right edge of the triangle when there's no adjacent triangle drawn there (eg. this is the visible right edge of an object). There's like an entire line of pixels missing there. In many cases (and with very high resolutions) it might not matter visually at all. In other cases it might (especially with certain textures with thin edge colors). The more "correct" way of deciding which edge pixels to draw is to see if the center of the pixel is inside the mathematical triangle or not (which can be done with a dot product). This is slightly more complicated and requires a few more operations (some multiplications and additions), but in principle gives a nicer result. Of course in this case there's still the problem of what happens if the pixel is _exactly_ on the edge of the mathematical triangle (which may well happen). In this case we need to decide whether to draw it or not, and change this decision for the other adjacent triangle. In this case we can use your simpler method: If it's on the right or the bottom, we don't draw it, else we do. But yeah, with a simpler renderer where it isn't crucial to draw triangles to the very edge, the simpler approach is viable. (I do not know what GPUs do, but I have the understanding that they use the better method I described.)
@KuraIthys
@KuraIthys 4 жыл бұрын
That's an interesting observation, and it did occur to me that just ignoring it would bring problems in some cases. However I would say that in most cases if your rendering resolution is low enough to for this kind of problem to be really obvious, then you're likely also dealing with a system that has performance constraints which make doing a dot product on every line of every polygon drawn an unjustifiable expense. Arguably in practice (for a software renderer) you'd probably be better off having a special case drawing routine and making the person using the graphics routines responsible for deciding which to use. Though, of course, it very much depends on why you're implementing these kind of graphics algorithms, and on what kind of hardware... For instance, bresenham's line algorithm, in typical implementation is full of divisions and multiplies. But you look through a book from the 80's or early 90's and you'll find variations of the routine that go out of their way to avoid having to do divides in particular, since those were really expensive for a lot of hardware of the time. (if you had such functionality in hardware at all it was often much slower than simple addition or subtraction. ) Context matters really. XD I guess I'm still stuck in the mindset of the era that led to the first Quake... Where you had a computer that was quite powerful relative to what was around before then, but where you had to optimise everything to death to even just stand a chance of getting decent performance out of it...
@WarpRulez
@WarpRulez 4 жыл бұрын
@@KuraIthys Even in modern hardware avoiding (or minimizing the number of) divisions can be beneficial because division usually takes like an order of magnitude more clock cycles than multiplication. (I think the fastest division implemented in modern CPUs takes something like 15-20 clock cycles. In slightly older CPUs it may take as long as 40 clock cycles. A multiplication, even a floating point multiplication, usually takes less than 5 clock cycles, the best implementations even taking just 1.)
@KuraIthys
@KuraIthys 4 жыл бұрын
Hmmh. I was not aware division was still that slow. I had thought both division and multiplication were largely single cycle operations these days. I mean, it was getting towards that (For multiplication definitely) even towards the mid 90's. In any event you can get away with using such instructions anyway without it killing you on a late 90's PC... But try it on an early 90's PC or any kind of 8 or 16 bit hardware and the performance penalty is pretty serious.
@WarpRulez
@WarpRulez 4 жыл бұрын
@@Jackson-yr9ih I don't really understand where you are getting that "shortest side" from.
@Jackson-yr9ih
@Jackson-yr9ih 4 жыл бұрын
WarpRulez well, alrighty. I have made it by myself already in python with pygame. Not actually all of the code, only triangle rasterizer using three dots and interpolation algorithm. By the way, do you know how to create random mash of triangles (groups of three dots) on the screen? Idk.
@3DSage
@3DSage 3 жыл бұрын
Really helpful and informative!
@SpookySkeleton738
@SpookySkeleton738 3 жыл бұрын
I was just wanting to make a 3d software renderer for bare metal x86, then I check KZbin and see Bisqwit just did a video series on the subject? My project is saved!
@underbrog
@underbrog 4 жыл бұрын
i thought i have a decent level of programming ... until this. I'm glad i find someone like you , now i want more knowledge :D
@xx_easyhdlpmcawpgod_xx7296
@xx_easyhdlpmcawpgod_xx7296 4 жыл бұрын
Love your videos!
@mairacristian54
@mairacristian54 4 жыл бұрын
This is why im subscribed to bisqwit
@silvertakana3932
@silvertakana3932 2 жыл бұрын
for Bisqwit to have such an amazing understanding of the polygon world, I think it's safe to say he is a polygon person too!
@Someone-ym1ny
@Someone-ym1ny 4 ай бұрын
Holy I was muttering random Japanese words and opened this video while being half-asleep. The intro was kinda surprising lmao
@0day694
@0day694 4 жыл бұрын
You are the best bisqwit it the world!
@TallaGrass
@TallaGrass 3 жыл бұрын
Bisqwit is a beautiful soul
@JoeBurnett
@JoeBurnett 4 жыл бұрын
I can’t wait to watch! Thanks for making such great video content. Have you considered making any training videos on Rust as well? It’s a great language, too.
@Bisqwit
@Bisqwit 4 жыл бұрын
I am not in position to teach any Rust, having only ever written one program in that language.
@mrshodz
@mrshodz Жыл бұрын
Thanks!
@davidgildegomezperez4364
@davidgildegomezperez4364 4 жыл бұрын
Awesome content.
@vaderasss
@vaderasss 4 жыл бұрын
The think I admire most is that you put a lot of work on each and every video to eliminate all errors and give perfect explanations!! In that context, I was about to comment that you could use std::apply to avoid the indexer which would give a simpler and more intuitive structure, but you just mentioned it the moment after!!Arrrghhhhh!!! :-) But I want leave the comment section without a fight ... 19:16 there is an ERROR ... “Function calll” there is one more l !!!!! hehe nice work 👍
@Bisqwit
@Bisqwit 4 жыл бұрын
Nice catch! Yeah, you can only use std::apply if you are going to iterate through _all_ values in the tuple/pair/array. It can not be used, at least not without much complication, if you only want to iterate through a subset.
@3DSage
@3DSage 4 жыл бұрын
... I had to learn this to program 3D graphics on the Game Boy Advance! Minecraft and Zelda. I added a raycaster tutorial which is like 2.5D. Very interesting.
@Y1001
@Y1001 4 жыл бұрын
Bisqwit be honest, are you spying on me? I've just spent the past 2 weeks debugging someone else's code that had it's own custom C++ scanline renderer & rasterizer lol Thanks for the video, actually makes everything even clearer for me.
@LuaanTi
@LuaanTi 4 жыл бұрын
What an amusing coincidence. I've just finished texture mapping in my latest software 3D renderer just a couple days ago :) My previous texture mapper only handled Build-engine style geometries (and the ceiling code was a mess), now I can do both, and with full blown (software) fragment, vertex and pixel shaders in 1080p, on one CPU core. Modern CPUs are mind bogglingly fast.
@Jackson-yr9ih
@Jackson-yr9ih 4 жыл бұрын
Luaan hello. Why do we need to find the shortest edge? I mean I get the idea of interpolation and all. We have three points in array or doesn’t matter where -> I need to sort all points by Y-value . OK. -> I find where is the shortest side: left or right. OK. -> but why I need to find short side? We got shortside Boolean variable - what is sides[shortside] = Makeslope(...) Where sides list has been defined? Where is the function? BTW I don’t know C++ language :(
@LuaanTi
@LuaanTi 4 жыл бұрын
@@Jackson-yr9ih Imagine the lines you have to render. You start at the top vertex, and go row by row, interpolating the left and right lines to form the left and right limits to the scanline rendering. At some point, you reach the Y of the _second_ vertex - now one of the lines you're following changes. You need to know whether that happens on the left or on the right - that's all. It's not necessarily the "short" side - for very oblique triangles, it can easily be the longest side. The only "short" thing about it is the Y coordinate.
@Jackson-yr9ih
@Jackson-yr9ih 4 жыл бұрын
Luaan, Oh dear, thank you, but I actually have already got it. And made this in python with pygame. Not ALL of the program. It can only rasterize a triangle from three dots with interpolation algorithm. But I really want to know how can I create a random triangles mash on the screen (groups of three dots). Do you know? Idk. :)
@LuaanTi
@LuaanTi 4 жыл бұрын
@@Jackson-yr9ih Don't try to randomly place triangles - just place the dots, and then connect them into triangles :) Or don't do it randomly at all; you're trying to see all the different kinds of triangles you have to deal with - list them, and put them on the screen. In my case, I just made a rotating 3D cube - that makes finding the broken edge cases pretty fast (though Bisqwit's version is much better for seeing how well everything works at a glance).
@Jackson-yr9ih
@Jackson-yr9ih 4 жыл бұрын
Luaan well, I would like to make the rotating cube too. I’m a newbie in all of this vector stuff. :)
@reexre
@reexre 4 жыл бұрын
Coincidentally, I was just trying to do this a few days ago.
@andreaangelici9751
@andreaangelici9751 4 жыл бұрын
I am trying right now to make a software rasterizer such as yours to try to render some 3d stuff by myself, but i lack of the basic math knowledge to do it so it is taking a very long time. Have you got any tips for me? Also, since all my experiments right now were made with ncurses (wich I used because it is simple to use and i'm comfortable with it), could you suggest me some easy but fast graphics libraries in c++ which i can use for displaying simple stuff (eg: plotting a pixel with a specific color maybe on a window wich is not the console)?
@doriphor
@doriphor 4 жыл бұрын
Love that Mystic Quest music! ❤
@zoeherriot
@zoeherriot 3 жыл бұрын
Worth noting that sotware rasterizers are still used in graphics engines today - not as renderers but intermediate steps for solving other problems.
@gasparliboreiro4572
@gasparliboreiro4572 4 ай бұрын
7:08 this interpolating ecuation is actually the ecuation for the line you are trying to get data from is literally the f(x) that creates the line that crosses the two points A and C, but inverted so intead of f(x) is f(y)
@Bisqwit
@Bisqwit 4 ай бұрын
Yes, that’s why it is called _line_ ar interpolation. :)
@RafiuskyGamer
@RafiuskyGamer 4 жыл бұрын
I’m not sure what that means, but I want to learn it so bad
@fdelduquenobre
@fdelduquenobre 4 жыл бұрын
lol. So do I
@JoelLonganecker
@JoelLonganecker 4 жыл бұрын
you are the bob ross of c++
@anter176
@anter176 4 жыл бұрын
Polygon graphics in software? That'll be interesting to see how it works! I've only programmed a software ray tracer before and I'm currently working with making a rasterising rendering engine but with the help of the GPU
@stofferm
@stofferm 4 жыл бұрын
View anything from the demoscene from 1988 to ~1997 and amaze ;)
@CT--od7ux
@CT--od7ux 3 жыл бұрын
finally i know how the rendirng works thanks bisqwit
@SevenDeMagnus
@SevenDeMagnus 4 жыл бұрын
So cool
@Devildoll89
@Devildoll89 4 жыл бұрын
i figured out that you're finnish on the first video i watched, what price do i get? also, you're incredible dude! no matter what anyone says keep uploading videos!
@scose
@scose 4 жыл бұрын
2:00 thought the GPU fan on my phone was spinning up for a second 😂
@jdm0ser
@jdm0ser 2 жыл бұрын
Hey Bisqwit! Just curious what resources you've used in the past to learn all this stuff? I've been (casually) researching software rendering for a few years now, and there's a lot of stuff I still can't quite wrap my head around. As GPUs were invented rather quickly, eliminating the need to understand this stuff at a software level, there doesn't seem to be a lot of literature on the subject that isn't outdated/old or just plain academic and hard to read.
@wonggran9983
@wonggran9983 2 жыл бұрын
4:03 so the compter represent points and lines in R then draws on a grid then displays grid on monitor of m x n pixels?
@davidnr
@davidnr Жыл бұрын
Thank you for an interesting video on an oldskool topic. However I think the use of c++ lies more on the m**turbation category. For the rest of the viewers: this is NOT how you're suppose to code in c++ this is only acceptable as an exercise to showcase c++ 20 features and not as an example of anything. Remember, kids p->x is faster to run, compile and far easier to read than std::get(GetXY(*p0)). If you need to parametrize your code there are other options besides templates (even duplicate code can be sometimes better than templates).
@OceanGroup001
@OceanGroup001 4 жыл бұрын
Nice sir
@KuraIthys
@KuraIthys 4 жыл бұрын
Interesting rasterisation function. It's not the most typical approach I've seen. Generally, to simplify the logic, the more typical approach is to make use of the fact that any of the other cases can be decomposed into a flat-top and flat-bottom triangle. Since both the flat-top and flat-bottom cases only require calculating two lines, and do not involve a change in direction, they are simpler to implement. The code for splitting a general triangle into a flat-top + flat-bottom pair is also trivial since it's simply dictated by the vertical location of the middle vertex of the triangle. Even a completely naive approach to this only requires comparing the Y value of each vertex and seeing which of these lies between the other two. (if two are at the same height you have a flat top or bottom triangle) Of course, dealing with that direction change in the middle isn't THAT difficult... But this still is not the approach I've seen taken very often...
@josephgoebbels1605
@josephgoebbels1605 4 жыл бұрын
I feel something hard in my pant every time i see a Bisqwit upload.
@CurrentlyObsessively
@CurrentlyObsessively 3 жыл бұрын
Hey bisqwit, the JOE hotkeys seem similar to linux hotkeys, but have you been able to use VIM to navigate with as much speed? I have been working with it for some time now, and the syntax for copying and navigating the text is a bit cumbersome. I assume that you use JOE because of this preference, or is there a performance difference as well?
@Bisqwit
@Bisqwit 3 жыл бұрын
What is "Linux hotkeys"?
@CurrentlyObsessively
@CurrentlyObsessively 3 жыл бұрын
@@Bisqwit ^A to beginning of line, ^E to end of line, etc... I'm sure that I'm calling it the wrong thing, lol.
@Bisqwit
@Bisqwit 3 жыл бұрын
Well yes, those, and ^U and ^V for pgup and pgdn, and ^D for delete… I don’t actually know where they originate. If I had to guess, probably GNU Emacs. And from there it has been incorporated into GNU Readline (used by bash and many other tools), PINE and PICO, and yes, JOE too. But the majority of inputs that JOE has is based on WordStar. JOE describes itself as a WordStar-Emacs hybrid. I have described why history and reason for editor choices in this video: kzbin.info/www/bejne/kH6lgqCehJ1-p6s
@beimanonestar
@beimanonestar 4 жыл бұрын
After the triangle everything was nonsense for me. Anyways good work and great video. Thank you again!
@theairaccumulator7144
@theairaccumulator7144 2 жыл бұрын
That's because modern c++ is purely syntactic diabetes to overcomplicate existing concepts (functions, structs, loops)
@rayforceaddict
@rayforceaddict 4 жыл бұрын
It makes me happy that you went for deltas instead of using that method Abrash describes. Awesome!
@Bisqwit
@Bisqwit 4 жыл бұрын
Who is Abrash?
@rayforceaddict
@rayforceaddict 4 жыл бұрын
@@Bisqwit He worked with id Software and wrote a bunch of columns on 3d graphics, which were later turned into books. His method IIRC was to use Bresenham, create an edge list from that, and then fill that in. I like the delta method though. My absolute favorite solution I've seen so far, though, splits the triangle in half (cut at the midpoint), then one routine draws the top half and another draws the bottom. IIRC it cuts down on if-then.
@rayforceaddict
@rayforceaddict 4 жыл бұрын
The thing is.. I almost never see anything but the edge list method mentioned online. That's why I'm happy that you are showing this technique. I think it's more straightforward.
@Bisqwit
@Bisqwit 4 жыл бұрын
Bresenham is just one very efficient way to do the “slope” with pure integer math. Another possible method is to used fixed point, but it sacrifices accuracy. Both of these are optimizations over floating point slopes, which were slow back then. Nowadays floating point math is pretty much exactly as fast as integer math, so there is no need for those hacks anymore. The edge list you are talking about is a different topic. It sounds like it is related to rasterization of arbitrary polygons (i.e. not triangles). The splitting of triangles into two halves is pretty much the fastest possible way. If you pay attention, you’ll notice my code does pretty much the same, except that it reuses the slope of the long side (p0-p2) along the way. At the midpoint (bend) it just recalculates the short slope (which was first calculated between p0-p1, but now between p1-p2) and carries on with the second half.
@rayforceaddict
@rayforceaddict 4 жыл бұрын
@@Bisqwit Yeah, it was definitely fixed-point (486/Pentium era) and extended to be able to draw N-point polygons. But I have seen people use it when only drawing triangles as well, probably because Abrash's book was so popular. So, it's really good that you're helping get this knowledge out there to the public IMO, to show people a different way of doing things than they might have known.
@potatopotato7561
@potatopotato7561 4 жыл бұрын
nice video
@UseQPixinDune
@UseQPixinDune 4 жыл бұрын
Looks fun. Time to get my hands dirty!
@ShanyGolan
@ShanyGolan 3 жыл бұрын
This is C++ on steroids. I love good old C.
@romainvincent7346
@romainvincent7346 4 жыл бұрын
Always cool to watch your videos. That lambda function though. #CodeGolf
@mllarson
@mllarson 4 жыл бұрын
Thumbs up for the Final Fantasy Mystic Quest music!
@persemake6090
@persemake6090 4 жыл бұрын
Do you always code with a style where you have such long lines, or is this just to fit more on same screen on the video?
@Bisqwit
@Bisqwit 4 жыл бұрын
Yes on both accounts. My typical terminal window is about 100 characters wide so I tend to keep lines shorter than that, but I went for presentation purposes here.
@smellthel
@smellthel 3 жыл бұрын
I love you
@Bleenderhead
@Bleenderhead 4 жыл бұрын
What precisely was meant by condensing the two loops into one using a lambda function? Is it just that it's more efficient to have a single loop that branches on a condition inside than to branch between two fixed loops based on the condition? I would expect the opposite to be true. Or does factoring out the contents of the loop to a lambda somehow allow the compiler to better decompose the program, and thus mysteriously generate more efficient code?
@Bisqwit
@Bisqwit 4 жыл бұрын
What I meant is difficult to explain unless you are familiar with how machine code works or how to read assembly. Assuming that you do, I’ll explain. I meant this: for(...) { call_func(&a); } for(...) { call_func(&b); } For the above code, the compiler is likely to generate the functor as a separate function, and perform explicit calls to that functor. This involves spilling local variables into stack / callee-saves registers, in honor of the ABI. The functor has to be compiled verbatim, literally at face value, and saved like that in the binary. for(...) { call_func(situation_one ? &a : &b); } For the second piece, the compiler is likely to inline the functor because it is only used once. This means that the compiler can optimize the functor _and_ the surrounding function as one unit, taking advantage of known values of variables, potentially generating more optimal code, eliminating never-taken branches form the functor, and so on.
@Bleenderhead
@Bleenderhead 4 жыл бұрын
@@Bisqwit Yes I am familiar with the cost of stack frames that go along with function calls. I see, so it's just that the compiler is more likely to inline the functor if it only gets called once. So if it were possible to force certain functions to be inlined, one wouldn't need to be as careful about calling them more than once?
@Bisqwit
@Bisqwit 4 жыл бұрын
Indeed.
@GOLEG11
@GOLEG11 4 жыл бұрын
who needs school when you have Bisqwit...? l:p
@Jaz_3001
@Jaz_3001 4 жыл бұрын
I wish I was that good at programming!
@whythosenames
@whythosenames 4 жыл бұрын
21:07 that took me ages to find but finally i got it!
@Bisqwit
@Bisqwit 4 жыл бұрын
If you use KZbin on a computer, you can pause the video and use comma and period keys to go backward/forward in the video frame by frame.
@whythosenames
@whythosenames 4 жыл бұрын
Bisqwit I am watching on my iPad and had to do x.25 and pause+play quickly and often and do -5s often since I kept failing :)
@martysh1226
@martysh1226 4 ай бұрын
4:22 i've never seen this kind of variable declaration in C++, why are there square brackets around the variable names? is this a new feature in C++20?
@Bisqwit
@Bisqwit 4 ай бұрын
It’s called a structured binding declaration, and it is new in C++17. en.cppreference.com/w/cpp/language/structured_binding
@martysh1226
@martysh1226 4 ай бұрын
@@Bisqwit thanks, now i'm gonna use this all the time 😁
@EgorChebotarev
@EgorChebotarev 2 жыл бұрын
Interesting thing
@F0r3v3rT0m0rr0w
@F0r3v3rT0m0rr0w 4 жыл бұрын
Question ... would it be efficiant or possible to combine rendering techniques ? Say voxels with raster ect ect ? When a situation requires said technique ect ect.
@Bisqwit
@Bisqwit 4 жыл бұрын
Yes, many games combine rendering techniques. For example, use a matte background and draw polygons over it, or use 2D sprites in otherwise 3D scene.
@F0r3v3rT0m0rr0w
@F0r3v3rT0m0rr0w 4 жыл бұрын
@@Bisqwit interesting cheers
@WarpRulez
@WarpRulez 4 жыл бұрын
Currently mixing traditional scanline rendering and raytracing is all the rage.
@Architector_4
@Architector_4 4 жыл бұрын
Probably an obvious example, but some videogames (e.g. Minecraft) use voxels to store and show the game world, but draw the voxels themselves with polygons that make up cubes representing the voxels.
@Architector_4
@Architector_4 4 жыл бұрын
Probably an obvious example, but some videogames (e.g. Minecraft) use voxels to store and show the game world, but draw the voxels themselves with polygons that make up cubes representing the voxels.
@Crossbow123
@Crossbow123 4 жыл бұрын
Is this real time typing? At least by watching the clock on the top right it doesn't look like a time lapse.
@forbeshowington5697
@forbeshowington5697 4 жыл бұрын
When do you plan on hosting the next video? This was amazing!
@Bisqwit
@Bisqwit 4 жыл бұрын
I am planning to publish the next episode in a few weeks. It is currently being edited, and I may still change its script depending on feedback I receive from this video. Generally speaking I try to maintain a video per month schedule, but let’s see.
@forbeshowington5697
@forbeshowington5697 4 жыл бұрын
@@Bisqwit Another question -- Is this rasterization algorithm comparable to some other well known algorithm? I'm curious how it compares to the one called "Parallel Algorithm for Polygon Rasterization", which seems to be the most commonly used.
@Bisqwit
@Bisqwit 4 жыл бұрын
It is very similar to the algorithm used by Fabrice Bellard’s TinyGL, except that to keep the code simple, I didn’t go for fixed-point.
@Gregi555
@Gregi555 4 жыл бұрын
Nice work again! You haven't showed off that you change your hardware :-). I see 48 threads on 4.2 GHz. It seems to be AMD 3960X with good cooling (water), doesn't seem? if yes which MB have you chosen?
@Bisqwit
@Bisqwit 4 жыл бұрын
That is correct. The MB is Asrock TRX40 Creator.
@wonggran9983
@wonggran9983 2 жыл бұрын
7:19 then include tick t for particle moving in triangle x(null) = 7, x(11) = 12, x(y) = 7 + ...
@JoLiKMC
@JoLiKMC 4 жыл бұрын
You keep this stuff up and before long, you'll have John Carmack *quaking* in his boots. ;)
@Bisqwit
@Bisqwit 4 жыл бұрын
Nah. He’s got a good 30ish years head-start on me.
@XanCraft21
@XanCraft21 3 жыл бұрын
Can you make a version of this for c++11? There’s a project i’m working on that uses this version and i don’t think it is possible to update it.
@Bisqwit
@Bisqwit 3 жыл бұрын
I’ll leave that as an exercise to the reader. I specifically teach the principles in this video series before writing any code, so that you understand the principle and can write your own code.
@XanCraft21
@XanCraft21 3 жыл бұрын
@@Bisqwit oh, well thank you. I could try to modify your version and see if that works if that’s ok with you.
@richardbajuzik2445
@richardbajuzik2445 4 жыл бұрын
Hi Bisqwit, love your channel! Have you tried also some low-level programming language and build something interesting? Eg. channel like Ben Eater is making simple computer based on assembly and machine code
@greatsaid5271
@greatsaid5271 4 жыл бұрын
nice
@KishoreG2396
@KishoreG2396 4 жыл бұрын
What's the difference between having a function template that takes a parameter of a type T, versus having the same function be a nontemplate and simply make that parameter "auto" instead of a type parameter? Is the latter more efficient than the template version? In what cases would one prefer "auto" over a template parameter?
@Bisqwit
@Bisqwit 4 жыл бұрын
The latter is just a simpler way to type the same. However, if you use “auto”, you need to use decltype(param) if you want to refer to the matched type.
@KishoreG2396
@KishoreG2396 4 жыл бұрын
@@Bisqwit So there is no benefit in efficiency or code size?
@Bisqwit
@Bisqwit 4 жыл бұрын
Only source code size and verbosity.
@eagle_2712
@eagle_2712 2 жыл бұрын
man, your tutorial is incredible and preety well explained, i trying to apply it on python, but i have a question: i dont program in c so, what does the function "make slope" does?
@Bisqwit
@Bisqwit 2 жыл бұрын
Slopes are used in my polygon blitter for anything that must be interpolated between two extremes. Mostly coordinates though. The X coordinates on both edges, from top to bottom, for instance.
@eagle_2712
@eagle_2712 2 жыл бұрын
@@Bisqwit ohhh cool, thanks!!
@stellarwind72
@stellarwind72 3 ай бұрын
The inefficient method shown at 4:07 is probably closer to what a GPU is doing under the hood, because it can be parallelized so easily.
@lrobertiii
@lrobertiii 2 жыл бұрын
You state four different techniques for implementing computer graphics; spheres, voxels, ray-tracing, and polygons. What is the difference between polygons and rasterization? I am new to computer graphics and just learning the basics. Is rasterization implemented using polygons? Thank you.
@Bisqwit
@Bisqwit 2 жыл бұрын
Rasterization is a way to make _something_ into pixels. Raster refers to a pixel grid. Like a squared paper, except that each square has to be fully colored or not at all; there is no “between” or partially colored pixels. Rasterization is when you represent _any_ visual content using pixels. Rasterization of polygons in particular is when your objective is to draw polygons on screen, the process in which you decide in which manner each pixel is colored.
@lrobertiii
@lrobertiii 2 жыл бұрын
@@Bisqwit So if I understand correctly, ray tracing is the process of, for every pixel, shooting rays of light into a scene to return the color value via illumination calculations and reflections and such but since it gets rendered on a pixel grid it's still rasterization in the end? So does this mean ray tracing does not use polygons at all? My apologies if the questions seem confusing, I'm still learning more and more and a lot of books and resources I've been seeing typically tries to distinguish between ray tracing and rasterization as being opposite approaches to rendering but if rasterization is just making something into pixels, then does ray tracing use rasterization? The resource I'm talking about is this video here that tries to distinguish them: kzbin.info/www/bejne/r5-mqaGIZs5jh7s Thanks again for your time.
@dvdv7777
@dvdv7777 4 жыл бұрын
Objection: The half-space method can be optimized greatly. It then is highly efficient. Optimizing it is a lot of work though. The SwiftShader software renderer uses the half-space method, and it performs surprisingly well on multicore CPUs.
@Bisqwit
@Bisqwit 4 жыл бұрын
Are you referring to the bounding box method? Yes you can use all sorts of tricks there, progressively turning it into a scanline renderer. In this video I just skipped that part altogether. The point of my series is to show intuitive and understandable methods that do not require you to go guide-dangit to find out how to do things.
@dvdv7777
@dvdv7777 4 жыл бұрын
@@Bisqwit I wouldn't say that it turns into a scanline renderer. Optimization methods rather turn it into a tile based renderer. For example, you could break up the bounding box into 8x8 tiles, and check if the 4 corners of a tile are fully inside or fully outside a triangle. If so, you can skip the half-space checks altogether. I do agree that the other method is more intuitive though.
@marksmod
@marksmod 4 жыл бұрын
13:22 It took me 3 days to come up with bersenham's line interpolation algorithm. Though I still am not happy with it, it produces janky lines. I never did make an algorithm which produced "nice" lines though
Texture Mapping & Polygon Rasterizing Tutorial (2/2) [C++20]
27:49
КАК СПРЯТАТЬ КОНФЕТЫ
00:59
123 GO! Shorts Russian
Рет қаралды 2,8 МЛН
狼来了的故事你们听过吗?#天使 #小丑 #超人不会飞
00:42
超人不会飞
Рет қаралды 55 МЛН
RenderWare: The Engine that Powered an Era | Retrohistories
8:42
Retrohistories
Рет қаралды 670 М.
An introduction to Raymarching
34:03
kishimisu
Рет қаралды 113 М.
Triangle Rasterization
2:17:06
pikuma
Рет қаралды 22 М.
Why PlayStation 1 Graphics Warped and Wobbled so much | MVG
12:36
Modern Vintage Gamer
Рет қаралды 2 МЛН
Rendering a car with my own ray-tracer
13:53
AngeTheGreat
Рет қаралды 282 М.
The Math Behind Font Rasterization | How it Works
16:07
GamesWithGabe
Рет қаралды 169 М.
How Ray Tracing (Modern CGI) Works And How To Do It 600x Faster
32:06
Josh's Channel
Рет қаралды 556 М.
I Made a Graphics Engine (again)
8:27
Zyger
Рет қаралды 156 М.
КАК СПРЯТАТЬ КОНФЕТЫ
00:59
123 GO! Shorts Russian
Рет қаралды 2,8 МЛН