Simple LOVE Raytracer Code (both untextured and textured)

Showcase your libraries, tools and other projects that help your fellow love users.
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Simple LOVE Raytracer Code (both untextured and textured)

Post by scutheotaku » Tue Dec 18, 2012 3:07 am

Simple LOVE Raytracer Code

I've been interested in learning how to make a basic raytracer-based 2.5D renderer for a while now, and, after being impressed by both LOVE's speed and Jasoco's work with his WolfenLOVE 3D engine, I decided to give it a try. So, I found Lode Vandevenne's excellent raycasting tutorials ( http://lodev.org/cgtutor/ ) and got started. And, after a night and a morning, here are the results...

Image

It's nothing spectacular, but it might be a good start for someone looking into making a 2.5D game like Wolfenstein 3D or Blake Stone.

(prepare for a verbose description - scroll to bottom of post for downloads :D )

I was able to mostly port the code from the untextured raycaster in Lode's first tutorial (which is in C++) to Lua/LOVE. There were obviously changes to get things working in Lua, and quite a few changes to the drawing to get it to work within LOVE's callback function (unfortunately resulting in a second For loop, though I don't imagine that it's affecting framerate too much).

For the textured version, I did make a fair amount of changes. First of all, instead of scanning each pixel individually, I, inspired by WolfenLOVE, used quads instead. This means that the program only has to do one check for each x position instead of one for each x/y combination (so, for a 640x480 window, it only has to do 640 checks per frame instead of 307,200 - actually 1280 per frame vs 614,400 per frame because of the drawing). It's a very simple way to boost the speed, but I probably wouldn't have thought of doing it that way without Jasoco's posts (if you're reading this, thanks!).

There is a problem with the textured version though...there's some distortion whenever you get very close to a wall (it seems to start once the height of a wall fills the entire height of the screen). I'm positive that this has something to do with my approach to using quads, but I haven't yet figured out a solution. If anyone does come up with something, I'd appreciate some tips :)

One thing to keep in mind: I purposely kept the structure of most of the code and the variable names as identical to those in Lode's tutorial as possible. This way, you (and I!) can directly use has excellent descriptions of how it works and what does what. Furthermore, one could use this as a base to continue with his raycasting tutorials and add things like sprites and textured ceilings and floors. You can find the tutorials here: http://lodev.org/cgtutor/ . I've also made an effort to clearly comment the code, so it should all be fairly easy to understand.

Here are the framerates I get for the textured version at different resolutions (obviously, the framerate is greatly affected by the resolution):
Resolution : Framerate
640 x 480 : 120 to 127 FPS
800 x 600 : 100 to 110 FPS
1024 x 768 : 86 to 95 FPS
1280 x 1024: 68 to 77 FPS
1280 x 960 : 67 to 75 FPS

I actually get a little slower framerate with the untextured version, though it's barely noticeable. I assume this has to do with the efficiency of LOVE's draw line function compared to the quads I use for the textured version. If the difference in framerate was bigger, I think I would probably try replacing the lines with quads in the untextured version.

I'd be curious to see what some of you get for a framerate. This computer has an Intel Core i5 3.2GHz processor in it, but only has Intel HD onboard graphics (though I don't think that the GFX card would make much of an impact since the math and loops are the main resource eaters here).

Also, both examples have some basic functions, including moving, turning, strafing, and support for multiple resolutions.

