PrOGL
processing open-gl interface
Loading...
Searching...
No Matches
Uniform.pde
Go to the documentation of this file.
1// uniform.pde
2// samantha jane
3// manages uniforms and updating
4//------------------------------------------------------------------------------
5
6/** interface to store lambda expression. */
7import java.util.function.Function;
8
9/** uniform underlying data type. */
10public static enum uniform_type {
11 INT, /**< integer */
12 FLOAT, /**< float */
13 BOOL, /**< boolean */
14 VEC2, /**< 2D vector */
15 VEC3, /**< 3D vector */
16 VEC4, /**< 4D vector */
17 TEXTURE, /**< 2D texture */
18 MAT2, /**< 2D matrix */
19 MAT3, /**< 3D matrix */
20 MAT4 /**< 4D matrix */
22
23/**
24 * organizes information for a single Uniform, including
25 * its underlying data type and update behavior.
26 */
27class Uniform<T> {
28
29 String name; /**< name used to reference uniform */
30 T val; /**< data value */
31 uniform_type type; /**< data type of the val member */
32 Function<T, T> update_function = t -> t; /**< a pass by value update function*/
33 boolean update_flag; /**< wether to update or not */
34
35 /**
36 * constant Uniform constructor.
37 * this constructor will initialize the uniform and set update_flag to false.
38 *
39 * @param name the Uniform name as referenced in the glsl shader code
40 * @param val the value to initialize the Uniform with
41 * @param type the data type of val
42 */
44 this.name = name;
45 this.val = val;
46 this.type = type;
47 this.update_flag = false;
48 }
49
50 /**
51 * updatable Uniform constructor.
52 * this constructor will initialize the uniform with an update_function,
53 * setting update_flag to true.
54 *
55 * @param name the Uniform name as referenced in the glsl shader code
56 * @param val the value to initialize the Uniform with
57 * @param type the data type of val
58 * @param update_function a lambda function, taking, updating, and returning
59 * the data type of this Uniform.
60 */
61 Uniform(String name, T val, uniform_type type, Function<T, T> update_function) {
62 this.name = name;
63 this.val = val;
64 this.type = type;
65 this.update_function = update_function;
66 this.update_flag = true;
67 }
68
69 /**
70 * update a uniforms value if applicable, ie. `update_flag = true`.
71 */
72 void update() {
73 if (update_flag) {
74 val = update_function.apply(val); // update function must take what it returns
75 } else {
76 println("[ERROR!] tried to update without an update function");
77 }
78 }
79
80 /**
81 * set this uniform within a given program.
82 */
83 void set_uniform(PShader program) {
84 switch (type) {
85 case INT:
86 program.set(name, (int)val);
87 break;
88 case FLOAT:
89 program.set(name, (float)val);
90 break;
91 case VEC2:
92 ArrayList<Float> v2val = (ArrayList<Float>)val;
93 program.set(
94 name,
95 v2val.get(0),
96 v2val.get(1)
97 );
98 break;
99 case VEC3:
100 ArrayList<Float> v3val = (ArrayList<Float>)val;
101 program.set(
102 name,
103 v3val.get(0),
104 v3val.get(1),
105 v3val.get(2)
106 );
107 break;
108 case VEC4:
109 ArrayList<Float> v4val = (ArrayList<Float>)val;
110 program.set(
111 name,
112 v4val.get(0),
113 v4val.get(1),
114 v4val.get(2),
115 v4val.get(3)
116 );
117 break;
118 case TEXTURE:
119 Texture tex = (Texture)val;
120 program.set(name, tex.img);
121 break;
122 // TODO implement mat2-4
123 default:
124 println("[ERROR!] unrecognized uniform data type\n");
125 break;
126 }
127 }
128
129 /**
130 * print out data structure for debugging.
131 */
132 void print() {
133 println("Uniform "+name+":");
134 println("- val : "+val);
135 println("- type : "+type);
136 println("- update : "+update_flag);
137 if (update_flag) {
138 println("- func : "+update_function);
139 }
140 println("----------------");
141 }
142}
the Texture class serves a wrapper for a PImage and image path.
Definition Texture.pde:9
PImage img
image once loaded into memory
Definition Texture.pde:10
organizes information for a single Uniform, including its underlying data type and update behavior.
Definition Uniform.pde:27
Uniform(String name, T val, uniform_type type, Function< T, T > update_function)
updatable Uniform constructor.
Definition Uniform.pde:61
Function< T, T > update_function
a pass by value update function
Definition Uniform.pde:32
void print()
print out data structure for debugging.
Definition Uniform.pde:132
uniform_type type
data type of the val member
Definition Uniform.pde:31
T val
data value
Definition Uniform.pde:30
Uniform(String name, T val, uniform_type type)
constant Uniform constructor.
Definition Uniform.pde:43
void set_uniform(PShader program)
set this uniform within a given program.
Definition Uniform.pde:83
String name
name used to reference uniform
Definition Uniform.pde:29
void update()
update a uniforms value if applicable, ie.
Definition Uniform.pde:72
boolean update_flag
wether to update or not
Definition Uniform.pde:33
interface to store lambda expression.
Definition Uniform.pde:10
MAT2
2D matrix
Definition Uniform.pde:18
VEC2
2D vector
Definition Uniform.pde:14
VEC3
3D vector
Definition Uniform.pde:15
TEXTURE
2D texture
Definition Uniform.pde:17
MAT3
3D matrix
Definition Uniform.pde:19
INT
integer
Definition Uniform.pde:11
VEC4
4D vector
Definition Uniform.pde:16
BOOL
boolean
Definition Uniform.pde:13