POV-Ray : Newsgroups : povray.text.scene-files : Source code for "Noisy Bezier patch" : Source code for "Noisy Bezier patch" Server Time
29 Mar 2024 10:06:52 EDT (-0400)
  Source code for "Noisy Bezier patch"  
From: Tor Olav Kristensen
Date: 5 Mar 2002 03:12:16
Message: <3C847CB1.82A24D52@hotmail.com>
I just posted an image to povray.binaries.images:

http://news.povray.org/%3C3C847B5F.14443A02%40hotmail.com%3E
news://news.povray.org/3C847B5F.14443A02%40hotmail.com

Here's the source code for it.


Tor Olav


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// Copyright 2002 by Tor Olav Kristensen
// Email: tor### [at] hotmailcom
// http://www.crosswinds.net/~tok
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.5;

#include "functions.inc"
#include "colors.inc"

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

// Factorial
#macro Fact(N)

    #if (N = 0)
      1
    #else
      (
        N
        #if (N > 1)
          *Fact(N - 1)
        #end // if
      )
    #end // if

#end // macro Fact

// Permutations
#macro Perm(N, R)

   (Fact(N)/Fact(N - R))

#end // macro Perm

// Combinations
#macro Comb(N, R)

   (Fact(N)/Fact(R)/Fact(N - R))
   
#end // macro Comb


#macro BezierPatchFunction(PP, v0)

  #local Rows = dimension_size(PP, 1);
  #local Cols = dimension_size(PP, 2);
  #local NR = Rows - 1;
  #local NC = Cols - 1;

  function(u, v) {
    0
    #local CntR = 0;
    #while (CntR < Rows)
      #local CR = Comb(NR, CntR);
      #local ER = NR - CntR;
      + CR
      #if (CntR != 0)
        *u^CntR
      #end // if
      #if (ER != 0)
        *(1 - u)^ER
      #end // if
      *
      (
        0
        #local CntC = 0;
        #while (CntC < Cols)
          #local CC = Comb(NC, CntC);
          #local EC = NC - CntC;
          + CC
          #if (CntC != 0)
            *v^CntC
          #end // if
          #if (EC != 0)
            *(1 - v)^EC
          #end // if
          #local S = vdot(PP[CntR][CntC], v0);
          *S
          #local CntC = CntC + 1;
        #end // while
      )
      #local CntR = CntR + 1;
    #end // while
  }

#end // macro BezierPatchFunction

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

// Define grid of control points
#declare PQ =
  array[5][4] {
    { < 0,  0,  2>, < 1, 0,  0>, < 2,  0,  1>, < 3,  0,  2> },
    { < 0,  1, -2>, < 1, 1, -2>, < 2,  1, -4>, < 3,  1, -4> },
    { < 0,  2,  1>, < 1, 2, -3>, < 2,  2, -1>, < 3,  2,  4> },
    { < 0,  3,  0>, < 1, 3,  0>, < 2,  3,  0>, < 3,  3, -4> },
    { < 0,  4, -3>, < 1, 4,  3>, < 2,  4, -3>, < 3,  4, -2> }
  }

// Show control points
union {
  #declare CntA = 0;
  #while (CntA < dimension_size(PQ, 1))
    #declare CntB = 0;
    #while (CntB < dimension_size(PQ, 2))
      sphere { PQ[CntA][CntB], 0.1 }
      #declare CntB = CntB + 1;
    #end // while
    #declare CntA = CntA + 1;
  #end // while
  pigment { color Red*2 }
}

// Calculate the patch functions
#declare xFn = BezierPatchFunction(PQ, x)
#declare yFn = BezierPatchFunction(PQ, y)
#declare zFn = BezierPatchFunction(PQ, z)

// A noise pattern function
#declare BozoFn =
  function {
    pattern {
      bozo
      turbulence 0.3
      omega 0.5
      lambda 0.2
      scale <0.7, 0.3, 0.5>
    }
  }

// Amount of noise
#declare W = 0.5;

// Paramteric sampling of the noise function
#declare NoiseFn =
  function(u, v, A, B, C) {
    BozoFn(
      xFn(u, v) + A,
      yFn(u, v) + B,
      zFn(u, v) + C
    )*W
  }

/*
// A fast preview
union {
  #declare U = 0;
  #while (U < 1)
    #declare V = 0;
    #while (V < 1)
      sphere {
        <
          xFn(U, V),
          yFn(U, V),
          zFn(U, V)
        >
        +
        <
          NoiseFn(U, V, 10, 0, 0),
          NoiseFn(U, V, 0, 10, 0),
          NoiseFn(U, V, 0, 0, 10)
        >,
        0.05
      }
      #declare V = V + 0.01;
    #end // while
    #declare U = U + 0.01;
  #end // while
  pigment { color Yellow }
}
*/

// The real thing
parametric {
  function { xFn(u, v) + NoiseFn(u, v, 10, 0, 0) }
  function { yFn(u, v) + NoiseFn(u, v, 0, 10, 0) }
  function { zFn(u, v) + NoiseFn(u, v, 0, 0, 10) }
  <0, 0>, <1, 1>
  contained_by { box { -5*<1, 1, 1>, 5*<1, 1, 1> } }
  // max_gradient 4
  accuracy 0.005
  precompute 15 x, y, z
  pigment { color White }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

background { color Blue/2 }

light_source {
  <7, 1, -5>*10
  color White
  shadowless
}

camera {
  location -8*z
  look_at <2, 3, 0>
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.