Advertisement Jump to content
Sign in to follow this  

2D Normal Mapping

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

If you intended to correct an error in the post then please contact us.

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.
		// 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.



      // 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

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] 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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!