PrOGL
processing open-gl interface
Loading...
Searching...
No Matches
Shader.pde
Go to the documentation of this file.
1// shader.pde
2// samantha jane
3// shader wrapper code to organize uniform creation and updating
4//------------------------------------------------------------------------------
5
6/** interface to store lambda expressions. */
7import java.util.function.Function;
8import java.util.Collections;
9
10/**
11 * shader type enum for Uniforms and Shader identification
12 */
13public static enum shader_type {
14 VERT, /**< vertex shader */
15 FRAG /**< fragment shader */
17
18/**
19 * Shader object organizes a list of uniforms and a compiled PShader.
20 */
21class Shader {
22 PShader program; /**< OpenGL program binding */
23 shader_type type; /**< Shader type (vertex or fragment) */
24 ArrayList<Uniform> uniforms = new ArrayList<Uniform>(); /**< Uniform data */
25 int w /**< width */;
26 int h /**< height */;
27
28 /** Shader Constructor without Uniforms.
29 * loads the shader code and sets default uniforms.
30 *
31 * @param path relative path to shader text file
32 * @param w width of shader
33 * @param h height of shader
34 * @return a Shader object
35 */
36 Shader(String path, int w, int h) {
37 this.w = w;
38 this.h = h;
39 program = loadShader(path);
42 }
43 /** Shader Constructor with Uniform.
44 * loads the shader code and sets default and one user supplied uniforms.
45 *
46 * @param path relative path to shader text file
47 * @param w width of shader
48 * @param h height of shader
49 * @param u Uniform object to add to list
50 * @return a Shader object
51 */
52 Shader(String path, int w, int h, Uniform u) {
53 this.w = w;
54 this.h = h;
55 program = loadShader(path);
57 add_uniform(u);
59 }
60 /** Shader Constructor with Uniforms.
61 * loads the shader code and sets default and additional uniforms.
62 *
63 * @param path relative path to shader text file
64 * @param w width of shader
65 * @param h height of shader
66 * @param u an ArrayList of Uniform objects to add to the shader
67 * @return a Shader object
68 */
69 Shader(String path, int w, int h, ArrayList<Uniform> u) {
70 this.w = w;
71 this.h = h;
72 program = loadShader(path);
74 add_uniforms(u);
76 }
77
78 /**
79 * this creates three common uniforms as defaults.
80 * they are time (updating float), resolution (vec2), and mouse (vec3).
81 */
83 // default time uniform
84 Function<Float, Float> update_time = t -> (float)millis()/1000.0;
85 uniforms.add(new Uniform(
86 "time",
87 update_time.apply(0.0),
89 update_time
90 ));
91 // default resolution uniform
92 ArrayList<Float> dimensions = new ArrayList<Float>();
93 dimensions.add((float)w);
94 dimensions.add((float)h);
95 uniforms.add(new Uniform(
96 "resolution",
97 dimensions,
99 ));
100 // default mouse uniform (2d position + 1d clicking)
101 ArrayList<Float> mouse_pos = new ArrayList<Float>();
102 mouse_pos.add((float)mouseX);
103 mouse_pos.add((float)(-1*mouseY + height));
104 mouse_pos.add(mousePressed ? 1.0 : -1.0);
105 Function<ArrayList<Float>, ArrayList<Float>> update_mouse = t -> {
106 t.set(0, (float)mouseX);
107 t.set(1, (float)(-1*mouseY + height));
108 t.set(2, mousePressed ? 1.0 : -1.0);
109 return t;
110 };
111 mouse_pos = update_mouse.apply(mouse_pos);
112 uniforms.add(new Uniform(
113 "mouse",
114 mouse_pos,
116 update_mouse
117 ));
118 }
119
120 /** add a list of custom defined uniforms. */
121 void add_uniforms(ArrayList<Uniform> custom_uniforms) {
122 uniforms.addAll(custom_uniforms);
123 }
124
125 /** add a single uniform. */
126 void add_uniform(Uniform custom_uniform) {
127 uniforms.add(custom_uniform);
128 }
129
130 /** set uniforms in shader. */
132 for (Uniform u: uniforms) {
133 u.set_uniform(program);
134 }
135 }
136
137 /** update uniforms with an update function. */
139 for (Uniform u: uniforms) {
140 if (u.update_flag) {
141 u.update();
142 u.set_uniform(program);
143 }
144 }
145 }
146
147 /** print function for debugging. */
148 void print() {
149 println("Shader:");
150 println("- program : " + program);
151 println("- type : " + type);
152 println("- uniforms : " + uniforms);
153 println("- dim(w,h) : " + w + ", " + h);
154 println("----------------");
155 }
156}
Shader object organizes a list of uniforms and a compiled PShader.
Definition Shader.pde:21
void print()
print function for debugging.
Definition Shader.pde:148
void update_uniforms()
update uniforms with an update function.
Definition Shader.pde:138
void make_default_uniforms()
this creates three common uniforms as defaults.
Definition Shader.pde:82
Shader(String path, int w, int h)
Shader Constructor without Uniforms.
Definition Shader.pde:36
void set_uniforms()
set uniforms in shader.
Definition Shader.pde:131
Shader(String path, int w, int h, Uniform u)
Shader Constructor with Uniform.
Definition Shader.pde:52
PShader program
OpenGL program binding.
Definition Shader.pde:22
void add_uniforms(ArrayList< Uniform > custom_uniforms)
add a list of custom defined uniforms.
Definition Shader.pde:121
int h
< height
Definition Shader.pde:26
ArrayList< Uniform > uniforms
Uniform data.
Definition Shader.pde:24
shader_type type
Shader type (vertex or fragment)
Definition Shader.pde:23
void add_uniform(Uniform custom_uniform)
add a single uniform.
Definition Shader.pde:126
Shader(String path, int w, int h, ArrayList< Uniform > u)
Shader Constructor with Uniforms.
Definition Shader.pde:69
int w
< width
Definition Shader.pde:25
organizes information for a single Uniform, including its underlying data type and update behavior.
Definition Uniform.pde:27
interface to store lambda expressions.
Definition Shader.pde:13
VERT
vertex shader
Definition Shader.pde:14
interface to store lambda expression.
Definition Uniform.pde:10
VEC2
2D vector
Definition Uniform.pde:14
VEC3
3D vector
Definition Uniform.pde:15