Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Normalmaps mipmaps Mathematica playground

# Normalmaps mipmaps Mathematica playground

Ratings: (0)|Views: 818|Likes:

Categories:Types, Research

### Availability:

See more
See less

08/15/2013

pdf

text

original

In[1]:=
Load a normalmap, convert to floats and remap it to the
1,1 range, resize to make it more manageable...
normalMap
ImageApply
Normalize
0.5
&,Image
ImageResize
Import
"C:\\Users\\angelo.pesce\\Desktop\\4.jpg"
, 128
, "Real"
Out[1]=

In[2]:=
We'll need a mean to operate on sub
blocks of an image,let's write a small helper function that does these operations in parallell
blockImageMap
img_, fn_, size_
:
Parallelize
Map
fn, ImagePartition
img, size
,
2

Let's compute the average normal for the blocks,we could have done it wiht anotherImageResize but it's also a test of blockImageMap...
blockSize
16;averagedNormals
blockImageMap
normalMap, Normalize
Apply
Plus, Flatten
ImageData
, 1

&, blockSize
;Image
averagedNormals
averagedNormals
Flatten
averagedNormals, 1
;
Out[5]=
2

normalmapFit.nb

In[7]:=
We're not going to use a four
dimensional Phong BRDF,we'll cheat reduce Phong to a simple lobe around the normal,like a "powered" diffuse
Let me motivate this a bit: let's say that we have a cone
Phong which takes a cone of normals and a light direction. The light aroundthe normal reflection direction around the cone will be a cone whosesize is equal to the normal cone no matter where the light direction is,thus this cone
Phong will have a constant lobe which justrotates as the light direction changes,so we can ignore the light direction and just construct a lobearound the normal
s
cosFn
n_, exp_, a_, b_
:
Max
n.
Cos
b
Sin
a
, Sin
b
Sin
a
, Cos
a

, 0.
^exp
Let's see how that looks like when we startaveraging different lobes around a bunch of normals
aggregateCosFn
Compile

nlist, _Real, 2
, exp, a, b
, Mean
Map
n
Max
n.
Cos
b
Sin
a
, Sin
b
Sin
a
, Cos
a

, 0.
^exp, nlist

;someTestNormals

0., 0., 1.
,
0., 1., 0.
,
0., 0.7071067811865475, 0.7071067811865475

;someTestData
Flatten
Table

a, b, aggregateCosFn
someTestNormals, 5., a, b

,
a, 0., Pi, 0.01
,
b,
Pi
2, Pi
2, 0.01

, 1
;

AbsoluteTimingtestPlot
SphericalPlot3D
aggregateCosFn
someTestNormals, 5., a, b
,
a,
Pi
2., Pi
2.
,
b, 0., Pi
, PlotRange
Full
Out[10]=
0.6400640, Null
CompiledFunction::cfsa:Argument a atposition 3 shouldbea machine
sizerealnumber.
normalmapFit.nb
3