Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Raycasting problems

This thread is locked; no one can reply to it. rss feed Print
Raycasting problems
aybabtu
Member #2,891
November 2002

I've attached a .zip with a DOS binary, the source code, and my little texture set. Could you guys test it out and tell me what kind of FPS you get with the "Ray Skip" at 1, and at 5? Use the -/+ butons to change it. It makes it run a lot faster on 5, but you lose detail...I wanna see how fast it runs on ppl's computers.

Goodbytes: I'm using 16bit color. Down with palettes!

J13: That screenshot looks nifty...I kinda like it with the lines on top of the walls!

Zaphos said:

For your outdated as anything setup, however, I'm afraid there might be more errors, which is entirely your fault. Update your software.

Fine...I'll download allegro 4 or the newest WIP or something...
Anywayz, here's the latest code:
(Control view with mouse/arrow keys, move with WASD)

[WHOOPS! The post's too big with it...just download the attachment]

As you can see, I've eliminated another call to the cos() function, by using a table to fix the distortion!

EDIT: 23: Words are backwards on my textures only on certain sides of a cube. 2 sides are right, 2 sides are backwards. I tested it with the all powerful NYAH.

Thomas Fjellstrom
Member #476
June 2000
avatar

YaY!!! I was waiting for source and some textures :)

edit:
Ok, I get about 30fps normally in XWIN mode, not too shabby. XWIN is like GDI mode in windows.. SLOW. and If I look directly at a wall I get 40fps :) I'd try it in DGA2 mode, but my monitor doesn't do anything less than 640x480.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

aybabtu
Member #2,891
November 2002

Are you going to FPS-test mine?

Thomas Fjellstrom
Member #476
June 2000
avatar

heh, edited.. :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Goodbytes
Member #448
June 2000
avatar

aybabtu said:

Goodbytes: I'm using 16bit color. Down with palettes!

Good. Then lighting is easy enough... sorta. I guess. Or something. Yeah. ;D

Anyways, if you want to shade based on depth, as if the player has a light on his/her head or whatever, you'll probably want to shade the ceiling and floor as well as the walls. So, you need to darken your floor/ceiling gradient a little as it reaches the horizon.

Then, as for shading the walls, you can probably just subtract a little from the colour components of each pixel based on how far away it is. Allow me to rip some code from FBlend for a minute.

Okay, here's a sort of example program for ya:

1#include <allegro.h>
2 
3int main()
4{
5 allegro_init();
6 set_color_depth(16);
7 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
8 install_keyboard();
9 
10 BITMAP* src = load_bitmap("src.bmp", 0);
11 BITMAP* dst = create_bitmap(src->w, src->h);
12 
13 // Important part #1: light levels
14 unsigned long lightlevel[32];
15 for(int i = 0; i < 32; ++i)
16 {
17 lightlevel<i> = (i | (i << 11) | (i << 22));
18 }
19 
20 int distance = 0;
21 
22 while(1)
23 {
24 // Important part #2: subtractive lighting
25 for(int y = 0; y < src->h; ++y)
26 {
27 for(int x = 0; x < src->w; ++x)
28 {
29 unsigned long c = _getpixel16(src, x, y);
30 unsigned long res;
31 c = ((c << 16) | c) & 0x7C0F81F;
32 c -= lightlevel[distance];
33 res = c & 0x8010020;
34 res -= (res >> 5);
35 c &= ~res;
36 c &= 0x7C0F81F;
37 c |= (c >> 16);
38 c &= 0xFFFF;
39 _putpixel16(dst, x, y, c);
40 }
41 }
42 
43 textprintf(screen, font, 0, 0, makecol(255, 255, 255), "Distance: %2d. [Q] to quit, [A] & [Z] to control distance. %d", distance, lightlevel[31]);
44 draw_sprite(screen, dst, 0, 10);
45 
46 clear_keybuf();
47 int k = readkey() & 0xff;
48 if(k == 'q')
49 break;
50 else if(k == 'a')
51 ++distance;
52 else if(k == 'z')
53 --distance;
54 distance = MID(0, distance, 31);
55 }
56}
57END_OF_MAIN();

This is C++ code, so compile it as so.

The two parts to note are the creation of the light table, and the actual subtractive blending parts, which are commented as important parts numbers one and two, respectively.

Now, credit must go to Bob for actually writing the subtractive blending part in FBlend, which I modified a teensy bit. The rest of the program is my fault. :)

Actually, I think it works well. Make a small bitmap called src.bmp, compile this, and put them in the same directory, and try it out yourself.

