Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #22 from OliverUv/improve-shaderload
Browse files Browse the repository at this point in the history
Improve shader code loading
  • Loading branch information
patriciogonzalezvivo committed Apr 7, 2014
2 parents c557131 + ff54fc4 commit d1fddb0
Show file tree
Hide file tree
Showing 19 changed files with 787 additions and 154 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -14,9 +14,9 @@ The structure it´s easy.
- ```int internalFormat```: if it use GL_RGB, GL_RGBA, GL_RGB16f, GL_RGBA16f, GL_RGB32f, GL_RGBA32f, etc...
- ```string fragShader```: it´s the code of the shader. Note that some changes have to be made in order to fill everything on a string

2. ```allocate(width,height,GL_RGBA)```: This usually it´s no need to bee re-define. It´s basically allocate the FBO´s and loads the shader by using injectShader();
2. ```allocate(width,height,GL_RGBA)```: This usually it´s no need to be re-define. It´s basically allocate the FBO´s and loads the shader by using injectShader();

3. ```setCode(string fragContent)```: here is where the shaders are loaded. See the example bellow.
3. ```setCode(string fragContent)```: here is where the shaders are loaded. See the example below.

4. ```begin(int texN)``` and ```end(int texN)```: remember nTextures variable? you can passthrough information to it by using this end() and begin() and giving the argument the number of texture you want to open. This texture can be access from the shader by the ```uniform sample2DRect tex0``` or ```tex1``` or ```tex2``` and so on.

Expand Down Expand Up @@ -57,6 +57,12 @@ fxObject.setCode("#version 120\n\
}");
```

Also look at the functions load, loadVersioned, and the different shader strings
to see how you can specify your shader code. You can also see how this is used
to easily load OpenGL2 and OpenGL3 shaders correctly if you look inside the
test-shader-load example. Note that if you don't specify any Vertex Shader for
OpenGL3, a simple pass-through shader will be added for you. :)

On update:

```c++
Expand Down Expand Up @@ -198,5 +204,8 @@ On this addon you will find examples of the classes I just describe. Some of the

