Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 411e57b

Browse files
committed
WaveFront reader also uses normals from file.
1 parent 299d829 commit 411e57b

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

examples/substrate.bling

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ filter mitchell 3 3 0.333333 0.333333
55
filter box
66

77
imageSize 1280 720
8-
# imageSize 640 360
8+
imageSize 640 360
99

1010
renderer {
1111
sampler
1212
sampled {
13-
sampler { stratified 8 8 }
13+
sampler { stratified 2 2 }
1414
integrator { path maxDepth 7 sampleDepth 3 }
1515
}
1616
}
@@ -43,6 +43,7 @@ material {
4343
matte
4444
kd {
4545
graphPaper 0.1
46+
map { uv 0 0 1 1 }
4647
tex1 { constant rgbR 0.8 0.8 0.8 }
4748
tex2 { constant rgbR 0.05 0.05 0.05 }
4849
}

src/Graphics/Bling/IO/WaveFront.hs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import Control.Monad.Trans.Class (lift)
2222
import qualified Data.Vector.Unboxed as V
2323
import qualified Data.Vector.Unboxed.Mutable as MV
2424

25-
import Debug.Trace
26-
2725
type STUGrowVec s a = GrowVec (MV.MVector) s a
2826

2927
data WFState s = WFState
@@ -48,17 +46,38 @@ wfTriVerts d i = (p1, p2, p3) where
4846
p3 = (wfPoints d) V.! (let (i', _, _) = (wfFaces d) V.! (i+2) in i')
4947

5048
wfTriUVs :: WFData -> Int -> TriUVs
51-
wfTriUVs d i = triangleNoUVs where -- (u1, v1, u2, v2, u3, v3) where
52-
(u1, v1) = (wfTexCoords d) V.! (let (_, i', _) = (wfFaces d) V.! i in i')
53-
(u2, v2) = (wfTexCoords d) V.! (let (_, i', _) = (wfFaces d) V.! (i+1) in i')
54-
(u3, v3) = (wfTexCoords d) V.! (let (_, i', _) = (wfFaces d) V.! (i+2) in i')
49+
wfTriUVs d i
50+
| i1 >= 0 && i2 >= 0 && i3 >= 0 = (u1, v1, u2, v2, u3, v3)
51+
| otherwise = triangleDefaultUVs
52+
where
53+
(u1, v1) = (wfTexCoords d) V.! i1
54+
(u2, v2) = (wfTexCoords d) V.! i2
55+
(u3, v3) = (wfTexCoords d) V.! i3
56+
57+
i1 = let (_, i', _) = (wfFaces d) V.! (i+0) in i'
58+
i2 = let (_, i', _) = (wfFaces d) V.! (i+1) in i'
59+
i3 = let (_, i', _) = (wfFaces d) V.! (i+1) in i'
60+
61+
wfTriNormals :: WFData -> Int -> Maybe TriNorms
62+
wfTriNormals d i
63+
| i1 < 0 && i2 < 0 && i3 < 0 = Nothing
64+
| otherwise = Just (n1, n2, n3)
65+
where
66+
n1 = (wfNormals d) V.! i1
67+
n2 = (wfNormals d) V.! i2
68+
n3 = (wfNormals d) V.! i3
69+
70+
i1 = let (_, _, i') = (wfFaces d) V.! (i+0) in i'
71+
i2 = let (_, _, i') = (wfFaces d) V.! (i+1) in i'
72+
i3 = let (_, _, i') = (wfFaces d) V.! (i+1) in i'
5573

5674
mkWFTri :: Material -> WFData -> Int -> Primitive
5775
mkWFTri !mat !d !i = prim where
58-
prim = Primitive tint ints bounds Nothing (flip const)
76+
prim = Primitive tint ints bounds Nothing shade
5977
tint = triangleIntersect mat prim (wfTriVerts d i) (wfTriUVs d i)
6078
ints = triangleIntersects (wfTriVerts d i)
6179
bounds = triangleBounds (wfTriVerts d i)
80+
shade = maybe (flip const) triangleShadingGeometry (wfTriNormals d i)
6281

6382
initialState :: ST s (WFState s)
6483
initialState = do

src/Graphics/Bling/Math.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ solveQuadric
128128
-> Maybe (Float, Float)
129129
{-# INLINE solveQuadric #-}
130130
solveQuadric a b c
131-
| discrim <= 0 = Nothing
131+
| discrim < 0 = Nothing
132132
| otherwise = Just (min t0 t1, max t0 t1)
133133
where
134134
(t0, t1) = (q / a, c / q)

src/Graphics/Bling/Primitive/KdTree.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ mkKdTree psl = {-# SCC "mkKdTree" #-} KdTree bounds root where
111111

112112
buildTree :: AABB -> V.Vector Primitive -> Int -> KdTreeNode
113113
buildTree bounds ps depth
114-
| depth == 0 || V.length ps <= 1 = {-# SCC "buildTree.leaf" #-} Leaf ps
115-
| otherwise = fromMaybe (Leaf ps) $ trySplit bounds ps depth
114+
| depth == 0 || V.length ps <= 1 = {-# SCC "buildTree.leaf" #-} leaf
115+
| otherwise = fromMaybe leaf $ trySplit bounds ps depth
116+
where
117+
leaf = Leaf $ V.force ps
116118

117119
trySplit :: AABB -> V.Vector Primitive -> Int -> Maybe KdTreeNode
118120
trySplit bounds bps depth = {-# SCC "trySplit" #-} go Nothing axii where

src/Graphics/Bling/Primitive/TriangleMesh.hs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
module Graphics.Bling.Primitive.TriangleMesh (
33

44
-- * Triangle
5-
TriVerts, TriUVs, triangleIntersects, triangleIntersect, triangleBounds,
6-
triangleNoUVs,
7-
5+
TriVerts, TriNorms, TriUVs, triangleIntersects, triangleIntersect,
6+
triangleShadingGeometry, triangleBounds, triangleDefaultUVs,
7+
88
-- * Triangle Mesh
99

1010
mkTriangleMesh, triangulate
@@ -89,7 +89,7 @@ triNormals i m =
8989

9090
triUVs :: Int -> Mesh -> (Float, Float, Float, Float, Float, Float)
9191
{-# INLINE triUVs #-}
92-
triUVs idx m = maybe triangleNoUVs go (muvs m) where
92+
triUVs idx m = maybe triangleDefaultUVs go (muvs m) where
9393
(o1, o2, o3) = triOffsets idx m
9494

9595
go uvs = (muv (2 * o1), muv (2 * o1 + 1),
@@ -110,25 +110,29 @@ mkTri mesh n
110110

111111
sg o2w dgg
112112
| isNothing $ mns mesh = dgg
113-
| otherwise = dgg { dgN = ns, dgDPDU = ss, dgDPDV = ts }
114-
where
115-
(b1, b2) = fromJust $ dgtriB dgg
116-
b0 = 1 - b1 - b2
117-
(n0, n1, n2) = triNormals idx mesh
118-
ns = normalize $ transNormal o2w ns'
119-
ns' = (b0 *# n0) + (b1 *# n1) + (b2 *# n2)
120-
ss' = normalize $ dgDPDU dgg
121-
ts' = ss' `cross` ns
122-
(ss, ts) = if sqLen ts' > 0
123-
then (normalize ts' `cross` ns, normalize ts')
124-
else coordinateSystem'' ns
113+
| otherwise = triangleShadingGeometry (triNormals idx mesh) o2w dgg
125114

126115
type TriVerts = (Point, Point, Point)
116+
type TriNorms = (Normal, Normal, Normal)
127117
type TriUVs = (Float, Float, Float, Float, Float, Float)
128118

129-
triangleNoUVs :: TriUVs
130-
triangleNoUVs = (0, 0, 1, 0, 1, 1)
119+
triangleDefaultUVs :: TriUVs
120+
triangleDefaultUVs = (0, 0, 1, 0, 1, 1)
131121

122+
triangleShadingGeometry :: TriNorms -> Transform -> DifferentialGeometry -> DifferentialGeometry
123+
{-# INLINE triangleShadingGeometry #-}
124+
triangleShadingGeometry (n0, n1, n2) o2w dgg = dgg { dgN = ns, dgDPDU = ss, dgDPDV = ts }
125+
where
126+
(b1, b2) = fromJust $ dgtriB dgg
127+
b0 = 1 - b1 - b2
128+
ns = normalize $ transNormal o2w ns'
129+
ns' = (b0 *# n0) + (b1 *# n1) + (b2 *# n2)
130+
ss' = normalize $ dgDPDU dgg
131+
ts' = ss' `cross` ns
132+
(ss, ts) = if sqLen ts' > 0
133+
then (normalize ts' `cross` ns, normalize ts')
134+
else coordinateSystem'' ns
135+
132136
triangleBounds :: TriVerts -> AABB
133137
{-# INLINE triangleBounds #-}
134138
triangleBounds (p1, p2, p3) = foldl' extendAABBP emptyAABB [p1, p2, p3]

0 commit comments

Comments
 (0)