We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I get the following error with a shader i made: OpenGL error 1282 at top endDraw(): invalid operation
First i will show what i want to do (basically a threshold shader with a color as input):
input:
output (mouseDrag is depending)
PImage img;
color targetColor = color(250, 0, 0);
float fuzziness = 0.5;
void setup() {
img = loadImage("img2.png");
size(img.width, img.height);
img.loadPixels();
//noLoop();
}
// . . . . . . . . . . . . . . .
void draw() {
image(img, 0, 0);
int x, y;
float v;
if (!keyPressed) {
for (int i = 0; i < img.pixels.length; i++) {
x = i % img.width;
y = (i - x) / img.width;
v = colorInRangeValue(img.pixels[i], targetColor, fuzziness);
if (v > 0) {
set(x, y, color(v*255));
}
else {
set(x, y, color(0));
}
}
}
}
// . . . . . . . . . . . . . . .
void mouseClicked() {
targetColor = get(mouseX, mouseY);
println(red(targetColor)+"\t"+green(targetColor)+"\t"+blue(targetColor));
}
// . . . . . . . . . . . . . . .
void mouseDragged() {
fuzziness = constrain(norm(mouseX, 0, width), 0, 1);
println(fuzziness);
}
// . . . . . . . . . . . . . . .
// returns a value between 0 and 1
float colorInRangeValue(color colorToTest, color targetColor, float fuzziness) {
// version 03
if (fuzziness == 0) {
return colorToTest == targetColor ? 1 : 0;
}
float targetRed = red(targetColor);
float targetGreen = green(targetColor);
float targetBlue = blue(targetColor);
float maxOffset = 255*fuzziness;
float minR = targetRed-maxOffset;
float maxR = targetRed+maxOffset;
float minG = targetGreen-maxOffset;
float maxG = targetGreen+maxOffset;
float minB = targetBlue-maxOffset;
float maxB = targetBlue+maxOffset;
/*
minR = constrain(minR, 0, 255);
maxR = constrain(minR, 0, 255);
minG = constrain(minR, 0, 255);
maxG = constrain(minR, 0, 255);
minB = constrain(minR, 0, 255);
maxB = constrain(minR, 0, 255);
*/
float r = red(colorToTest);
float g = green(colorToTest);
float b = blue(colorToTest);
if (r >= minR && r <= maxR && g >= minG && g <= maxG && b >= minB && b <= maxB ) {
// 0 ---------------------------------------------------------- 255
// ^ targetColor ^ colorToTest
// ^ minR ^maxR
// 0 1 0
// value red, value green, value blue
float vr, vg, vb;
if (r >= targetRed) {
vr = norm(r, maxR, targetRed);
}
else {
vr = norm(r, minR, targetRed);
}
if (g >= targetGreen) {
vg = norm(g, maxG, targetGreen);
}
else {
vg = norm(g, minG, targetGreen);
}
if (b >= targetBlue) {
vb = norm(b, maxB, targetBlue);
}
else {
vb = norm(b, minB, targetBlue);
}
float m = min(vr, vg);
m = min(m, vb);
return m < 0 ? 0 : m;
}
return 0;
}
// . . . . . . . . . . . . . . . . .
Here is my attempt to create a shader out of it:
PShader colorRange;
PImage img;
void setup() {
img = loadImage("img2.png");
size(img.width, img.height, P2D);
colorRange = loadShader("colorRange.glsl");
colorRange.set("targetColor", new PVector(1.0, 0.0, 0.0));
stroke(255, 0, 0);
rectMode(CENTER);
}
void draw() {
colorRange.set("fuzziness", norm(mouseX, 0, width));
filter(colorRange);
image(img, 0, 0);
}
colorRange.glsl
#define PROCESSING_TEXTURE_SHADER
uniform sampler2D texture;
varying vec4 vertTexCoord;
uniform float fuzziness;
uniform vec4 targetColor;
float minR, maxR, minG, maxG, minB, maxB;
float norm(float value, float start, float stop) {
return (value - start) / (stop - start);
}
void main() {
minR = targetColor.r-fuzziness;
maxR = targetColor.r+fuzziness;
minG = targetColor.g-fuzziness;
maxG = targetColor.g+fuzziness;
minB = targetColor.b-fuzziness;
maxB = targetColor.b+fuzziness;
vec4 texColor = texture2D(texture, vertTexCoord.st).rgba;
if (fuzziness == 0.0) {
gl_FragColor = texColor == targetColor ? vec4(1.0, 1.0, 1.0, 1.0) : vec4(0.0, 0.0, 0.0, 1.0);
}
else {
if ( texColor.r >= minR && texColor.r <= maxR && texColor.g >= minG && texColor.g <= maxG && texColor.b >= minB && texColor.b <= maxB ) {
// value red, value green, value blue
float vr, vg, vb;
if (texColor.r >= targetColor.r) {
vr = norm(texColor.r, maxR, targetColor.r);
}
else {
vr = norm(texColor.r, minR, targetColor.r);
}
if (texColor.g >= targetColor.g) {
vg = norm(texColor.g, maxG, targetColor.g);
}
else {
vg = norm(texColor.g, minG, targetColor.g);
}
if (texColor.b >= targetColor.b) {
vb = norm(texColor.b, maxB, targetColor.b);
}
else {
vb = norm(texColor.b, minB, targetColor.b);
}
float m = min(vr, vg);
m = min(m, vb);
gl_FragColor = m < 0.0 ? vec4(0.0, 0.0, 0.0, 1.0) : vec4(m, m, m, 1.0);
}
else {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}
}
I hope someone can help.
Answers
finally :D
the shader file: