Skip to content

Commit

Permalink
Add colors for lines, text, keys, minor keys and flashes.
Browse files Browse the repository at this point in the history
As suggested in #12.
  • Loading branch information
kosua20 committed Apr 14, 2019
1 parent b89ab1b commit 4f6234d
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 48 deletions.
24 changes: 12 additions & 12 deletions ressources/shaders/background.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ uniform bool useVLines = true;
uniform bool useKeys = true;
uniform float minorsWidth = 1.0;
uniform sampler2D screenTexture;
uniform vec3 textColor = vec3(1.0);
uniform vec3 linesColor = vec3(1.0);
uniform vec3 keysColor = vec3(0.0);

const bool isMinor[88] = bool[](true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, true, true, false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
const float octaveLinesPositions[8] = float[](2.0/52.0, 9.0/52.0, 16.0/52.0, 23.0/52.0, 30.0/52.0, 37.0/52.0, 44.0/52.0, 51.0/52.0);
Expand Down Expand Up @@ -78,14 +81,13 @@ float printNumber(float num, vec2 position, vec2 uv, vec2 scale){

void main(){

float intensity = 0.0;

// Keys
if(useKeys && gl_FragCoord.y < bottomLimit/inverseScreenSize.y){


// White keys, and separators.
intensity = int(abs(fract(In.uv.x*52.0)) >= 2.0 * 52.0 * inverseScreenSize.x);
float intensity = int(abs(fract(In.uv.x*52.0)) >= 2.0 * 52.0 * inverseScreenSize.x);

// Upper keyboard.
if(gl_FragCoord.y > 0.10/inverseScreenSize.y){
Expand All @@ -98,18 +100,16 @@ void main(){
intensity = step(marginSize, abs(fract(In.uv.x*52.0+0.5)*2.0-1.0));
}
}
fragColor = vec3(intensity);
fragColor = mix(keysColor, vec3(1.0), intensity);
// Done.
return;



}

vec3 bgColor = vec3(0.0);
// Octaves lines.
for(int i = 0; i < 8; i++){
float lineIntensity = useVLines ? (0.7 * step(abs(In.uv.x - octaveLinesPositions[i]),inverseScreenSize.x)) : 0.0;
intensity = max(intensity, lineIntensity);
bgColor = mix(bgColor, linesColor, lineIntensity);
}

vec2 scale = 1.5*vec2(64.0,50.0*inverseScreenSize.x/inverseScreenSize.y);
Expand All @@ -123,18 +123,18 @@ void main(){
// Compute position of the measure currentMesure+i.
vec2 position = vec2(0.005,bottomLimit + (secondsPerMeasure*(currentMesure+i) - time)*mainSpeed*0.5);

// Compute intensity for the number display, and for the horizontal line.
// Compute color for the number display, and for the horizontal line.
float numberIntensity = useDigits ? printNumber(currentMesure + i,position, In.uv, scale) : 0.0;
bgColor = mix(bgColor, textColor, numberIntensity);
float lineIntensity = useHLines ? (0.25*(step(abs(In.uv.y - position.y - 0.5 / scale.y), inverseScreenSize.y))) : 0.0;

intensity = max(intensity, max(numberIntensity, lineIntensity));
bgColor = mix(bgColor, linesColor, lineIntensity);
}

if(intensity == 0.0){
if(all(equal(bgColor, vec3(0.0)))){
// Transparent background.
discard;
}

fragColor = vec3(intensity);
fragColor = bgColor;

}
3 changes: 2 additions & 1 deletion ressources/shaders/flashes.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ layout(location = 1) in int isOn;

uniform float time;
uniform vec2 inverseScreenSize;
uniform float userScale = 1.0;

#define notesCount 52.0

Expand All @@ -21,7 +22,7 @@ out INTERFACE {
void main(){

// Scale quad, keep the square ratio.
vec2 scaledPosition = v * 2.0 * scale /notesCount * vec2(1.0, inverseScreenSize.y/inverseScreenSize.x);
vec2 scaledPosition = v * 2.0 * scale * userScale/notesCount * vec2(1.0, inverseScreenSize.y/inverseScreenSize.x);
// Shift based on note/flash id.
vec2 globalShift = vec2(-1.0 + (shifts[gl_InstanceID] * 2.0 + 1.0) / notesCount,-0.5);

Expand Down
3 changes: 2 additions & 1 deletion ressources/shaders/notes.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in INTERFACE {
} In;

uniform vec3 baseColor;
uniform vec3 minorColor;
uniform vec2 inverseScreenSize;

#define cornerRadius 0.01
Expand All @@ -30,7 +31,7 @@ void main(){
}

// Fragment color.
fragColor = (0.8+0.2*(1.0-In.isMinor))*baseColor;
fragColor = mix(baseColor, minorColor, In.isMinor);

if( radiusPosition > 0.8){
fragColor *= 1.05;
Expand Down
8 changes: 8 additions & 0 deletions src/rendering/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ void Background::setDisplay(const bool digits, const bool horiz, const bool vert
glUseProgram(0);
}

void Background::setColors(const glm::vec3 & linesColor, const glm::vec3 & textColor, const glm::vec3 & keysColor){
glUseProgram(_programId);
GLuint id1 = glGetUniformLocation(_programId, "linesColor"); glUniform3fv(id1, 1, &linesColor[0]);
GLuint id2 = glGetUniformLocation(_programId, "textColor"); glUniform3fv(id2, 1, &textColor[0]);
GLuint id3 = glGetUniformLocation(_programId, "keysColor"); glUniform3fv(id3, 1, &keysColor[0]);
glUseProgram(0);
}

2 changes: 2 additions & 0 deletions src/rendering/Background.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Background : public ScreenQuad {
void setScaleAndMinorWidth(const float scale, const float width);

void setDisplay(const bool digits, const bool horiz, const bool vert, const bool keys);

void setColors(const glm::vec3 & linesColor, const glm::vec3 & textColor, const glm::vec3 & keysColor);
};

#endif
10 changes: 7 additions & 3 deletions src/rendering/MIDIScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,23 @@ void MIDIScene::drawParticles(const float time, const glm::vec2 & invScreenSize,
}


void MIDIScene::draw(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, bool prepass){
void MIDIScene::draw(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, const glm::vec3 & minorColor, bool prepass){

glUseProgram(_programId);

// Uniforms setup.
GLuint screenId = glGetUniformLocation(_programId, "inverseScreenSize");
GLuint timeId = glGetUniformLocation(_programId, "time");
GLuint colorId = glGetUniformLocation(_programId, "baseColor");
GLuint colorMinId = glGetUniformLocation(_programId, "minorColor");
glUniform2fv(screenId,1, &(invScreenSize[0]));
glUniform1f(timeId,time);
if(prepass){
glUniform3f(colorId, 0.6f*baseColor[0], 0.6f*baseColor[1], 0.6f*baseColor[2]);
glUniform3f(colorMinId, 0.6f*minorColor[0], 0.6f*minorColor[1], 0.6f*minorColor[2]);
} else {
glUniform3fv(colorId, 1, &(baseColor[0]));
glUniform3fv(colorMinId, 1, &(minorColor[0]));
}


Expand All @@ -305,7 +308,7 @@ void MIDIScene::draw(float time, glm::vec2 invScreenSize, const glm::vec3 & base

}

void MIDIScene::drawFlashes(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor){
void MIDIScene::drawFlashes(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, float userScale){

// Need alpha blending.
glEnable(GL_BLEND);
Expand All @@ -323,10 +326,11 @@ void MIDIScene::drawFlashes(float time, glm::vec2 invScreenSize, const glm::vec3
GLuint screenId1 = glGetUniformLocation(_programFlashesId, "inverseScreenSize");
GLuint timeId1 = glGetUniformLocation(_programFlashesId, "time");
GLuint colorId = glGetUniformLocation(_programFlashesId, "baseColor");
GLuint scaleId = glGetUniformLocation(_programFlashesId, "userScale");
glUniform2fv(screenId1,1, &(invScreenSize[0]));
glUniform1f(timeId1,time);
glUniform3fv(colorId, 1, &(baseColor[0]));

glUniform1f(scaleId,userScale);
// Flash texture.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _texFlash);
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/MIDIScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class MIDIScene {
void updatesActiveNotes(double time);

/// Draw function
void draw(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, bool prepass);
void draw(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, const glm::vec3 & minorColor, bool prepass);

void drawFlashes(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor);
void drawFlashes(float time, glm::vec2 invScreenSize, const glm::vec3 & baseColor, float userScale);

void drawParticles(const float time, const glm::vec2 & invScreenSize, const glm::vec3 & particlesColor, const float particlesScale, const std::vector<GLuint> &lookTextures, const int particlesCount, bool prepass);

Expand Down
68 changes: 44 additions & 24 deletions src/rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void Renderer::init(int width, int height){
void Renderer::setColorAndScale(const glm::vec3 & baseColor, const float scale){
_state.scale = scale;
_state.baseColor = baseColor;
_state.minorColor = baseColor;
_state.flashColor = baseColor;
_state.particles.color = _state.baseColor;
}

Expand All @@ -71,11 +73,6 @@ void Renderer::loadFile(const std::string & midiFilePath){
// Init objects.
_scene = std::make_shared<MIDIScene>(midiFilePath);
_background = std::make_shared<Background>(_scene->midiFile().tracks[0].secondsPerMeasure);
// _scene->setScaleAndMinorWidth(_state.scale, _state.background.minorsWidth);
// _scene->setParticlesParameters(_state.particles.speed, _state.particles.expansion);
//
// _background->setScaleAndMinorWidth(_state.scale, _state.background.minorsWidth);
// _background->setDisplay(_state.background.digits, _state.background.hLines, _state.background.vLines, _state.background.keys);

applyAllSettings();
}
Expand Down Expand Up @@ -138,7 +135,7 @@ void Renderer::draw(const float currentTime){
}
if(_state.showBlurNotes){
// Draw the notes.
_scene->draw(_timer, invSizeB, _state.baseColor, true);
_scene->draw(_timer, invSizeB, _state.baseColor, _state.minorColor, true);
}

_particlesFramebuffer->unbind();
Expand Down Expand Up @@ -172,12 +169,12 @@ void Renderer::draw(const float currentTime){
_background->draw(_timer, invSizeFb);
// Draw the notes.
if(_state.showNotes){
_scene->draw(_timer, invSizeFb, _state.baseColor, false);
_scene->draw(_timer, invSizeFb, _state.baseColor, _state.minorColor, false);
}

if(_state.showFlashes){
// Draw the flashes.
_scene->drawFlashes(_timer, invSize, _state.baseColor);
_scene->drawFlashes(_timer, invSize, _state.flashColor, _state.flashSize);
}
_finalFramebuffer->unbind();

Expand Down Expand Up @@ -248,16 +245,15 @@ void Renderer::drawGUI(const float currentTime){


bool colNotesEdit = ImGui::ColorEdit3("Notes", &_state.baseColor[0], ImGuiColorEditFlags_NoInputs); ImGui::SameLine(80);
bool colPartsEdit = ImGui::ColorEdit3("Effects", &_state.particles.color[0], ImGuiColorEditFlags_NoInputs);
ImGui::SameLine(160);
bool colMinorsEdit = ImGui::ColorEdit3("Minors", &_state.minorColor[0], ImGuiColorEditFlags_NoInputs); ImGui::SameLine(160);
bool colPartsEdit = ImGui::ColorEdit3("Effects", &_state.particles.color[0], ImGuiColorEditFlags_NoInputs); ImGui::SameLine(240);
bool colFlashesEdit = ImGui::ColorEdit3("Flashes", &_state.flashColor[0], ImGuiColorEditFlags_NoInputs);

if(ImGui::Checkbox("Lock colors", &_state.lockParticleColor)){
// If we enable the lock, make sure the colors are synched.
colNotesEdit = true;
}

ImGui::Checkbox("Particles", &_state.showParticles); ImGui::SameLine(160);
ImGui::Checkbox("Flashes", &_state.showFlashes);
ImGui::Checkbox("Notes", &_state.showNotes); ImGui::SameLine(160);
ImGui::SameLine(160);
ImGui::Checkbox("Blur", &_state.showBlur);
if(_state.showBlur) {
ImGui::Checkbox("Blur notes", &_state.showBlurNotes);
Expand All @@ -273,6 +269,15 @@ void Renderer::drawGUI(const float currentTime){
ImGui::PopItemWidth();
}

ImGui::Checkbox("Notes", &_state.showNotes); ImGui::SameLine(160);
ImGui::Checkbox("Particles", &_state.showParticles);
ImGui::Checkbox("Flashes", &_state.showFlashes); ImGui::SameLine(160);
ImGui::PushItemWidth(86);
ImGui::SliderFloat("Flash size", &_state.flashSize, 0.1f, 3.0f);
ImGui::PopItemWidth();



bool smw1 = false;

if (ImGui::CollapsingHeader("Background##HEADER", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand All @@ -289,19 +294,29 @@ void Renderer::drawGUI(const float currentTime){
glUniform3fv(id1, 1, &_state.background.color[0]);
glUseProgram(0);
}
ImGui::SameLine(120);
ImGui::PushItemWidth(80);
smw1 = ImGui::InputFloat("Minor keys size", &_state.background.minorsWidth, 0.1f, 1.0f, "%.2f");
ImGui::PopItemWidth();
ImGui::SameLine(80);
bool cbg0 = ImGui::ColorEdit3("Lines##Background", &_state.background.linesColor[0], ImGuiColorEditFlags_NoInputs);
ImGui::SameLine(160);
bool cbg1 = ImGui::ColorEdit3("Text##Background", &_state.background.textColor[0], ImGuiColorEditFlags_NoInputs);
ImGui::SameLine(240);
bool cbg2 = ImGui::ColorEdit3("Keys##Background", &_state.background.keysColor[0], ImGuiColorEditFlags_NoInputs);


bool m2 = ImGui::Checkbox("Horizontal lines", &_state.background.hLines); ImGui::SameLine(160);
bool m3 = ImGui::Checkbox("Vertical lines", &_state.background.vLines);
bool m4 = ImGui::Checkbox("Keyboard", &_state.background.keys); ImGui::SameLine(160);
bool m1 = ImGui::Checkbox("Digits", &_state.background.digits);

ImGui::PushItemWidth(145);
smw1 = ImGui::InputFloat("Minor keys size", &_state.background.minorsWidth, 0.1f, 1.0f, "%.2f");
ImGui::PopItemWidth();

if(m1 || m2 || m3 || m4){
_background->setDisplay(_state.background.digits, _state.background.hLines, _state.background.vLines, _state.background.keys);
}

if(cbg0 || cbg1 || cbg2){
_background->setColors(_state.background.linesColor, _state.background.textColor, _state.background.keysColor);
}
}

if(_state.showParticles){
Expand Down Expand Up @@ -413,12 +428,16 @@ void Renderer::drawGUI(const float currentTime){
}

// Keep the colors in sync if needed.
if(_state.lockParticleColor){
if(colNotesEdit){
_state.particles.color = _state.baseColor;
} else if(colPartsEdit){
_state.baseColor = _state.particles.color;
if(_state.lockParticleColor && (colNotesEdit || colPartsEdit || colMinorsEdit || colFlashesEdit)){
glm::vec3 refColor = _state.baseColor;
if(colPartsEdit){
refColor = _state.particles.color;
} else if(colMinorsEdit){
refColor = _state.minorColor;
} else if(colFlashesEdit){
refColor = _state.flashColor;
}
_state.baseColor = _state.particles.color = _state.minorColor = _state.flashColor = refColor;
}


Expand Down Expand Up @@ -507,6 +526,7 @@ void Renderer::applyAllSettings(){
_background->setScaleAndMinorWidth(_state.scale, _state.background.minorsWidth);
_scene->setParticlesParameters(_state.particles.speed, _state.particles.expansion);
_background->setDisplay(_state.background.digits, _state.background.hLines, _state.background.vLines, _state.background.keys);
_background->setColors(_state.background.linesColor, _state.background.textColor, _state.background.keysColor);

// Background color.
glClearColor(_state.background.color[0],_state.background.color[1],_state.background.color[2], 1.0f);
Expand Down
21 changes: 20 additions & 1 deletion src/rendering/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ void State::save(const std::string & path){
configFile << attenuation << std::endl;
configFile << showNotes << std::endl;

configFile << background.linesColor[0] << " " << background.linesColor[1] << " " << background.linesColor[2] << std::endl;
configFile << background.textColor[0] << " " << background.textColor[1] << " " << background.textColor[2] << std::endl;
configFile << background.keysColor[0] << " " << background.keysColor[1] << " " << background.keysColor[2] << std::endl;
configFile << minorColor[0] << " " << minorColor[1] << " " << minorColor[2] << std::endl;
configFile << flashColor[0] << " " << flashColor[1] << " " << flashColor[2] << std::endl;
configFile << flashSize << std::endl;
configFile.close();
}

Expand Down Expand Up @@ -129,17 +135,29 @@ void State::load(const std::string & path){
}

// MIDIVIZ_VERSION_MAJOR == 3, MIDIVIZ_VERSION_MINOR == 3
// Added attenuation factor.
minorColor = 0.8f*baseColor;
flashColor = baseColor;
if(majVersion >= 3 && minVersion >= 3){
configFile >> showNotes ;
configFile >> background.linesColor[0] >> background.linesColor[1] >> background.linesColor[2] ;
configFile >> background.textColor[0] >> background.textColor[1] >> background.textColor[2] ;
configFile >> background.keysColor[0] >> background.keysColor[1] >> background.keysColor[2] ;
configFile >> minorColor[0] >> minorColor[1] >> minorColor[2] ;
configFile >> flashColor[0] >> flashColor[1] >> flashColor[2] ;
configFile >> flashSize;
}

configFile.close();
}

void State::reset(){
baseColor = 1.35f*glm::vec3(0.57f,0.19f,0.98f);
minorColor = 0.8f*baseColor;
flashColor = baseColor;
background.color = glm::vec3(0.0f, 0.0f, 0.0f) ;
background.linesColor = glm::vec3(1.0f, 1.0f, 1.0f);
background.textColor = glm::vec3(1.0f, 1.0f, 1.0f);
background.keysColor = glm::vec3(0.0f, 0.0f, 0.0f);
particles.color = baseColor;

scale = 0.5f ;
Expand All @@ -150,6 +168,7 @@ void State::reset(){
showBlurNotes = false ;
lockParticleColor = true ;
showNotes = true;
flashSize = 1.0f;

background.minorsWidth = 0.8f;
background.hLines = true;
Expand Down
Loading

0 comments on commit 4f6234d

Please sign in to comment.