Page 1 of 2
Realtime bumpmapping in 2D
Posted: Sat Oct 11, 2003 1:54 am
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..
Posted: Sat Oct 11, 2003 8:33 am
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.
Posted: Sat Oct 11, 2003 11:49 am
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*
Posted: Sat Oct 11, 2003 3:18 pm
by jack
am i missing something?
i looked all over the web page you linked but all i see is version
in blitz basic.
Posted: Sat Oct 11, 2003 3:36 pm
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
Posted: Sat Oct 11, 2003 3:38 pm
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
Posted: Sat Oct 11, 2003 4:10 pm
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
Posted: Sat Oct 11, 2003 4:20 pm
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.

Posted: Sat Oct 11, 2003 4:41 pm
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
Posted: Sat Oct 11, 2003 6:11 pm
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
Posted: Sat Oct 11, 2003 6:15 pm
by Proteus
WOW!
FPS is now 62 about 50% of the time...
The other 50% it's -2147483648...
Posted: Sat Oct 11, 2003 6:22 pm
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...
-Lars
Posted: Sat Oct 11, 2003 6:44 pm
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:
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! 
Posted: Sat Oct 11, 2003 6:55 pm
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
Posted: Sun Oct 12, 2003 1:41 am
by Dreglor
awsome man
