Help with floorcasting

Started by
4 comments, last by UnshavenBastard 10 years, 9 months ago

Been trying to make a little textured raycaster for fun the past few days. Walls went fine, but I hit a wall when it came to floor texturing. No idea how to do it. So I found http://lodev.org/cgtutor/raycasting2.html , and I was able to adapt their floorcasting code quite easily. It worked, except that it couldn't handle it when I changed the camera height. Looking at their section on computing the 'currentDist' (from current pixel to floor, I'm assuming vertically), none of what they say makes any sense.

The distance the projection of the current pixel is to the floor can be calculated as follows:
  • If the pixel is in the center of the screen (in vertical direction), the distance is infinite.
  • If the pixel is at the bottom of the screen, you can choose a certain distance, for example 1
  • So all the pixels between those are between 1 and infinite, the distance the pixel represents in function of it's height in the bottom half of the screen is inversely related as 1 / height. You can use the formula "currentDist = h / (2.0 * y - h)" for the distance of the current pixel.
  • You can also precalculate a lookup table for this instead, since there are only h / 2 possible values (one half of the screen in vertical direction).

"the distance from a pixel in the center of the screen to the floor is infinite" What?

"for the bottom of the screen, you can choose a distance" I'm assuming this would be where my camera elevation goes in

"for example, 1" oh, thank god they chose 1 as their magic number. It's not like that ever comes up anywhere else

"the distance the pixel represents in function of it's height in the bottom half of the screen is inversely related as 1 / height." that wasn't even proper english. Plus, when they say 'inversely' and then use 1/x I assume the 1 is from standard math, but it's the only place on the page where they make any reference to the camera height which they so helpfully chose as 1 earlier.

"you can use this formula" no explanation on how they got that, or anything.

Is it even possible to have different camera elevations with this technique?

Advertisement

"the distance from a pixel in the center of the screen to the floor is infinite" What?

This makes sense when you assume the ceiling or floor is not at the same height as your camera. The exact center of the screen would be the point where the ceiling or wall touch the depth axis (e.g. 'Z'), but they don't, so the distance where the screen center meets the floor is... infinitely far away.
But there is no vertical center of the screen if you have an even number of pixels in the vertical dimension... not sure why the guy brings that up at all.
If his explanation for some of the things are confusing, try to figure it out yourself.

"You can also precalculate a lookup table for this instead, since there are only h / 2 possible values (one half of the screen in vertical direction)."

This is true, if he means the difference of distance (e.g. Z coordinates if Z is your depth axis) from one horizontal row of the screen to the next, or easier for later use, the difference to the bottom row, i.e. if you assume you would draw your floor completely from bottom row of your screen to the "center" row, each next row will be a bit further away. And since this sort of rendering has the restriction that you cannot look up/down and thus all floor / ceiling planes are parallel to your viewing direction, you have constant-height all over e.g. a floor plane. So you can make a table for each row from bottom to center, holding the difference in depth from bottom row. When rendering, you have to add those to e.g. the actual depth value of your bottom row which you calculate based on current camera height. So, you quickly have the real current depth values for each row.

You also need to rotate your floor plane based on camera angle. Then, if you go row by row, e.g. from bottom to center for the floor, imagine each row is layed on the rotated floor plane, at a start and end positions (first and last pixel of current row) which you determine with the camera position on the floor plane, and the rotation. So to say, actually you rotate those points about the inverse camera angle, not the floor itself.
Then when you move along the pixel row, from first to last pixel, you can compute where on the floor plane the current pixel would "hit", and thus compute the current texture index (assuming you have some grid specifying what texture some "cell" of floor has) and texture coordinates.
If you draw sketches of that on paper, you'll see how this works I hope, I can't draw anything here right now.

Hope this makes sense ^^

ah, and also note that, when drawing a row, you don't have to compute each pixel's floor coordinate the expensive way you compute the start,end pixel's coords. Because of the geometrical and camera orientation restrictions making your floor constant height for one render frame, also, your depth value for that frame is constant over the whole row of pixels. So you can just linearly step from start pixel's tex coord to the the end pixel's, by adding a delta vector each next pixel of the row, of the length (end - start) / pixels_per_row.

This all assumes always the whole lower screen half is drawn as floor, and the walls then on top, having overdraw up to a factor of 2. If you don't want to run that on spme weak machine, you can probably afford that, otherwise things get more complicated.
I haven't looked intensely at how the tutorial guy does this... but I'd rather not dig through his code ;)

If this is all confusing, I could draw some things later.

Ah! When he was saying "the distance from the projected pixel to the floor" I thought he meant vertical distance! That makes all of his code make sense, even if I still can't get the floor to render right when I change the camera height. (I've got it working fine for z=0)

In his tutorial, he renders the floor in vertical spans above and below the wall sections, computing for every pixel. You seem to be suggesting that I do floor spans horizontally? Wouldn't that mean I'm going to need some kind of complicated algorithm to figure out the horizontal spans, since the walls are all drawn vertically?

Heh you're right, what I suggested was just shrugging off the overdraw, if you can afford it.
You'll have to see what's faster... Since each horizontal line is constant-Z and you can just step through the texcoords linearly.
This does not work vertically, but since if you make those tables spoken of earlier, it might be similarly fast... I was thinking too much 3D earlier and thinking of perspective correct floor rendering as more expensive vertically, but that way it probably isn't ^^

Makes sense to just draw the spans vertically then along with the walls, yeah.
You should be able to have different camera heights, at least with what I have in mind, but I haven't looked at the guys exact implementation.

Btw., I guess it wouldn't be too compicated to do it horizontally. If you track, for each horizontal position of the screen, the top and bottom positions of all walls if you render them first, when drawing e.g. the ceiling afterwards you'd just need to run through the buffer for the top positions. You find the spans of current scanline by finding the highest positions (intersecting the current scanline) on left and right side of each "valley" in the buffer, one after another...

Back then I just drew floor and ceiling completely, horizontally, and painted the walls over it, it still ran sufficiently fast on my K6 200 or so, not at very high resolutions, granted ^^ It's luxurious for wolfenstein, but my goal back then was not to really do much with it game wise.

Btw., on today's PCs this may run faster if you use 32bit pixel format, not 8bit or what this old stuff uses...

This topic is closed to new replies.

Advertisement