Representation of a GLSL function Objects of this class can be used for re-using and composing GLSL snippets. Each Function consists of a GLSL snippet in the form of a function. The code may have template variables that start with the dollar sign. These stubs can be replaced with ex
| 32 | |
| 33 | |
| 34 | class Function(ShaderObject): |
| 35 | """Representation of a GLSL function |
| 36 | |
| 37 | Objects of this class can be used for re-using and composing GLSL |
| 38 | snippets. Each Function consists of a GLSL snippet in the form of |
| 39 | a function. The code may have template variables that start with |
| 40 | the dollar sign. These stubs can be replaced with expressions using |
| 41 | the index operation. Expressions can be: |
| 42 | |
| 43 | * plain text that is inserted verbatim in the code |
| 44 | * a Function object or a call to a funcion |
| 45 | * a Variable (or Varying) object |
| 46 | * float, int, tuple are automatically turned into a uniform Variable |
| 47 | * a VertexBuffer is automatically turned into an attribute Variable |
| 48 | |
| 49 | All functions have implicit "$pre" and "$post" placeholders that may be |
| 50 | used to insert code at the beginning and end of the function. |
| 51 | |
| 52 | Examples |
| 53 | -------- |
| 54 | This example shows the basic usage of the Function class:: |
| 55 | |
| 56 | vert_code_template = Function(''' |
| 57 | void main() { |
| 58 | gl_Position = $pos; |
| 59 | gl_Position.x += $xoffset; |
| 60 | gl_Position.y += $yoffset; |
| 61 | }''') |
| 62 | |
| 63 | scale_transform = Function(''' |
| 64 | vec4 transform_scale(vec4 pos){ |
| 65 | return pos * $scale; |
| 66 | }''') |
| 67 | |
| 68 | # If you get the function from a snippet collection, always |
| 69 | # create new Function objects to ensure they are 'fresh'. |
| 70 | vert_code = Function(vert_code_template) |
| 71 | trans1 = Function(scale_transform) |
| 72 | trans2 = Function(scale_transform) # trans2 != trans1 |
| 73 | |
| 74 | # Three ways to assign to template variables: |
| 75 | # |
| 76 | # 1) Assign verbatim code |
| 77 | vert_code['xoffset'] = '(3.0 / 3.1415)' |
| 78 | |
| 79 | # 2) Assign a value (this creates a new uniform or attribute) |
| 80 | vert_code['yoffset'] = 5.0 |
| 81 | |
| 82 | # 3) Assign a function call expression |
| 83 | pos_var = Variable('attribute vec4 a_position') |
| 84 | vert_code['pos'] = trans1(trans2(pos_var)) |
| 85 | |
| 86 | # Transforms also need their variables set |
| 87 | trans1['scale'] = 0.5 |
| 88 | trans2['scale'] = (1.0, 0.5, 1.0, 1.0) |
| 89 | |
| 90 | # You can actually change any code you want, but use this with care! |
| 91 | vert_code.replace('gl_Position.y', 'gl_Position.z') |
no outgoing calls
searching dependent graphs…