Anyhow, now on to how it works. It's kind of hard to explain... basically, in the subtractive blending part, the color is grabbed from the source bitmap(you should use a faster method than _getpixel16 if possible, btw) and then the individual colour components are separated apart, so where originally the binary representation of the colour looks like this:
OOOOOOOOOOOOOOOORRRRRGGGGGGBBBBB // 16-bit colour on a 32-bit int, 5 bits for red, 6 for green, 5 for blue
it gets transformed to look like this:
OOOOOGGGGGOOOOOORRRRROOOOOOBBBBB // 16-bit colour on a 32-bit int, but spread apart, and 5r5g5b instead of 5r6g5b
The lightlevel[] array is set up to match the above representation for 32 levels of light... for example, lightlevel[31] looks like this:
OOOOO11111OOOOOO11111OOOOOO11111 // 11111 = decimal 31 - for each colour component
Then, the light level is subtracted from the source colour, put back together into the packed 565 format, and drawn onto the destination bitmap. Again, use direct bitmap access instead of _putpixel16.

This is basically the same thing as doing

unsigned int r, g, b, c;
c = _getpixel16(src, x, y);
r = getr16(c); g = getg16(c); b = getb16(c);
r -= distance; g -= distance; b -= distance;
c = makecol16(r, g, b);
_putpixel16(dst, x, y, c);

But hopefully faster.

Finally, remember that since you only have 32 distance values, try to spread them out a little - i.e. you would probably want a slice that's 32 'pixels' away to actually be quite bright. In fact, you can probably map distance in 'tile units' - i.e. with a slice that is 1 tile length away being a distance value of 1.

Hope this helps.


--
~Goodbytes

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

EDIT: 23: Words are backwards on my textures only on certain sides of a cube. 2 sides are right, 2 sides are backwards. I tested it with the all powerful NYAH.

That's what happened to me. My latest code up there corrects this.

Quote:

YaY!!! I was waiting for source and some textures

Dude, that's all we've been posting ;)

Sweet mother, aybabtu, why does your code run at 0 FPS?!? :P Seriously, I thought it froze my computer. I haven't looked at your code yet to see what's wrong, but it's literally unplayable. Exited via crash. BTW, have you tried my latest? I get 150 FPS, dunno what that comes to on your box ...

BTW, if you aren't texturing the floor or roof, gradient shading becomes very easy ;D

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Dude, that's all we've been posting

Sory, didnt notice any textures. :P I compiled and ran a previous version of somebodys and it crashed :o I was about to report it, but realize that it was because using a NULL bitmap doesn't work to well.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Trezker
Member #1,739
December 2001
avatar

I've done some bugfixes and other stuff.
I don't know how things are with the M_PI define you complained about, it compiles fine here.
Have a look and do as you wish.

23yrold3yrold
Member #1,134
March 2001
avatar

I used my own texture. Just throw a 64x64 truecolor texture in with the executable and you're good to go. Try it with mine; how's it work for you?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

Textures? I don't have no stinkin textures. Really. (um.. you want me to try yours now? demands... sheesh :P)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

I don't have any texture either; I just drew some scribbles in MSPaint ;D

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

;D. Ok, I forgot to mention, my fps that I reported was totally unoptimized, and unaccelerated, windowedmode... I'll try that other one again, just optomized...

edit: ok that wolf one is like 50-60 fps now...

edit: ok, yours chris :o 300fps. in 320x240. in XWIN mode.. I'll see what DGA mode does.. (cause aparently, my monitor can do 320x240... I thought it couldn't)

edit: more updates... with DGA it hits 300-370 (400 at max) fps, and at 640x480 in DGA mode, I get 80-100fps.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

23yrold3yrold
Member #1,134
March 2001
avatar

BTW, can anyone help with this little bit of math? The if statement is to skip the offscreen pixels (HUGE fps boost) but it messes up the texture a bit. I've attached a screenshot which demonstrates this ...

1 
2void DrawSlice(BITMAP* buffer, BITMAP* texture, int xoff, int x, int y, int tall)
3{
4 int srcy = 0, num = 0;
5 
6 // skip down until we hit the top of the screen (for very close walls)
7 // this bit is currently a little buggy ...
8 if(tall > SCREENHEIGHT)
9 {
10 num = 64 * ((tall - SCREENHEIGHT) / 2);
11
12 if(num >= tall)
13 {
14 srcy = num / tall;
15 num = num % 64;
16 }
17
18 y = 0;
19 }
20
21 // draw that slice!
22 for(int i = y ; i < y + tall ; ++i)
23 {
24 num += 64;
25
26 while(num >= tall)
27 {
28 num -= tall;
29 ++srcy;
30 }
31
32 if(srcy >= 64 || i >= buffer->h) return;
33
34 // this assumes the texture is rotated (otherwise it's slower due to cache missing)
35 // ((short *)buffer->line<i>)[x] = (((short*)texture->line[xoff])[srcy] >> 1 & 0x7BEF);
36 ((short *)buffer->line<i>)[x] = ((short*)texture->line[xoff])[srcy];
37 }
38}

See anything wrong with that first half of the function?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Harte
Member #33
April 2000
avatar

