- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderNode.java
More file actions
Latest commit
203 lines (164 loc) · 5.04 KB
/
Copy pathShaderNode.java
File metadata and controls
203 lines (164 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.media.opengl.GL;
/**
* A Shader Node
* @author Andrew
*/
publicclassShaderNodeextendsNode
{
/** Default value for null shaders */
privatestaticfinalintNO_SHADER_ID = -1;
/** Compiled vertex shader id */
privateintvertexShader = NO_SHADER_ID;
/** Compiled frag shader id */
privateintfragmentShader = NO_SHADER_ID;
/** The complete shader program */
privateintshaderProgram = NO_SHADER_ID;
/** The source code for the vertex shader */
privateStringvertexSource;
/** The source code for the fragment shader */
privateStringfragmentSource;
/** True if shader is ready to use */
privatebooleanshaderReady;
/**
* Creates a new ShaderNode
* @param name the name of this node
*/
publicShaderNode(Stringname)
{
super(name);
shaderReady = false;
}
/**
* Loads the shaders for this node
* @param shaderType either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
* @param shaderFile the filename to get the source from
* @return true if shader was successfully loaded
*/
publicbooleanloadShaderSource(intshaderType, StringshaderFile)
{
booleansuccess = false;
try
{
BufferedReaderbr = newBufferedReader(newFileReader(shaderFile));
Stringdata = "";
Stringline = null;
while ((line = br.readLine()) != null)
{
data += line + "\n";
}
if(shaderType == GL.GL_VERTEX_SHADER)
{
vertexSource = data;
}
elseif(shaderType == GL.GL_FRAGMENT_SHADER)
{
fragmentSource = data;
}
br.close();
success = true;
}
catch (FileNotFoundExceptionex)
{
Logger.getLogger(ShaderNode.class.getName()).log(Level.SEVERE, null, ex);
success = false;
}
catch (IOExceptionex)
{
Logger.getLogger(ShaderNode.class.getName()).log(Level.SEVERE, null, ex);
}
returnsuccess;
}
/**
* Inits this shader node
* @param gl
*/
@Override
publicvoidinit(GLgl)
{
//create the shaders on the GPU
vertexShader = gl.glCreateShader(GL.GL_VERTEX_SHADER);
fragmentShader = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
//send source of vertex and compile
gl.glShaderSource(vertexShader, 1, newString[]{vertexSource}, null);
gl.glCompileShader(vertexShader);
//ensure vertex was compiled
int[] status = newint[1];
gl.glGetShaderiv(vertexShader, GL.GL_COMPILE_STATUS, status, 0);
if(status[0] != GL.GL_TRUE) //then something went wrong
{
Logger.getLogger(ShaderNode.class.getName()).log(Level.SEVERE, "Error compiling VERTEX shader");
return;
}
//send source of frag and compile
gl.glShaderSource(fragmentShader, 1, newString[]{fragmentSource}, null);
gl.glCompileShader(fragmentShader);
//ensure frag was compiled
gl.glGetShaderiv(fragmentShader, GL.GL_COMPILE_STATUS, status, 0);
if(status[0] != GL.GL_TRUE) //then something went wrong
{
Logger.getLogger(ShaderNode.class.getName()).log(Level.SEVERE, "Error compiling FRAGMENT shader");
return;
}
//create the program
shaderProgram = gl.glCreateProgram();
//link shaders
gl.glAttachShader(shaderProgram, vertexShader);
gl.glAttachShader(shaderProgram, fragmentShader);
gl.glLinkProgram(shaderProgram);
//ensure shader was linked
gl.glGetProgramiv(shaderProgram, GL.GL_LINK_STATUS, status, 0);
if(status[0] != GL.GL_TRUE) //then something went wrong
{
Logger.getLogger(ShaderNode.class.getName()).log(Level.SEVERE, "Error linking shader program");
return;
}
shaderReady = true;
super.init(gl);
}
/**
* Gets the shader program of this node.
* @return The shader program ID
*/
@Override
publicintgetShaderProgram()
{
returnshaderProgram;
}
/**
* Turns on this shader
* @param gl
*/
publicvoidturnOnShader(GLgl)
{
gl.glUseProgram(shaderProgram);
}
/**
* Turns off this shader
* @param gl
*/
publicvoidturnOffShader(GLgl)
{
if(parentNode != null)
gl.glUseProgram(parentNode.getShaderProgram());
else
gl.glUseProgram(0);
}
/**
* Turns the shader on and draws this (and children) nodes
* @param gl
*/
@Override
publicvoiddraw(GLgl)
{
turnOnShader(gl);
super.draw(gl);
turnOffShader(gl);
}
}