* conway: life game made by [Kalwalt](http://www.kalwaltart.it/)

## Tests

* test-buffer-copying is a regression test that shows three simple buffers printed side by side. They should all look the same. If they don't look the same, we are somehow changing textures as we are copying them.

* test-shader-load is a test to ensure that loading shaders from file and from string works properly. Should be tested both with and without ```#define USE_PROGRAMMABLE_RENDERER``` set in main.cpp.
119 changes: 50 additions & 69 deletions src/filters/ofxBloom.h
Expand Up @@ -19,78 +19,59 @@ class ofxBloom : public ofxFXObject {
passes = 1;
internalFormat = GL_RGBA;

if (ofIsGLProgrammableRenderer()) { // OpenGL 3.0
string vertexShader = "#version 150\n";
vertexShader += STRINGIFY(
uniform mat4 modelViewProjectionMatrix;
uniform mat4 textureMatrix;
gl3FragmentShader = "#version 150\n";
gl3FragmentShader += STRINGIFY(
uniform sampler2DRect tex0;
in vec2 texCoordVarying;
out vec4 outputColor;

in vec4 position;
in vec2 texcoord;

out vec2 texCoordVarying;
void main(){
vec4 sum = vec4(0);
vec2 st = gl_FragCoord.st;
int j;
int i;
for(i=-4; i<4; i++){
for (j=-3; j<3; j++){
sum += texture(tex0, st + vec2(j, i)*0.004) * 0.25;
}
}

if (texture(tex0, st).r < 0.3){
outputColor = sum * sum * 0.012 + texture(tex0, st);
} else {
if (texture(tex0, st).r < 0.5){
outputColor = sum * sum * 0.009 + texture(tex0, st);
} else {
outputColor = sum * sum * 0.0075 + texture(tex0, st);
}
}
outputColor.a = texture(tex0, st).a;
});

gl2FragmentShader = ""; // For some reason "#version 120\n" makes this break.
gl2FragmentShader += STRINGIFY(
uniform sampler2DRect tex0;
void main(){
texCoordVarying = texcoord;
gl_Position = modelViewProjectionMatrix * position;
vec4 sum = vec4(0);
vec2 st = gl_TexCoord[0].st;
int j;
int i;
for(i=-4; i<4; i++){
for (j=-3; j<3; j++){
sum += texture2DRect(tex0, st + vec2(j, i)*0.004) * 0.25;
}
}

if (texture2DRect(tex0, st).r < 0.3){
gl_FragColor = sum * sum * 0.012 + texture2DRect(tex0, st);
} else {
if (texture2DRect(tex0, st).r < 0.5){
gl_FragColor = sum * sum * 0.009 + texture2DRect(tex0, st);
} else {
gl_FragColor = sum * sum * 0.0075 + texture2DRect(tex0, st);
}
}
gl_FragColor.a = texture2DRect(tex0, st).a;
});

shader.setupShaderFromSource(GL_VERTEX_SHADER, vertexShader);

fragmentShader = "#version 150\n";
fragmentShader += STRINGIFY(uniform sampler2DRect tex0;
in vec2 texCoordVarying;
out vec4 outputColor;

void main(){
vec4 sum = vec4(0);
vec2 st = gl_FragCoord.st;
int j;
int i;
for(i=-4; i<4; i++){
for (j=-3; j<3; j++){
sum += texture(tex0, st + vec2(j, i)*0.004) * 0.25;
}
}

if (texture(tex0, st).r < 0.3){
outputColor = sum * sum * 0.012 + texture(tex0, st);
} else {
if (texture(tex0, st).r < 0.5){
outputColor = sum * sum * 0.009 + texture(tex0, st);
} else {
outputColor = sum * sum * 0.0075 + texture(tex0, st);
}
}
outputColor.a = texture(tex0, st).a;
});

} else { // OpenGL 2.0
fragmentShader = ""; // For some reason "#version 120\n" makes this break.
fragmentShader += STRINGIFY(uniform sampler2DRect tex0;

void main(){
vec4 sum = vec4(0);
vec2 st = gl_TexCoord[0].st;
int j;
int i;
for(i=-4; i<4; i++){
for (j=-3; j<3; j++){
sum += texture2DRect(tex0, st + vec2(j, i)*0.004) * 0.25;
}
}

if (texture2DRect(tex0, st).r < 0.3){
gl_FragColor = sum * sum * 0.012 + texture2DRect(tex0, st);
} else {
if (texture2DRect(tex0, st).r < 0.5){
gl_FragColor = sum * sum * 0.009 + texture2DRect(tex0, st);
} else {
gl_FragColor = sum * sum * 0.0075 + texture2DRect(tex0, st);
}
}
gl_FragColor.a = texture2DRect(tex0, st).a;
});
}
}
};
49 changes: 45 additions & 4 deletions src/filters/ofxBlur.h
@@ -1,5 +1,5 @@
/*
* ofxBlurFast.h
* ofxBlur.h
*
* Created by Patricio Gonzalez Vivo on 25/11/11.
* Copyright (c) 2011 http://PatricioGonzalezVivo.com All rights reserved.
Expand Down Expand Up @@ -60,11 +60,52 @@ class ofxBlur : public ofxFXObject {

// Fade constant
fade = 0.03f;

// Since we want to run several passes and we have backbuffer declared,
// it will contain the first frame we want to act on during the first
// pass, and after that it will contain the result of each previous pass.
fragmentShader = STRINGIFY(

gl3FragmentShader = "#version 150\n";
gl3FragmentShader += STRINGIFY(
out vec4 outputColor;

uniform sampler2DRect backbuffer;
uniform float fade;

float kernel[9];
vec2 offset[9];

void main(void){
vec2 st = gl_FragCoord.st;
vec4 sum = vec4(0.0);

offset[0] = vec2(-1.0, -1.0);
offset[1] = vec2(0.0, -1.0);
offset[2] = vec2(1.0, -1.0);

offset[3] = vec2(-1.0, 0.0);
offset[4] = vec2(0.0, 0.0);
offset[5] = vec2(1.0, 0.0);

offset[6] = vec2(-1.0, 1.0);
offset[7] = vec2(0.0, 1.0);
offset[8] = vec2(1.0, 1.0);

kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;
kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;
kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;

int i = 0;
for (i = 0; i < 9; i++){
vec4 tmp = texture(backbuffer, st + offset[i]);
sum += tmp * kernel[i];
}

vec4 previousValue = texture(backbuffer, st);
outputColor = (1.0 - fade) * previousValue + fade * vec4(sum.rgb, previousValue.a);
});


gl2FragmentShader = STRINGIFY(
uniform sampler2DRect backbuffer;
uniform float fade;

Expand Down Expand Up @@ -100,7 +141,7 @@ class ofxBlur : public ofxFXObject {
vec4 previousValue = texture2DRect(backbuffer, st);
gl_FragColor = (1.0 - fade) * previousValue + fade * vec4(sum.rgb, previousValue.a);
});
};
}

protected:
void injectUniforms() {
Expand Down

0 comments on commit d1fddb0

Please sign in to comment.