PDA

View Full Version : assign a vec4 check variable to a uniform vec4 to change the palette dynamically



Heavybrush
11-16-2016, 01:03 PM
Hi, I'm doing a cga 4 colors shader



#define _ 0.0
#define o (1./3.)
#define b (2./3.)
#define B 1.0

#define check(r,g,b) color = vec4(r,g,b,0.); dist = distance(sample,color); if (dist < bestDistance) {bestDistance = dist; bestColor = color;}

uniform vec4 cga;

//cga1
//check(_,_,_);
//check(_,b,b);
//check(b,_,b);
//check(b,b,b);

//cga2
//check(_,_,_);
//check(o,B,B);
//check(B,o,B);
//check(B,B,B);

//cga3
//check(_,_,_);
//check(_,b,_);
//check(b,_,_);
//check(b,o,_);

//cga4
//check(_,_,_);
//check(o,B,o);
//check(B,o,o);
//check(B,B,o);

//cga5
//check(_,_,_);
//check(_,b,b);
//check(b,_,_);
//check(b,b,b);

//cga6
//check(_,_,_);
//check(o,B,B);
//check(B,o,o);
//check(B,B,B);

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float dist;
float bestDistance = 1000.;
vec4 color;
vec4 bestColor;

vec2 pixel = vec2(320,200)-floor( fragCoord.xy / iResolution.xy * vec2(320,200));
vec4 sample = texture2D(iChannel0, pixel / vec2(320,200));

sample += (texture2D(iChannel0, pixel / iChannelResolution[1].xy)-0.5)*0.5;

//cga
check(_,_,_);
check(o,B,B);
check(B,o,o);
check(B,B,B);

vec4 cga = bestColor;

fragColor = cga;
}


it works perfectly but I would assign the color information of the check, into the uniform vec4 cga at the start of the code
this could give me the possibility to change dynamically the palette assigning the uniform variable inside a local variable of my application


var palette = shader_get_uniform(shader,"cga1"); //cga1, cga2 cga3...



if(shader_is_compiled(shader)) {
shader_set(shader);
var palette = shader_get_uniform(shader,"cga1");
shader_set_uniform_f(palette); //dice alla variabile che valore ha
draw_surface(application_surface,0,0);
shader_reset();
}


https://www.shadertoy.com/view/XtdXWH#

john_connor
11-17-2016, 06:52 AM
https://www.opengl.org/sdk/docs/man/html/glProgramUniform.xhtml


int location = glGetUniformLocation(program, "cga");
if (location != -1)
{
vec4 newcolor(1, 1, 1, 1);
glProgramUniform4fv(program, location, 1, &newcolor[0]);
}