Ok, well, here are the downloads (I'm new to Lua and LOVE, so please exuse the code!):
Untextured: https://dl.dropbox.com/u/17269775/raytr ... g-1.0.love
Textured*: https://dl.dropbox.com/u/17269775/scuth ... d-0.6.love
*the textures are from the game Wolfenstein 3D and are copyrighted by id Software

Cheers,
Ben

PS: I realize that there are a few other threads on raycastering engines, but, since most of them having been posted in for a while, I figured I'd start a new thread.

EDIT:
I just came across Inny's port of Lode's tutorial code here: viewtopic.php?f=5&t=11770&p=70655&hilit ... ing#p70655
That will probably be a better reference or basic engine than mine as it 1. handles textures much better, and 2. has most of the features of the end-product of Lode's tutorial series.
Last edited by scutheotaku on Sat Dec 22, 2012 3:14 am, edited 2 times in total.

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by substitute541 » Tue Dec 18, 2012 7:00 am

Here's my FPS
Nontextured
1. 640x480 : 79
2. 800x600 : 60
3. 1024x768 : 56
4. 1280x1024 : 45

Textured
1. 640x480 : 85
2. 800x600 : 72
3. 1024x768 : 58
4. 1280x1024 : 47


Strange, Textured is faster.. Here are my specs :

1280x1024 Monitor
No Graphics Card (derp)
Windows 7 Ultimate x64
(from memory) ASUS P5KPL-AM-SE Motherboard
Currently designing themes for WordPress.

Sometimes lurks around the forum.

coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by coffee » Tue Dec 18, 2012 9:07 am

Congratulations. Your raytracer should be probably the lightest engine of this kind in LOVE (Inny's one is also quite light).
It's hard to ran one without heat up my machine but your hardware fingerprint is almost null. Also solid vsynced 60fps in both versions.
Plus your code is so clean (and short) that even a math idiot like myself could learn it easily. Well done.
Don't worry about the stretch bug some quad/math LOVE wizard will certainly help you to solve it.

User avatar
Jasoco
Inner party member
Posts: 3651
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Jasoco » Tue Dec 18, 2012 5:14 pm

Yeah, messy code is one of the reasons I shelved mine temporarily. I'll get back to it eventually. It isn't dead. It'll be back.

Gravy
Citizen
Posts: 80
Joined: Sun Jan 22, 2012 10:15 pm
Location: CA, USA

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Gravy » Tue Dec 18, 2012 6:15 pm

Very nice! It makes much more sense to increment the X distance and the Y distance in the same loop rather than do all the X's and all the Y's separately, as in the earlier raycasting demos that Jasoco and others posted a while back. I've been using raycasting for the lighting in my game, and this should make it a bit quicker.

User avatar
Jasoco
Inner party member
Posts: 3651
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Jasoco » Tue Dec 18, 2012 6:46 pm

The more raycasters the better. Let everyone create their own. I love it. Imagine if Ken Silverman decided not to create the Build engine because DOOM was just so awesome. We wouldn't have the superior (To the DOOM engine) Duke Nukem 3D/Blood/Shadow Warrior engine which was capable of things the DOOM engine could never do, and even Quake couldn't do in some ways.

Ken started by creating a Wolfenstein 3D style engine of his own when he was a teenager. Then graduated to the more DOOM-like Build engine later on. YouTube has a lot of really cool videos documenting the history of his engines.
http://www.youtube.com/watch?v=l8tDBg4pfYk


My engine eventually gained floor textures (Aided by Canvas) as well as buggy doors, annoying dumb NPC's that just constantly walk towards you, a map and sprite objects. When I get around to redoing it, it will get even more... eventually. At some point eventually.

User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Inny » Tue Dec 18, 2012 8:11 pm

Very neat.

In addition to Lode's tutorial, you probably saw this in your searches as well: http://www.permadi.com/tutorial/raycast/index.html

There's a few good tidbits of info in there, like how to get looking up/down and changing the eyeline (to simulate jumping/ducking). In terms of how it applies to Lode's code, the center line you draw the scanlines from is for "Looking", meaning change that and you can look up and down. The eyeline was a bit more tricky. I have updated code that I probably should refresh my old thread with to demonstrate these features. I was taking my engine towards being an RPG engine though, and gave up when it came to floor/ceiling textures since I couldn't find a reasonable way to do it (even the mode7 style code looked to be way too painful toward the framerate).

What I'd also love to see eventually done is variable height floors/ceilings. Even if we can't quite get to textured surfaces, it'd be nice to see doom/hexen or even RiseoftheTriad style vertical movement. It might make a nice longterm goal for you if you want to take your code past the wolfenstein era.

User avatar
Jasoco
Inner party member
Posts: 3651
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Jasoco » Wed Dec 19, 2012 12:43 am

DOOM would be very possible. Except that floor/ceiling would be very hard as far as I've seen so far. (I had to resort to canvases for floors in my Wolf clone and that's just one plane) Also you'd need to figure out collision with arbitrarily positioned lines.

I did do a lot of research into the histories of DOOM and Wolfenstein back then.

The source code for each is readily available even though it's C.

User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Inny » Wed Dec 19, 2012 1:42 am

RoTT seems like the next logical step, seeing as how it was just the floor on a flat plane, and they emulated staircases and platforms heights through sprites.

See: http://i.imgur.com/uTlGG.jpg

Gravy
Citizen
Posts: 80
Joined: Sun Jan 22, 2012 10:15 pm
Location: CA, USA

Re: Simple LOVE Raytracer Code (both untextured and textured

Post by Gravy » Wed Dec 19, 2012 3:50 am

Jasoco, I remember all the awesome screens you posted of your double-layered raycasting with mouselook. We're still waiting for the final product :)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests