-
Notifications
You must be signed in to change notification settings - Fork 23
/
ofxAutoReloadedShader.cpp
executable file
·213 lines (172 loc) · 6.21 KB
/
ofxAutoReloadedShader.cpp
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
204
205
206
207
208
209
210
211
212
213
#include "ofxAutoReloadedShader.h"
ofxAutoReloadedShader::ofxAutoReloadedShader(){
bWatchingFiles = false;
}
ofxAutoReloadedShader::~ofxAutoReloadedShader(){
disableWatchFiles();
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxAutoReloadedShader::load(string shaderName )
{
return load( shaderName + ".vert", shaderName + ".frag", shaderName + ".geom" );
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxAutoReloadedShader::load(string vertName, string fragName, string geomName)
{
unload();
ofShader::setGeometryOutputCount(geometryOutputCount);
ofShader::setGeometryInputType(geometryInputType);
ofShader::setGeometryOutputType(geometryOutputType);
// hackety hack, clear errors or shader will fail to compile
GLuint err = glGetError();
lastTimeCheckMillis = ofGetElapsedTimeMillis();
setMillisBetweenFileCheck( 2 * 1000 );
enableWatchFiles();
loadShaderNextFrame = false;
vertexShaderFilename = vertName;
fragmentShaderFilename = fragName;
geometryShaderFilename = geomName;
vertexShaderFile.clear();
fragmentShaderFile.clear();
geometryShaderFile.clear();
vertexShaderFile = ofFile( ofToDataPath( vertexShaderFilename ) );
fragmentShaderFile = ofFile( ofToDataPath( fragmentShaderFilename ) );
geometryShaderFile = ofFile( ofToDataPath( geometryShaderFilename ) );
ofBuffer vertexShaderBuffer = ofBufferFromFile( ofToDataPath( vertexShaderFilename ) );
ofBuffer fragmentShaderBuffer = ofBufferFromFile( ofToDataPath( fragmentShaderFilename ) );
ofBuffer geometryShaderBuffer = ofBufferFromFile( ofToDataPath( geometryShaderFilename ) );
fileChangedTimes.clear();
fileChangedTimes.push_back( getLastModified( vertexShaderFile ) );
fileChangedTimes.push_back( getLastModified( fragmentShaderFile ) );
fileChangedTimes.push_back( getLastModified( geometryShaderFile ) );
if( vertexShaderBuffer.size() > 0 )
{
setupShaderFromSource(GL_VERTEX_SHADER, vertexShaderBuffer.getText() );
}
if( fragmentShaderBuffer.size() > 0 )
{
setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShaderBuffer.getText());
}
#ifndef TARGET_OPENGLES
if( geometryShaderBuffer.size() > 0 )
{
setupShaderFromSource(GL_GEOMETRY_SHADER_EXT, geometryShaderBuffer.getText());
}
#endif
bindDefaults();
return linkProgram();
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxAutoReloadedShader::_update(ofEventArgs &e)
{
if( loadShaderNextFrame )
{
reloadShaders();
loadShaderNextFrame = false;
}
int currTime = ofGetElapsedTimeMillis();
if( ((currTime - lastTimeCheckMillis) > millisBetweenFileCheck) &&
!loadShaderNextFrame )
{
if( filesChanged() )
{
loadShaderNextFrame = true;
}
lastTimeCheckMillis = currTime;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxAutoReloadedShader::reloadShaders()
{
return load( vertexShaderFilename, fragmentShaderFilename, geometryShaderFilename );
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxAutoReloadedShader::enableWatchFiles()
{
if(!bWatchingFiles){
ofAddListener(ofEvents().update, this, &ofxAutoReloadedShader::_update );
bWatchingFiles = true;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxAutoReloadedShader::disableWatchFiles()
{
if(bWatchingFiles){
ofRemoveListener(ofEvents().update, this, &ofxAutoReloadedShader::_update );
bWatchingFiles = false;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxAutoReloadedShader::filesChanged()
{
bool fileChanged = false;
if( vertexShaderFile.exists() )
{
std::time_t vertexShaderFileLastChangeTime = getLastModified( vertexShaderFile );
if( vertexShaderFileLastChangeTime != fileChangedTimes.at(0) )
{
fileChangedTimes.at(0) = vertexShaderFileLastChangeTime;
fileChanged = true;
}
}
if( fragmentShaderFile.exists() )
{
std::time_t fragmentShaderFileLastChangeTime = getLastModified( fragmentShaderFile );
if( fragmentShaderFileLastChangeTime != fileChangedTimes.at(1) )
{
fileChangedTimes.at(1) = fragmentShaderFileLastChangeTime;
fileChanged = true;
}
}
if( geometryShaderFile.exists() )
{
std::time_t geometryShaderFileLastChangeTime = getLastModified( geometryShaderFile );
if( geometryShaderFileLastChangeTime != fileChangedTimes.at(2) )
{
fileChangedTimes.at(2) = geometryShaderFileLastChangeTime;
fileChanged = true;
}
}
return fileChanged;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
std::time_t ofxAutoReloadedShader::getLastModified( ofFile& _file )
{
if( _file.exists() )
{
return std::filesystem::last_write_time(_file.path());
}
else
{
return 0;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxAutoReloadedShader::setMillisBetweenFileCheck( int _millis )
{
millisBetweenFileCheck = _millis;
}
//--------------------------------------------------------------
void ofxAutoReloadedShader::setGeometryInputType(GLenum type) {
ofShader::setGeometryInputType(type);
geometryInputType = type;
}
//--------------------------------------------------------------
void ofxAutoReloadedShader::setGeometryOutputType(GLenum type) {
ofShader::setGeometryOutputType(type);
geometryOutputType = type;
}
//--------------------------------------------------------------
void ofxAutoReloadedShader::setGeometryOutputCount(int count) {
ofShader::setGeometryOutputCount(count);
geometryOutputCount = count;
}