Quote:

TH: Could you compile it into a binary? My DJGPP complains about MANY "errors". I tried fixing some, but I just gave up. How smooth is it? Would you think that you were in a raycaster when you're running it? What is a 2D portal engine?

Portal engines work on the principle of sectors. For my engine, a sector is a convex room. Some of the walls are thought to be portals. In ray casting terms of thought, when a ray hits a portal, it carries on through the portal into another sector rather than deciding that it has hit a wall.

To do all this, the engine knows which sector the player is currently in, and works on the following (pseudo-code) draw loop:

function DrawSector(s)
{
   for(all non-portal walls in sector)
      draw walls to the display

   for(all portals)
   {
      calculate area of screen that portal would cover if it were a solid wall
      call DrawSector with the connected sector, specifying to restrict to that area of wall
   }
}

Because the sectors are convex, if we know the start sector, this gives a no-overdraw front to back draw.

I shall try to dust off a copy of DJGPP and compile my code. It was developed on MSVC so I may have inadvertently used some Microsoft extensions (e.g. unnamed structs) without realising...

Quote:

TH, I think the word you're looking for is sliver, not slither.

Nah, its all to do with the first tutorial I read about Wolfenstein type things, many years ago, which compared them to snakes (don't ask).

MiquelFire
Member #3,110
January 2003
avatar

Chris: Where's yours? I don't have a compiler here at work (and don't feel like setting one up here)

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

23yrold3yrold
Member #1,134
March 2001
avatar

Okay, I'm attaching a zip with makefile, source, Win32 binary and texture file (32KB).

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Zaphos
Member #1,468
August 2001

23: Your code stays over 150 on my computer even near walls, and tends to be >200 ... very nice! I can't really see the glitches you're talking about, though, so I can't really comment on what might be causing them. Perhaps you could post a screenie of a pronounced glitch? (Heh, you could even check miran's screenie module in the early modules submission thread to do it ...). This test result is from using your latest code posted in this thread, which is quite a few posts up, actually, so I guess you may have made progress since then.

EDIT: Gah! I idle a bit and look what happens ... takes me 20 minutes to write this post and to test 23's code and sooo many people have already posted. It's not being beaten ... it's being completely demolished :o

23yrold3yrold
Member #1,134
March 2001
avatar

I did post a screenshot (5 posts up) as an attachment. Guess you missed it, dawdle-boy ;). It will show up on any wall that is a) taller than the screen, and b) at an angle (you won't see it if you're standing facing straight at the wall).

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Zaphos
Member #1,468
August 2001

For reference, to document the worst beating I've seen yet: the last post in this thread when I wrote the above response was TF saying:

Quote:

Textures? I don't have no stinkin textures. Really. (um.. you want me to try yours now? demands... sheesh )

Thomas Fjellstrom
Member #476
June 2000
avatar

Actually if you face the wall right up close, the ray infront gets quite abit bigger than it should, if you take a look at the size of the rays around it..

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

MiquelFire
Member #3,110
January 2003
avatar

23: Stays over 260 with this 1.70GHz machine I have here at work. When not close to a wall, I get around 313.

aybabtu: 2000 don't like DOS apps :(, so I couldn't test it here.

Will do a test at home on my 600 MHz system (which, I assume because of RAM and other network stuff, run laps around this computer :P)

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

23yrold3yrold
Member #1,134
March 2001
avatar

TF: If that's directed at me, I'm not seeing it ...

If Matthew closes this thread for length, I'd like to start another one, because I'd really like some help with those texture errors :-/ EDIT: nm, I just did ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

aybabtu
Member #2,891
November 2002

Thomas JelloStorm said:

edit: ok that wolf one is like 50-60 fps now...

Yay! What's your system?

Goodbytes: Thanks...I'll try that stuff out tonight or something...

23: Your new code skips off screen pixels! Yay! Maybe even more FPS!

Miquel: Sorry it's DOS! I don't have MinGW or anything, but it shouldn't be hard to compile...you should just have to stick END_OF_MAIN in there.
On a 600Mhz system...you should get nice numbers. On my 233, I get ~20 frames...and with TF getting 50-60! Ah...life is good.:P

MiquelFire
Member #3,110
January 2003
avatar

Ahem I went over this already.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

Thomas Fjellstrom
Member #476
June 2000
avatar

23: darned. well, it seems that must have been a fluke, or only happens in very rare circumstances... but I did see something else thats a bit odd.. hard to explain, just that I found if you're standing right up to a wall, and turn back and forth a bit the colors on the texture move :o In the attached image, notice the green, almost teal color, now If I use the arrow keys to look side to side, that teal grows and shrinks in size, after a bit of looking left, it totally disapears and the colors underneath takeover. weird.

Quote:

Yay! What's your system?

Athlon 900.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730



Go to: