[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Bump mapping
On Wed, 30 Jun 2004, Federico Di Gregorio wrote:
> Lì martedì, 2004/06/29 alle 15:41, -0700, Jasper Phillips ha scritto:
> > As I posted a while ago, I want a clothify filter that I can use on
> > dynamically created maps. After some investigation I found that this was
> > equivalent to bump mapping a carefully crafted image, so I hacked gimp to
> > spit out a bump map it generated for clothify, made it seamless, and am
> > tiling that to bump-map my map-images.
> >
> > This was rather slow, so I've set it up so that the "shade map" that the
> > bump-mapping algortihm calculates from the bump map can be saved as an
> > image which can just be applied without any undue arithmetic. Much
> > faster, but still taking ~7 seconds for larger images.
>
> you have a lot of math. have you tried psyco? that should speed-up your
> math a lot.
True, but the math is all in the area that can be precalculated, so I don't
really care if it's slow (and it sure is slow!). I'm mostly worried about
code that a user will run, namely:
def shadeMap( surface, shadeSurf, elevation=55.0, compensate=False ):
'''Bump map from a pre calculated shade map'''
surfW, surfH = surface.get_size()
shadeW, shadeH = shadeSurf.get_size()
compensation = sin(elevation)
resultSurf = pygame.Surface( surface.get_size() )
for x in range( surfW ):
for y in range( surfH ):
r,g,b,a = surface.get_at( (x,y) )
shade = shadeSurf.get_at( (x%shadeW,y%shadeH) )[0] # red
if compensate:
r = min( 255, r * shade / (255*compensation) )
g = min( 255, g * shade / (255*compensation) )
b = min( 255, b * shade / (255*compensation) )
else:
r = r * shade / 255
g = g * shade / 255
b = b * shade / 255
resultSurf.set_at( (x,y), (r,g,b,a) )
return resultSurf
Actually, now that I look at it in isolation there isn't much here beyond
the get_at()s and set_at()s. I should just get off my lazy ass and try
surfarray. ;-)
-Jasper