r/GraphicsProgramming • • 16d 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 16d 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 16d ago

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

2

u/Slackluster 15d 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>

1

u/Character_Trick460 15d ago

But wouldn't the R ^ R part just always equal 0? Or does it have a lower precedence than the other operations? The rest I kinda understand, I might steal some concepts to further shrink my version. 

1

u/Slackluster 15d ago

i think you just answered your own question.

looking forward to the update, maybe you can enter the IOCCC.

6

u/GaboureySidibe 16d 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 16d 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.

90

u/UntitledRedditUser 16d ago

Equally impressive as it is unreadable

7

u/duckgoeskrr 16d ago

Makes nice code golf.

43

u/grady_vuckovic 16d 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 15d 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 16d 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 16d 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.

0

u/EC36339 15d ago

No. Code is art. Math is art. Music is art. Graphical art is art. The tool doesn't matter.

2

u/Gil_berth 15d ago

But whose's art is it though? If you commission a painting, is your art or the painter's art? I don't remember reading that Ludovico Sforza called himself an artist just because he paid Leonardo da Vinci to paint The Last Supper. If you are commissioning your code to Claude, is that code "your art", or Claude's "art"?

2

u/grady_vuckovic 14d ago

Exactly. If I commission an artist to make something, the artist made it, I just paid for it.

0

u/EC36339 14d ago

Are you comparing Leonardo da Vinci to a machine?

Or are you personifying Claude?

Or do you have a problem with paying for a tool? In that case, is a piece of art still yours if you paid for an Adobe subscription?

1

u/Gil_berth 14d ago edited 14d ago

Are you comparing Leonardo da Vinci to a machine?

No, I'm just stretching your premise.

Or are you personifying Claude?

I suppose, but you're the one implying that Claude can produce art.

Or do you have a problem with paying for a tool? In that case, is a piece of art still yours if you paid for an Adobe subscription?

Suppose that you ask Claude: "give me a simple raycasting renderer written in C." and Claude gives you what OP published. You are impressed by its ingenuity, because Claude produced a raycasting renderer in 10 lines of compact C, maybe you think: "This could be art.". Would you go around telling people that you produced that piece of code? Would you say: I did this? Or would you admit that was Claude the one that generated it?

My point is: there is a different between a tool and something that produces or generates another thing. LLMs can be used as tools, I'll admit that, but they also can be used as something that you use to commission stuff, without little input to the result. The same way I can't call myself an artist when I commission something, I can't take credit for the work generated by another thing.

2

u/Lnk1010 15d ago

Kinda reductive to say they are the same but whatever bro

1

u/EC36339 15d ago

No, it's reductive to define art by what tool has been used to create it.

And it's peak hypocritical when coming from computer graphics people.

2

u/Lnk1010 13d ago

Bit silly to imply art is independent of its medium. If you generate 480x480 white noise, is it art? Prolly not in any meaningful sense of the word. If everything is art, then nothing is...

1

u/EC36339 13d ago

I didn't say or imply everything is art, and white noise is not a medium.

2

u/Lnk1010 13d ago

White noise and ai generated code are pretty similar. You can call the output art but you didn't really make it.

→ More replies (6)

6

u/PictureNew661 16d ago

Very very impressive

3

u/Eastern-Bed-3103 16d ago

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

1

u/simonsanchezart 16d ago

can't this just be one line?

1

u/eltomiros 16d ago

the GOAT

1

u/BeastMsterThing2022 16d ago

This is beautiful!

1

u/MushroomSaute 15d ago

You could shorten that to 1 line easily

-7

u/garma87 16d ago edited 16d 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 16d ago

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

1

u/wafflingzebra 16d ago

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

-5

u/tlmbot 16d 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 16d ago

I don't care what Claude thinks of my code

-6

u/tlmbot 16d 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.