Terrain rendering algorithm in less than 20 lines of code
github.com
github.com
I don't know if it is just me and the appeal to the stuff I grew up with, but I find the style of the visual artefacts a lot more appealing than in many more modern rendering techniques. Perhaps it is just the simplicity of it.
Looking at the visual artefacts in question, it appears that the big chunks of pixels (the ones closer to the POV) are not regularly aligned on screen, but rather disseminated around, which I guess improves the render quite a bit by giving it a somewhat "fuzzier", more organic look.
Maybe this irregularity is a side effect of the algorithm itself, and not something done on purpose?
I wonder at the speed of this on a modern machine vs an assembly version running in dos on a 1992 machine. A benchmark of interpreted code on a modern OS + machine vs bare metal native code written in the 1992 style on an old machine would be interesting.
I remember when Comanche came out... the 1990s were like the Heyday of flight sim games and Comanche was mind-blowing at the time as a teen. They were my favorite by far type of game. At some point it feels like they really died out. There were a lot of games that had a happy medium of fun vs realistic/complex back then.
At some point the # of flight sim type games plummeted and the ones that stuck around bifurcated into hyper-realistic to the point they are a time suck (cause you could be using an actual real flight sim for real flight training) or they are hopelessly unrealistic and no fun compared to the old stuff.
Not sure but I might disagree with the author about Comanche being 3 years ahead of it's time. It might have been closer to 5 years ahead of it's time. You didn't get stuff that really blew it away till hardware acceleration became prevalent, but by that time 3D FPS games were demolishing the flight sim market.
The problem isn’t JavaScript, which these days is remarkably fast. It’s that every app loads almost an entire OS in order to run. Process isolation, layout engine, text rendering, 2D/3D graphics, audio…
Those people would also be enabled if they bothered to get out of the Web comfort zone and actually learn about them.
I don't think they had a voxel engine built in.
Check out https://m.youtube.com/watch?v=48KtJtDqkaM for a 4K Vista Pro animation render!
You can see for yourself on this PDF, starting on page 38:
http://www.simwarrior.com/gunship/utilities/GSManUK.pdf
Edit: this is Comanche's manual:
http://www.starehry.eu/download/simulation/docs/Comanche-Man...
I'm amazed to discover that Tornado has gone open source and is still being modded by people: http://www.moodurian.com/
https://archive.org/details/Project_Stealth_Fighter_1987_Mic... for another blast from the past.
We knew how to project things in 3D and rotation worked well, but rendering more than a few points would bring the CPU to a halt. At one point we read about texturing, but there again, how could the huge levels of Wolfenstein 3d be rendered on modest CPUs? Surely it was not drawing all the sorted triangles over each other each frame? There was not enough resources for that!
Then one of us had internet access, printed two pages that explained raycasting. "Ohhhhh! That's how! rendering per column!"
And yes, Comanche was very highly regarded by people interested in 3D rendering. It was said to be coded entirely in assembly (was it true?) which to me at the time felt really impressive.
The following info might be of interest:
The company behind Digital Combat Simulator (DCS World), which is the hardest of hard core military flight Sims, recently announced Modern Air Combat (MAC) which I believe is aiming for the level of fun vs realism you mentioned.
"Over the past year, a massive amount of work has gone into Modern Air Combat in order to make it a AAA title. During this period, we have also expanded the scope and features of the product substantially. This has led to us push back its release date to 2020.
Modern Air Combat will not be a DCS World product, but rather a new series for us that focuses more on action, gameplay, and a shallowing learning curve."
https://www.digitalcombatsimulator.com/en/newsletters/newsle...
https://frankdenneman.nl/2015/02/19/memory-deep-dive-memory-...
Ok 1333 MHz DDR3 runs at 10,600 MB/s. A 640x480 32 bit color image takes up 640 * 480 * 4 = 1.17 MB. So the mini should get just over 9,000 fps with perfectly optimized code. I’m not sure if it’s possible to get there through OpenGL though, without direct access to the metal.
We just don’t see this kind of speed today in practice because it's really easy to fall off the fast path with renderers like OpenGL and not realize it. So people get used to always thinking in terms of 60 or 120 or 240 fps and don't even realize that memory busses and video cards are orders of magnitude faster than that. Figure roughly a factor of 4 slower for hand-rolled blitter innermost loops, and we're probably looking at rendering Comanche at between 1,000 and 10,000 fps on a typical i7 computer today.
All of this is why I was never really fond of the direction that 3D rendering went after video cards arrived around 1997. Shaders are cool and everything (even though they took 10 years to go mainstream), but it's a very specialized discipline and my brain is too old to keep up with it all now. I'd much rather have a multicore CPU with a wide bus (or better yet, multiple narrow busses) and no cache, where the GPU is ordinary RAM, and write blitters by hand. With 100,000 fps we could do some pretty wild stuff with voxels.
I've completely given up on all that though, as the world went a different direction. It will keep doubling down on the branch it's on (SIMD with specialized and proprietary shaders running through Unity or Unreal) and completely ignore the rest of the tree of possibilities like ray tracing and ray casting. Which is fine. But this is probably why my heart fell out of game programming, to be completely honest.
I did build a gaming PC with an NVIDIA GeForce RTX 2070 video card, but have yet to play around with the ray tracing stuff.
In another universe, span buffering (s-buffering) would have been a fast way to render scenes as strips of scan lines. I'm having trouble finding the original article, but maybe these will help:
https://www.gamedev.net/articles/programming/graphics/s-buff...
http://www.hugi.scene.org/online/coding/hugi%2016%20-%20co3d...
https://mikro.naprvyraz.sk/docs/Coding/2/SBUFF2.TXT
http://www.cs.uoi.gr/~fudos/sbuffer.pdf
I’m sure there are even better techniques now, but I haven’t followed them.
(Also, thank you @s-macke, your github page taught me the fundamentals of the algorithm - previously I had only seen Ken Silverman's post on wave surfing, which was not nearly as clear).
https://github.com/true-grue/terrain
Looks like Python+Tkinter is a good demoscene platform where you have performance of graphics close to 286/EGA :)
Again, thank you very much for all your education reverse engineering works!
There were Turbo Pascal versions of this on websites in the 90s I think, but it seems they were lost.
var ylean = (input.leftright*(i/screenwidth-0.5) + 0.5) * screendata.canvas.height / 4;
DrawVerticalLine(i, heightonscreen+ylean, hiddeny[i]+ylean, map.color[mapoffset]);
It adds a lot to the "feeling" IMO :)You can still find a lot of this old code if you dig for it; lot's of it can be found on various SIMTEL MS-DOS ftp archive sites, for instance. I also think the textfiles site has some of it. Also archive.org might have some of it.
Take a look around using google and such: "ftp msdos source code" etc - you'll find plenty to be sure (and even if you don't find what you're looking for, you're sure to find stuff you weren't expecting!)
Searching for "voxel" in https://github.com/nickelsworth/swag/ also finds some very similar tiny Turbo Pascal programs.
Regarding speeding it up & drawing front-to-back, didn’t some games do a back-to-front with some kind of bounds on how far down they draw? That way you get speed up without needing the y-buffer memory. I remember that in some games you could sometimes see cracks in the terrain, and I’m guessing from my faded memory that it was bounding the vertical draw. Not entirely sure if I’m remembering seeing cracks in sprite-drawn terrain though.
I was also wondering about the “Rotation” animated gif example, some extra mountains show up on the right side in the 360 spin for a couple of frames while the camera is passing the far end of the river, just before the pyramid comes into view. Is that just a bug, or is it something about the algorithm that’s tricky to fix?
I mean, the "Y-buffer" memory is just one integer per column, pretty trivial even for a software rasterizer on an early 90s machine with a limited cache hierarchy. Pretty much any algorithm that has overdraw will lose to that. That's why this technique was so popular in the nineties—what Doom did was just a generalization of this idea to allow multiple sparse pixel spans per column instead of just one. (For comparison, Z-buffer bandwidth is expensive because it's an extra 16/24/32 bits per pixel, which can double your fill rate requirements.)
Another trick I now remember was to interpolate the color values on such a y-segment, to reduce the pixelated look.
https://www.pastiebin.com/5e10d48ac6595
Just exchange the Render() method in my VoxelSpace.html file with the pastie.
That fixes the rotation artifacts, and then makes it really easy to add a distance-based fog effect to hide the cut-off where new terrain comes into view as well.
Voxels are a pretty neat graphics technology. For the fans of such style, Voxatron (from Lexaloffle, the PICO8 crew) is a pretty great environment for experimenting with the subject - its a fun game, but also a neat design/experimentation environment as well... https://www.lexaloffle.com/voxatron.php
Another World (Out Of This World) featured a couple of days ago hightlights this also, imho.
And fun, I had plenty while playing these games. Increasing computing power improves immersion and realism, which certainly improves the entertainment; but the fun? Not obvious to me.
Anyways, it seems new Comanche game is on the way!
I really like the animations that illustrate how the algorithms render the final result.
Low priority to find a current flight stick.
It really isn't much different from floor/ceiling rendering code.
Something else I was thinking was if you flipped it upside down (and kept the current view), you could render "caves"; heck, it probably wouldn't take much effort to mod the current code to achieve this.
Somewhere I have code to a voxel rendering engine someone made in QBasic; they posted a demo of it on youtube, but never posted the code - I got in contact with them, and they sent me a copy of the code. The interesting thing they did, though, was add voxel rendering of "buildings" - you could easily go inside spaces and outside, all voxel rendered, and it was fast (for QB code).
Note - I know about Ken Silverman's voxel and other 3D engines he did in QB (and posted his old code), prior to Duke Nukem 3D - but this wasn't that code; it was completely original...
Anyhow - this is a great little "3D" engine; I'm glad it was posted!
P.S. This code is not mine, the author has made an effort after seeing the documentary on fractals.
Cus I could write some stuff in one line of code. But that one line would be horrendously unmaintainable.
What metrics are there for maintainability (the most important aspect of code imo)
If there was a hard measure you could put on granularity for example.. Instead of saying hey I wrote this in 10 lines, you'd say hey I wrote this with 90 fleeborps of granularity! its super-maintainable!
ps. Not meant as a sly criticism of this. I have not even looked at the code. Just the 'lines of code' in the title sparked that thought. This thing is awesome! Just a few days ago I was chcking out Commanche Maximum overkill videos.. in 1992 that was mindblowing!
I remember it taking about an hour per 800x600 frame, and then we got a new computer and it only took 3-4 minutes per frame.
Was these hand drawn by an artist or generated in some way? Any info about it exists?
Or, to put that another way: what does the code in Sim City 2000’s terrain generator look like? Because it’s almost just doing this, but then it has that one extra thing...
Archive.org has most (all?) issues in a collection: https://archive.org/details/game_developer_magazine
The two ends of the spectrum serve completely different purposes but I think both deserve to be appreciated! Just for different reasons. This post (IMO) veers pretty close to the "art" side of the spectrum, so it doesn't make much sense to talk about its efficiency.
Yeah they could. But it's not a competition and would anybody care if they did?
I've never seen any comment about any of them which confused the brevity for computational efficiency, so I don't think that's an issue at all, and don't know where you got that idea.
> To whit: there’s absolutely nothing preventing anybody whipping together a de facto DSL wherein one can call a single render_terrain( ) function and get the task done with a single token
Sure, but they don't, because that's usually not interesting. Leveraging and demonstrating ways to do a task with small amounts of code using tools that are available but flexible enough that they can be applied to other tasks or variations of the task that are not identical, OTOH, is interesting and useful, and tends to be what people do in these things.
> To whit: there’s absolutely nothing preventing anybody whipping together a de facto DSL
That’s true, but that’s not what happened here, the author did not write a DSL, so this is not a valid example of your first sentence. As such, your phrase ‘to whit’ is misused, the phrase is synonymous with ‘specifically’. Also just FYI it’s spelled ‘to wit’.
In any case, I find it best to start by assuming not that people are getting rubbed the wrong way, but that I actually wrote something incorrect and didn’t know it. The primary reason downvotes are given is for information that is either incorrect or irrelevant to the thread. Without passing judgement or being upset, it is safe to say your top comment is incorrect about this article, and thus irrelevant here.
Also, note the guidelines ask to not complain about downvotes, which is why the immediate comment above is getting them. See “In comments” https://news.ycombinator.com/newsguidelines.html
I realize that justifying downvotes or continuing to talk on a thread that’s gone south doesn’t necessarily make you feel any better, but I honestly hope that helps. These things have helped me in the past.
I didn’t mean to come across as ‘complaing’ about downvotes, just as I didn’t mean to offend anybody with my comment (which admittedly was rather generic and probably not a specific response to the posted article, rather something I’d been meaning to get off my chest for a while).
I just hope I didn’t come across as dismissive, because that wasn’t my intention.
As for your charge of ignorance, that’s your call to make, but you seem to be missing the point I was trying to make (and which I obviously articulated very poorly), namely that code length and complexity is an interaction between both the algorithm being encoded and the formalism into which it is being encoded (programming language).
Anyway, I’ll leave it at that. Have a good day.
You did, at the point where you said "misguided".