# 2D Normal Mapping

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Can anyone notice what I've done wrong in this 2d Normal Mapping code? Basically it's a tile engine, but I prebake lighting information into a new map generated from the unlit tiles. I have a valid 2d normal map for the tiles. This isn't C++, it's bmax, but just think of it as psudeo-code. I've commented it. Atm it appears to work but any pixel that produces a negative point to light normal results in a dark pixel. As if I'm not mapping on the right planes.
  For Each Pixel
Local ra,ga,ba
///For Each Light
For Local lgt:Light = EachIn Light.Lights

// Calculate Vector From Pixel to Light, and normalize it.
Local nx:Float,ny:Float,nz:Float
nx = lgt.x-(tilex+x)
ny = lgt.y-(tiley+y)
nz = 100
Local mag:Float = Sqr( nx*nx+ny*ny+nz*nz )
nx = nx/mag
ny = ny/mag
nz = nz/mag

Local lscale:Float
Local px:Float,py:Float,pz:Float

//Read Matching Normal Map Pixel of Tile and
//And Scale it into the range of 0-1
pz =bpix[x,y,0]/255.0
py =bpix[x,y,1]/255.0
px =bpix[x,y,2]/255.0

//Scale it into the range of -1 to 1. Normalized Vector.
px=-1+px*2
pz=-1+py*2
py=-1+pz*2

// Calculate dot product between Normal Map vector and
// Pixel to Light source vector.
lscale =(px * nx) + (py * ny)  + (pz * nz)

If lscale>0

// Multiple Light's color by this.
ra:+lgt.r*lscale
ga:+lgt.g*lscale
ba:+lgt.b*lscale

EndIf

Next

// Modulate total light value
// By original color pixels value.
Local rm:Float = ra/255.0
Local gm:Float = ga/255.0
Local bm:Float = ba/255.0

r=r*(rm*2)
g=g*(gm*2)
b=b*(bm*2)

Next


It's not a problem elsewhere, as there's a flat lighting model that is within the same code base that works fine. \/Flat lighting model. -Edit- What's the forum code for a pic? [img http://img354.imageshack.us/img354/6744/lmap0xf.jpg] I'll upload a shot of how it appears using the above lighting model in a sec. Any help would be great..I can't think of anything else at this point. Thanks.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011267
• ### Who's Online (See full list)

There are no registered users currently online

×