DIRECTORY Rope USING [ ROPE, Equal ], Atom USING [ GetPropFromList ], Checksum USING [ ComputeChecksum ], Real USING [ Fix, RoundC ], RealFns USING [ Sin, Cos, Power ], ThreeDScenes USING [ VertexInfo, VtxToRealSeqProc, RGB ], TextureMaps USING [ GetTxtrAt ], Tilers USING [ AddHighlight ], ScanConvert USING [ RealSequence, GetColorProc, Spot, IntRGBT, Extend ], SolidTextures; SolidTexturesImpl: CEDAR PROGRAM IMPORTS Rope, Atom, Checksum, Real, RealFns, ScanConvert, TextureMaps, Tilers, SolidTextures EXPORTS SolidTextures ~ BEGIN RealSequence: TYPE ~ ScanConvert.RealSequence; RGB: TYPE ~ ThreeDScenes.RGB; SolidTexturesError: PUBLIC SIGNAL [reason: ATOM] = CODE; RopeToProc: PUBLIC PROC[ name: Rope.ROPE] RETURNS[ScanConvert.GetColorProc] ~ { SELECT TRUE FROM Rope.Equal[ "Spots", name, FALSE] => RETURN[SolidTextures.Spots]; Rope.Equal[ "Wurlitzer", name, FALSE] => RETURN[SolidTextures.Wurlitzer]; Rope.Equal[ "TwistedStripes", name, FALSE] => RETURN[SolidTextures.TwistedStripes]; Rope.Equal[ "BurlWood", name, FALSE] => RETURN[SolidTextures.BurlWood]; Rope.Equal[ "ZebraBurl", name, FALSE] => RETURN[SolidTextures.ZebraBurl]; Rope.Equal[ "Marble", name, FALSE] => RETURN[SolidTextures.Marble]; ENDCASE; SIGNAL SolidTexturesError[$ProcNotFound]; RETURN[NIL]; }; ProcToRope: PUBLIC PROC[ proc: ScanConvert.GetColorProc] RETURNS[Rope.ROPE] ~ { SELECT TRUE FROM proc = SolidTextures.Spots => RETURN["Spots"]; proc = SolidTextures.Wurlitzer => RETURN["Wurlitzer"]; proc = SolidTextures.TwistedStripes => RETURN["TwistedStripes"]; proc = SolidTextures.BurlWood => RETURN["BurlWood"]; proc = SolidTextures.ZebraBurl => RETURN["ZebraBurl"]; proc = SolidTextures.Marble => RETURN["Marble"]; ENDCASE; SIGNAL SolidTexturesError[$ProcNotFound]; RETURN[NIL]; }; GetLerpedVals: PUBLIC ThreeDScenes.VtxToRealSeqProc ~ { IF dest = NIL OR dest.maxLength < dest.length+3 THEN dest _ ScanConvert.Extend[ dest, dest.length+3]; dest[dest.length ] _ source.coord.x; dest[dest.length+1] _ source.coord.y; dest[dest.length+2] _ source.coord.z; dest.length _ dest.length+3; RETURN [dest]; }; Spots: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddSpots] ]; }; Wurlitzer: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddWurlitzer] ]; }; TwistedStripes: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddTwistedStripes] ]; }; BurlWood: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddBurlWood] ]; }; ZebraBurl: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddZebraBurl] ]; }; Marble: PUBLIC ScanConvert.GetColorProc ~ { RETURN[ RecoverColor[spot, AddMarble] ]; }; RecoverColor: PROC[spot: ScanConvert.Spot, proc: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ] RETURNS[ScanConvert.IntRGBT] ~ { IF Atom.GetPropFromList[spot.props, $TextureMap] # NIL THEN spot _ TextureMaps.GetTxtrAt[spot]; -- modify with mapped texture spot _ proc[spot]; -- modify with solid texture spot _ Tilers.AddHighlight[spot]; -- get shading RETURN[ [ r: Real.RoundC[spot.val[0]*255.0], g: Real.RoundC[spot.val[1]*255.0], b: Real.RoundC[spot.val[2]*255.0], t: Real.RoundC[spot.val[3]*255.0] ] ]; }; AddSpots: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; intensity: REAL _ RealFns.Sin[10.0 * spot.val[x] ] * RealFns.Sin[14.0 * spot.val[y] ] * RealFns.Sin[20.0 * spot.val[z] ]; intensity _ (intensity + 1.0) / 2.0; spot.val[r] _ spot.val[r] * intensity; spot.val[g] _ spot.val[g] * intensity; spot.val[b] _ spot.val[b] * intensity; spot.val[t] _ spot.val[t] * intensity; RETURN[spot]; }; AddWurlitzer: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; intensity: REAL _ RealFns.Sin[10.0 * spot.val[x] ] * RealFns.Sin[14.0 * spot.val[y] ] * RealFns.Sin[20.0 * spot.val[z] ]; intensity _ (intensity + 1.0) / 2.0; spot.val[r] _ spot.val[r] * (RealFns.Sin[10.0*spot.val[x]] +1.0) / 2.0; spot.val[g] _ spot.val[g] * (RealFns.Sin[14.0*spot.val[y]] +1.0) / 2.0; spot.val[b] _ spot.val[b] * (RealFns.Sin[20.0*spot.val[z]] +1.0) / 2.0; spot.val[t] _ spot.val[t] * intensity; RETURN[spot]; }; AddTwistedStripes: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; angle: REAL _ 3.1416 * spot.val[z]; -- rotation varies with z cosAngle: REAL _ RealFns.Cos[angle]; sinAngle: REAL _ RealFns.Sin[angle]; intensity: REAL _ RealFns.Sin[40.0 * -- x component of rotated x-y vector (cosAngle * spot.val[x] + sinAngle * spot.val[y]) ]; intensity _ (intensity + 1.0) / 2.0; spot.val[r] _ spot.val[r] * intensity; spot.val[g] _ spot.val[g] * (1.0 - intensity); spot.val[2] _ spot.val[b] * intensity; spot.val[t] _ spot.val[t] * (1.0 - intensity); RETURN[spot]; }; AddBurlWood: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; red, grn, blu: REAL; chaos: REAL _ Chaos[ spot.val[x], spot.val[y], spot.val[z] ]; midBrown: REAL _ RealFns.Sin[ chaos*8 + 7*spot.val[x] + 3* spot.val[y] ]; brownLayer: REAL _ ABS[ RealFns.Sin[midBrown] ]; greenLayer: REAL _ - brownLayer; perturb: REAL _ IF brownLayer > 0.0 THEN ABS[RealFns.Sin[40 * chaos + 50*spot.val[z] ]] ELSE ABS[RealFns.Sin[30 * chaos + 30*spot.val[x] ]]; brownPerturb: REAL _ perturb * .6 + .3; -- perturb up to .6 greenPerturb: REAL _ perturb * .2 + .8; -- perturb up to .2 grnPerturb: REAL _ perturb * .15 + .85; -- perturb up to .15 grn _ .5 * RealFns.Power[ABS[brownLayer], 0.3]; -- makes seams brownLayer _ RealFns.Power[(brownLayer + 1.0) / 2.0, 0.6] * brownPerturb; greenLayer _ RealFns.Power[(greenLayer + 1.0) / 2.0, 0.6] * greenPerturb; red _ (.6 * brownLayer + .35 * greenLayer) * 2 * grn; blu _ (.25 * brownLayer + .35 * greenLayer) * 2 * grn; grn _ grn * MAX[brownLayer, greenLayer] * grnPerturb; spot.val[r] _ spot.val[r] * red; spot.val[g] _ spot.val[g] * grn; spot.val[b] _ spot.val[b] * blu; RETURN[spot]; }; AddZebraBurl: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; red, grn, blu: REAL; chaos: REAL _ Chaos[ spot.val[x], spot.val[y], spot.val[z] ]; midBrown: REAL _ RealFns.Sin[ chaos*8 + 7*spot.val[x] + 3* spot.val[y] ]; brownLayer: REAL _ RealFns.Sin[midBrown]; greenLayer: REAL _ - brownLayer; perturb: REAL _ IF brownLayer > 0.0 THEN ABS[RealFns.Sin[40 * chaos + 50*spot.val[z] ]] ELSE ABS[RealFns.Sin[24 * chaos + 30*spot.val[x] ]]; brownPerturb: REAL _ perturb * .6 + .3; -- perturb up to .6 greenPerturb: REAL _ perturb * .2 + .8; -- perturb up to .2 grnPerturb: REAL _ perturb * .15 + .85; -- perturb up to .15 grn _ .5 * RealFns.Power[ABS[brownLayer], 0.3]; -- makes seams brownLayer _ RealFns.Power[(brownLayer + 1.0) / 2.0, 0.6] * brownPerturb; greenLayer _ RealFns.Power[(greenLayer + 1.0) / 2.0, 0.6] * greenPerturb; red _ (.6 * brownLayer + .35 * greenLayer) * 2 * grn; blu _ (.25 * brownLayer + .35 * greenLayer) * 2 * grn; grn _ grn * MAX[brownLayer, greenLayer] * grnPerturb; spot.val[r] _ spot.val[r] * red; spot.val[g] _ spot.val[g] * grn; spot.val[b] _ spot.val[b] * blu; RETURN[spot]; }; AddMarble: PROC[spot: ScanConvert.Spot] RETURNS[ScanConvert.Spot] ~ { x: NAT _ spot.val.length-3; y: NAT _ x+1; z: NAT _ x+2; -- object space coordinate r: NAT ~ 0; g: NAT ~ 1; b: NAT ~ 2; t: NAT ~ 3; intensity: REAL _ RealFns.Sin[Chaos[ spot.val[x], spot.val[y], spot.val[z] ]*8 + 7*spot.val[z]]; intensity _ (intensity + 1.0) / 2.0; intensity _ RealFns.Power[intensity, 0.77]; spot.val[r] _ spot.val[r] * intensity; spot.val[g] _ spot.val[g] * intensity; spot.val[b] _ spot.val[b] * intensity; RETURN[spot]; }; Chaos: PUBLIC PROC[x, y, z: REAL] RETURNS [REAL] ~ { f: REAL _ 1.; s, t: REAL _ 0.; FOR n: INT IN [0..7) DO s _ Noise[x * f, y * f, z * f]; t _ t + ABS[s] / f; f _ 2 * f; ENDLOOP; RETURN [t]; }; realScale: REAL _ 2.0 / LAST[CARDINAL]; RTable: TYPE ~ RECORD[SEQUENCE length: NAT OF REAL]; rTable: REF RTable _ NIL; Noise: PUBLIC PROC[vx, vy, vz: REAL] RETURNS [REAL] ~ { R: PROC[i, j, k: REAL] RETURNS [CARDINAL] ~ TRUSTED { A: TYPE ~ ARRAY [0..3) OF REAL; a: A _ [i * .12345 , j * .12345 , k * .12345 ]; aPointer: LONG POINTER ~ @a; h: CARDINAL _ Checksum.ComputeChecksum[nWords: SIZE[A], p: aPointer]; RETURN [h]; }; SCurve: PROC[x: REAL] RETURNS [REAL] ~ { RETURN [x * x * (3 - 2 * x)]; }; m: NAT; ix, iy, iz: INT; x, y, z, jx, jy, jz, sx, sy, sz, tx, ty, tz, s, f: REAL; IF rTable = NIL THEN { rTable _ NEW[RTable[259]]; FOR n:INT IN [0..259) DO r:REAL _ n; rTable[n] _ R[r, r, r] * realScale - 1.; ENDLOOP; }; x _ vx + 1000.; y _ vy + 1000.; z _ vz + 1000.; ix _ Real.Fix[x]; iy _ Real.Fix[y]; iz _ Real.Fix[z]; sx _ SCurve[x - ix]; sy _ SCurve[y - iy]; sz _ SCurve[z - iz]; tx _ 1. - sx; ty _ 1. - sy; tz _ 1. - sz; f _ 0.; -- initialize sum to zero. FOR n: INT IN [0..8) DO -- sum together 8 local fields from neighboring lattice pts. SELECT n FROM -- each of 8 corners of the surrounding unit cube. 0 => {jx _ ix ; jy _ iy ; jz _ iz ; s _ tx * ty * tz }; 1 => {jx _ ix+1 ; s _ sx * ty * tz }; 2 => {jx _ ix ; jy _ iy+1 ; s _ tx * sy * tz }; 3 => {jx _ ix+1 ; s _ sx * sy * tz }; 4 => {jx _ ix ; jy _ iy ; jz _ iz+1 ; s _ tx * ty * sz }; 5 => {jx _ ix+1 ; s _ sx * ty * sz }; 6 => {jx _ ix ; jy _ iy+1 ; s _ tx * sy * sz }; 7 => {jx _ ix+1 ; s _ sx * sy * sz }; ENDCASE; m _ R[jx, jy, jz] MOD 256; f _ f + s * ( rTable[m]/2 + rTable[m+1]*(x-jx) + rTable[m+2]*(y-jy) + rTable[m+3]*(z-jz) ); ENDLOOP; RETURN [f]; }; END. NSolidTexturesImpl.mesa Copyright c 1986 by Xerox Corporation. All rights reserved. Last Edited by: Crow, April 14, 1986 1:12:51 pm PST Perlin, August 5, 1985 0:23:18 am PDT PROC[dest: REF RealSequence, source: VertexInfo] RETURNS[REF RealSequence]; Regular array of dark spots Wurlitzer colors, stripes in 3-d Rotating stripes (barber pole) Perlin's marble texture returns band limited noise over R3. map the unit interval into an "S shaped" cubic f[x] | f[0]=0, f'[0]=0, f[1]=1, f'[1]=0. declare local variables. initialize random gradient table Force everything to be positive ixyz _ the integer lattice point "just below" v (identifies the surrounding unit cube). sxyz _ the vector difference v - ixyz biased with an S-Curve in each dimension. txyz _ the complementary set of S-Curves in each dimension. Add in each weighted component Κ k˜headšœ™Icodešœ Οmœ1™˜‹Jšœ$˜$Jšœ&˜&Jšœ.˜.Jšœ&˜&Jšœ.˜.Jšžœ˜ Jšœ˜—šŸ œžœžœ˜GJšœžœžœžœ’˜XJš œžœ žœ žœ žœ˜8Jšœžœ˜Jšœžœ3˜>Jšœ žœ<˜JJšœ žœžœ˜0Jšœ žœ˜ J˜šœ žœžœ˜$Jšžœ+˜3Jšžœžœ,˜4—Jšœžœ’˜Jšœžœ’˜?JšœI˜IJšœI˜IJšœ6˜6Jšœ7˜7Jšœ žœ'˜6Jšœ ˜ Jšœ ˜ Jšœ ˜ Jšžœ˜ Jšœ˜—šŸ œžœžœ˜HJšœžœžœžœ’˜XJš œžœ žœ žœ žœ˜8Jšœžœ˜Jšœžœ3˜>Jšœ žœ<˜JJšœ žœ˜)Jšœ žœ˜ J˜šœ žœžœ˜$Jšžœ+˜3Jšžœžœ,˜4—Jšœžœ’˜Jšœžœ’˜?JšœI˜IJšœI˜IJšœ6˜6Jšœ7˜7Jšœ žœ'˜6Jšœ ˜ Jšœ ˜ Jšœ ˜ Jšžœ˜ Jšœ˜—š  œžœžœ˜EJ™Jšœžœžœžœ’˜XJš œžœ žœ žœ žœ˜8Jšœ žœh˜wJšœ$˜$Jšœ+˜+Jšœ&˜&Jšœ&˜&Jšœ&˜&Jšžœ˜ Jšœ˜—š Ÿœžœžœ žœžœžœ˜4Jšœžœ˜ Jšœžœ˜šžœžœžœž˜Jšœ ˜ J˜J˜ Jšžœ˜—Jšžœ˜ J˜—J˜Lšœ žœ žœžœ˜'L˜4˜L˜—š ŸœžΟsžœžœžœ˜7šœ#™#J˜š Ÿœžœ žœžœžœžœ˜5Jš œžœžœžœžœ˜Lšœ/˜/Lšœ žœžœ˜Lšœžœ$žœ˜ELšžœ˜ Jšœ˜—š Ÿœžœžœžœžœ˜(šœW™WJ˜Jšžœ˜—˜J˜——šœ™Jšœ˜Jšœ žœ˜Jšœ3žœ˜8—šœ ™ šžœ žœžœ˜Jšœ žœ˜šžœžœžœ ž˜J˜ J˜(Jšžœ˜—J˜——™Jšœ˜Jšœ˜Jšœ˜—šœΟdœS™WJšœ˜Jšœ˜Jšœ˜—šœ€œ€œ*™OJ˜J˜J˜—šœ€œ7™;J˜ J˜ J˜ —Jšœ’˜"š žœžœžœžœ’<˜Tšžœžœ’2˜@Jšœ:˜:Jšœ.˜.Jšœ5˜5Jšœ.˜.Jšœ;˜;Jšœ.˜.Jšœ4˜4Jšœ-˜-Jšžœ˜—šœ™J˜Jšœ_˜_—Jšžœ˜—Jšžœ˜ —Jšœ˜—Jšžœ˜M˜——…—(6»