Realtime bumpmapping in 2D

Developed or developing a new product in PureBasic? Tell the world about it.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Realtime bumpmapping in 2D

Post by LarsG »

Hi,

I just converted an example by Andreas Blixt on the blitzcoder forum to PureBasic...
You can download a zipfile, containing source and exe here.

See the post on blitzcoder forums here.

-Lars

*edit*
-Fixed a integer/float bug
-Added FPS text
*edit*

also.. *edit* <backspace> *edit* saves a screenshot..
Last edited by LarsG on Sat Oct 11, 2003 6:12 pm, edited 3 times in total.

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
cyberseth
New User
New User
Posts: 4
Joined: Sat Oct 11, 2003 8:30 am
Location: Doncaster, UK
Contact:

Post by cyberseth »

It doesn't look right at all. andreas_blixt's looks fine but this one is just all pixelated and weird. I would post a screenshot but for some reason the PrintScreen button doesn't work while running the PureBasic app...?

Let us know if you can fix the problem(s), and then have some FPS readings on the two examples to compare them.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Hi Cyberseth.. good to see you have stopped by to say hello on this forum.. :)

I uploaded the new version (as I also mentioned in the showcase on blitzcoder).
The pixelation was due to my using integers where I was supposed to used floats.. hehe.
It's all fixed now, and a new version is at the same link above..

-Lars

(ps. I'll add some FPS soon..) *edit* added *edit*

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

am i missing something?
i looked all over the web page you linked but all i see is version
in blitz basic.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Yes, you are probably missing something, as there are two links in my first post!! ;)

the two links are:

http://www.tola-travels.com/_pure/bump.zip
(which is the PB version)

and
http://www.blitzcoder.com/cgi-bin/showc ... omments=no
(which is the post on the blitzcoder forum)

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Jack,

It's confusing, but the link to the PB program is attached to the word "here" at the end of the second sentence:

"You can download a zipfile, containing source and exe here."

I suggest changing the font color to blue on text links to make them easier to find.

Eric
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Lars,

That's pretty COOL!

I have a suggestion to make your code faster:

Move the test for UT <> 0 immediately after the assignment statement:
"UT = environmentmap(nnx,nny)",
instead of after all the calculations are done.
That way, you can skip all the time-consuming work if you're not going to plot the point anyway.

I got a 5 FPS speed-up on my machine with this change.

Regards,
Eric
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

It's cool, it's very cool!
ebs wrote:I have a suggestion to make your code faster:

Move the test for UT <> 0 immediately after the assignment statement:
"UT = environmentmap(nnx,nny)",
instead of after all the calculations are done.
That way, you can skip all the time-consuming work if you're not going to plot the point anyway.

I got a 5 FPS speed-up on my machine with this change.
11 FPS speed-up here. :D
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Here's the drawing code with every optimzation I could think of:

Code: Select all

.
.
.
  Repeat
    timer = gettickcount_()
    ClearScreen(0,0,0)
    ExamineKeyboard() : ExamineMouse()
    Event = WindowEvent()
    If StartDrawing(ScreenOutput())
      For y = 1 To 478
        For x = 1 To 638
          nnx = bumpmap(x + 1,y) - bumpmap(x - 1,y) - (x - MouseX()) + 128
          nny = bumpmap(x,y + 1) - bumpmap(x,y - 1) - (y - MouseY()) + 128
          If nnx < 0 Or nnx > 255 : nnx = 0 : EndIf
          If nny < 0 Or nny > 255 : nny = 0 : EndIf
          UT = environmentmap(nnx,nny)
          If UT
            sxy = surface(x,y)
            r = (UT * (sxy & $FF)) >> 7
            sxy >> 8
            g = (UT * (sxy & $FF)) >> 7
            sxy >> 8
            b = (UT * (sxy & $FF)) >> 7          
            If r > 255 : r = 255 : EndIf
            If g > 255 : g = 255 : EndIf
            If b > 255 : b = 255 : EndIf
            Plot(x,y,RGB(r,g,b))
          EndIf
        Next
      Next
.
.
.
Eric
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Thanks Ebs...
I used your optimization, and added some of my own, and the result is getting better...

New version uploaded to same place..

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

WOW!

FPS is now 62 about 50% of the time...

The other 50% it's -2147483648...
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Proteus wrote:WOW!

FPS is now 62 about 50% of the time...

The other 50% it's -2147483648...
8O Don't mind those minor technicalities, Proteus... :lol:

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Lars,

Limiting the drawing to just the spotlight area makes lots of sense, and provides a good speed-up.

I've got one more suggestion. I'm not really sure if this one is faster, but it seems like it should be.
Instead of using MouseX() and MouseY() inside the drawing loops, set two variables right after "Event = WindowEvent()", like this:

Code: Select all

mx = MouseX()
my = MouseY()
then use mx and my instead of MouseX() and MouseY() everywhere in the code. I don't know if reading a variable is significantly faster than calling the MouseX()/MouseY() routines, but it seems likely.

Eric

I just noticed that your original code did this - OOPS! :oops:
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

hehe.. yes it did, but i didn't notice much speed difference when you used MouseX() and MouseY() in the loop.. but I can always change it back...

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

awsome man 8)
~Dreglor
Post Reply