terrain - How do I apply generated normals to a bitmap? -
i going "backwards" terrain generation: procedurally generating terrain , normals. generating bitmap of terrain used 2d map. working correctly , generating results want. right now, 2d map has "flat" shading -- how can apply terrain's generated normals 2d generated bitmap?
ideally, apply lighting if "sun" directly overhead (at "high noon"). have searched , other dev sites, general internet, haven't found specific needs, theory , concept information.
can tell me how or point me in right direction? don't have supply code (although pseudo-code nice) need idea of start. using c#/xna game welcome ideas language.
what you're attempting create bump map.
http://www.gutgames.com/post/normal-map-creation-using-c.aspx
it assumes normal pointing in specific direction (perpendicular face usually) , offsets based on rgb values. there lots of resources covering bump maps including link above.
if wanted simplify things, store actual normal values in rgb of new bitmap (note lose precision method).
if range [-1...1] then: add 1 result , divide 2, multiply 255.
so if had normal (0, 1, 0)
0 + 1 = 1 / 2 = 0.5 * 255 = 128 = new x value
1 + 1 = 2 / 2 = 1 * 255 = 255 = new y value
0 + 1 = 1 / 2 = 0.5 * 255 = 128 = new z value
then store (128,255,128) in bitmap.
to return correct value reverse process:
128 / 255 = 0.5 * 2 = 1 - 1 = 0 = old x value
255 / 255 = 1 * 2 = 2 - 1 = 1 = old y value
128 / 255 = 0.5 * 2 = 1 - 1 = 0 = old z value
precision poor works enough. pack values 2 bytes per x , y. x = rg , y = ba of rgba values. can magic pull z value out full normal. when normalize normal length becomes 1. 1 = sqrt(x*x + y*y + z*z). know x , y when reading them bitmap, can solve z. improves precision , used improve bump mapping precision depending on needs.
Comments
Post a Comment