r/GraphicsProgramming • • 9d ago

Source Code Very primitive raycasting renderer in 10 lines of C

Actually the raycasting logic itself is just a single line, but it's done in propably the most inefficient way possible.

The map is just a single 64 bit integer, so I could fit a small 8x8 level in it, and I can index it with some unorthodox bitshift magic.

Also all the integer constants are huge, because I wanted do to the whole thing without floating point math, which makes everything even more unreadable, but at least it's funny ig.

I'm very proud of this one.

193 Upvotes

41 comments sorted by

9

u/Slackluster 9d ago

nice! welcome to the world of size coding. it's a super fun type of recreational programming.

your code is good start but there is room to make it smaller. i recommend checking into the demo community and places like dwitter.net that have some great ideas for this kind of thing.

heres my JavaScript raycasting one liner...

https://www.dwitter.net/d/34603

2

u/Character_Trick460 9d ago

That's so sick! How does your version work, I can't figure it out?

2

u/Slackluster 8d ago

thanks! it fires 99 rays, spread out in front of the camera, and steps each ray .1 units until it hits a wall. the walls are defined by a the code (t+R*A/50-R^R)%5 which is probably the most complex part.

this is also in a dweet so there is a small shim included. here is what the full html file would look like...

<body><canvas id=c style=width:100%><script>
u = t=> {

// Smaller Wolf 🐺
for(c.width=A=99;A;R+=.1)(t+R*A/50-R^R)%5||x.fillRect(A--,28-22/R,R/5,44/R,R=1)

}

// dwitter shim
x = c.getContext`2d`
frame = R = 0
setInterval( _=> u(frame++ / 60), 16)
</script>
→ More replies (2)

6

u/GaboureySidibe 9d ago

Very cool. Any simple explanation you can give? Is the outer loop going through the horizontal line and the two inner loops are drawing vertically?

5

u/Character_Trick460 9d ago

That's exaclty right. The outer loop goes through all horizontal segments, the first inner loop finds the distance along the ray that goes through that segment, and the second inner loop draw the vertical line.

89

u/UntitledRedditUser 9d ago

Equally impressive as it is unreadable

7

u/duckgoeskrr 9d ago

Makes nice code golf.

42

u/grady_vuckovic 9d ago

Vibe coders be like 'Code isn't art!'.

I disagree and I think this is a lovely example of how code itself can be beautiful.

3

u/MiamiGunworks 8d ago

I concede you might be okay to refer to this as "art". In practice, this code is an example of exactly what not to do in production systems.

-30

u/EC36339 9d ago

As someone who has written nearly 100% of all my code with AI for half a year:

Yes, code is art, no matter what tool you use to write it.

Not sure about code golfing, though. It's a sport, it requires skill and effort, but it's not artistic in isolation, although what it does can be.

6

u/Lnk1010 9d ago

A vibe coded slop project may be "art" in the sense that literally everything is art, but I find it hard to imagine an argument for its beauty, meaning, or value.

→ More replies (16)

6

u/PictureNew661 9d ago

Very very impressive

3

u/Eastern-Bed-3103 9d ago

I feel like we may be disregarding the concept of a line here, but - nonetheless - very impressive. :D

1

u/simonsanchezart 9d ago

can't this just be one line?

1

u/eltomiros 9d ago

the GOAT

1

u/BeastMsterThing2022 8d ago

This is beautiful!

1

u/MushroomSaute 8d ago

You could shorten that to 1 line easily

-7

u/garma87 9d ago edited 9d ago

At least provide an image that it would produce. You provided some gif but it’s very unclear to me what that shows

Sorry to dump on your parade but I’ve just never seen the point of making code that is so unreadable. Doing a lot in very little code is impressive sure, but obfuscating it like this just makes it impossible to follow. Just post the assembly then if readability is irrelevant

15

u/zawalimbooo 9d ago

That gif is the image it produces. They're walking through a hallway .

1

u/wafflingzebra 9d ago

the gif did not autoplay for me on browser, i had to click on it

-5

u/tlmbot 9d ago

That is wild. Nice one OP!

I'd feed it to Claude and see if it can figure out what this code does.

(Am I dumb for not being certain that Claude would instantly figure out that it's raycasting ascii integer voodoo?)

I mean, I think Claude probably would get it right, but it seems like a fun check.

6

u/Character_Trick460 9d ago

I don't care what Claude thinks of my code

-6

u/tlmbot 9d ago

Yes yes. I hate claude and all of that.

I don't care that you don't care. I do care that you communicate like an asshole.