Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciogonzalezvivo committed May 10, 2018
0 parents commit e4eb0e1
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 0 deletions.
Empty file added .gitignore
Empty file.
Empty file added README.md
Empty file.
9 changes: 9 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The code in this repository is available under the [MIT License](https://secure.wikimedia.org/wikipedia/en/wiki/Mit_license).

Copyright (c) Feb 24th 2018 Patricio Gonzalez Vivo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
188 changes: 188 additions & 0 deletions src/ofxSmartShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@

#include "ofxSmartShader.h"

ofxSmartShader::ofxSmartShader() {
bWatchingFiles = false;
}

ofxSmartShader::~ofxSmartShader() {
disableWatchFiles();
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxSmartShader::load(string _shaderName ) {
return load( _shaderName + ".vert", _shaderName + ".frag", _shaderName + ".geom" );
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxSmartShader::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 ofxSmartShader::_update(ofEventArgs &e) {
if ( loadShaderNextFrame ) {
reloadShaders();
loadShaderNextFrame = false;
}

int currTime = ofGetElapsedTimeMillis();

if (((currTime - lastTimeCheckMillis) > millisBetweenFileCheck) &&
!loadShaderNextFrame ) {
if ( filesChanged() ) {
loadShaderNextFrame = true;
}

lastTimeCheckMillis = currTime;
}
}


// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxSmartShader::reloadShaders() {
return load( vertexShaderFilename, fragmentShaderFilename, geometryShaderFilename );
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxSmartShader::enableWatchFiles() {
if (!bWatchingFiles) {
ofAddListener(ofEvents().update, this, &ofxSmartShader::_update );
bWatchingFiles = true;
}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxSmartShader::disableWatchFiles() {
if (bWatchingFiles) {
ofRemoveListener(ofEvents().update, this, &ofxSmartShader::_update );
bWatchingFiles = false;
}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
bool ofxSmartShader::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 ofxSmartShader::getLastModified( ofFile& _file ) {
if ( _file.exists() ) {
return std::filesystem::last_write_time(_file.path());
}
else {
return 0;
}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void ofxSmartShader::setMillisBetweenFileCheck( int _millis ) {
millisBetweenFileCheck = _millis;
}

//--------------------------------------------------------------
void ofxSmartShader::setGeometryInputType( GLenum _type ) {
ofShader::setGeometryInputType(_type);
geometryInputType = _type;
}

//--------------------------------------------------------------
void ofxSmartShader::setGeometryOutputType( GLenum _type ) {
ofShader::setGeometryOutputType(_type);
geometryOutputType = _type;
}

//--------------------------------------------------------------
void ofxSmartShader::setGeometryOutputCount( int _count ) {
ofShader::setGeometryOutputCount(_count);
geometryOutputCount = _count;
}
53 changes: 53 additions & 0 deletions src/ofxSmartShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "ofMain.h"

class ofxSmartShader : public ofShader {
public:
ofxSmartShader();
~ofxSmartShader();

// override the initialisation functions
bool load(string shaderName );
bool load(string vertName, string fragName, string geomName);

bool reloadShaders();

void enableWatchFiles();
void disableWatchFiles();

void setMillisBetweenFileCheck( int _millis );

void _update(ofEventArgs &e);

void setGeometryInputType(GLenum type);
void setGeometryOutputType(GLenum type);
void setGeometryOutputCount(int count);

private:

bool bWatchingFiles;
bool filesChanged();

bool loadShaderNextFrame;

std::time_t getLastModified( ofFile& _file );

int lastTimeCheckMillis;
int millisBetweenFileCheck;

string vertexShaderFilename;
string fragmentShaderFilename;
string geometryShaderFilename;

ofFile vertexShaderFile;
ofFile fragmentShaderFile;
ofFile geometryShaderFile;

vector< std::time_t > fileChangedTimes;

GLenum geometryInputType, geometryOutputType;
int geometryOutputCount;
};


0 comments on commit e4eb0e1

Please sign in to comment.