Next: , Previous: Maths, Up: Functions



12.7 PrimitiveData

Primitive data (pdata for short) is fluxus' name for data which comprises primitives. In polygon primitives this means the vertex information, in particle primitives it corresponds to the particle information, in NURBS primitives it's the control vertices. Access to pdata gives you the ability to use primitives which are otherwise not very interesting, and deform and shape other primitives to give much more detailed models and animations. You can also add your own pdata, which is treated exactly like the built in types. Primitive data is named by type strings, the names of which depend on the sort of primitive. All pdata commands operate on the currently grabbed primitive.

Example
      ; a function to deform the points of an object
      (define (deform n)
          (pdata-set "p" n (vadd  (pdata-get "p" n)                ; the original point, plus
              (vmul (vector (flxrnd) (flxrnd) (flxrnd)) 0.1)))     ; a small random vector
          (if (zero? n)
              0
              (deform (- n 1))))
     
      (hint-unlit) ; set some render settings to
      (hint-wire)  ; make things easier to see
      (line-width 4)
      (define myobj (build-sphere 10 10)) ; make a sphere
      (grab myobj)
      (deform (pdata-size)) ; deform it
      (ungrab)
     

12.7.1 (pdata-get type-string index-number)

Returns

value-vector/colour/matrix/number

Description

Returns the corresponding pdata element.

Example
      (pdata-get "p" 1)
     

12.7.2 (pdata-set type-string index-number value-vector/colour/matrix/number)

Returns

void

Description

Writes to the corresponding pdata element.

Example
      (pdata-get "p" 1)
     

12.7.3 (pdata-add type-string name-string)

Returns

void

Description

Adds a new user pdata array. Type is one of "v":vector, "c":colour, "f":float or "m":matrix.

Example
      (pdata-add "v" "mydata")
      (pdata-set "mydata" 0 (vector 1 2 3))
     

12.7.4 (pdata-op funcname-string pdataname-string operator)

Returns

void

Description

This is an experimental feature allowing you to do operations on pdata very quickly, for instance adding element for element one array of pdata to another. You can implement this in Scheme as a loop over each element, but this is slow as the interpreter is doing all the work. It's much faster if you can use a pdata-op as the same operation will only be one Scheme call.

Example
      (pdata-op "+" "mydata" (vector 1 2 3))  add a vector to all the pdata vectors
      (pdata-op "+" "mydata" "myotherdata")  add two pdata vectors element for element
      (pdata-op "*" "mydata" (vector 1 2 3))  multiply a vector to all the pdata vectors
      (pdata-op "*" "mydata" "myotherdata")  multiply two pdata vectors element for element
      (pdata-op "closest" "p" (vector 100 0 0))  returns position of the closest vertex to this point
      (pdata-op "sin" "mydata" "myotherdata")  sine of one float pdata to another
      (pdata-op "cos" "mydata" "myotherdata")  cosine of one float pdata to another
     

12.7.5 (pdata-copy pdatafrom-string pdatato-string)

Returns

void

Description

Copies the contents of one pdata array to another. Arrays must match types.

Example
      (pdata-copy "p" "mydata")  copy the vertex positions to a user array
     

12.7.6 (pdata-size)

Returns

count-number

Description

Returns the size of the pdata arrays (they must all be the same). This is mainly used for iterating over the arrays.

Example
      (define (mashup n)
          (pdata-set "p" n (vector (flxrnd) (flxrnd) (flxrnd))) ; randomise the vertex position
          (if (zero? n)
              0
              (mashup (- n 1)))) ; loops till n is 0
     
      (define shape (build-sphere 10 10))
      (grab shape)
      (mashup (pdata-size)) ; randomise verts on currently grabbed primitive
      (ungrab)
     

12.7.7 (finalise)

Returns

void

Description

Doesn't do anything anymore, I need to remove this :)

Example
     

12.7.8 (recalc-normals smoothornot-number)

Returns

void

Description

For polygon primitives only. Looks at the vertex positions and calculates the lighting normals for you automatically. Call with "1" for smooth normals, "0" for faceted normals.

Example
      (define shape (build-sphere 10 10)) ; build a sphere (which is smooth by default)
      (grab shape)
      (recalc-normals 0) ; make the sphere faceted
      (ungrab)