Passing variables to the shader, not working (black)

Hi guys,

i was wondering why an example was not working, then i t

http://forum.openframeworks.cc/t/ofxshader:-mesh-displacement,-perlin-noise/4256/0

then i did a test:

shader.setUniform1f(“mycolor”, 0.5);

and passed this value to the shader.frag

when i do

gl_FragColor = vec4(mycolor, mycolor, 0.0, 1.);

i get only black screen, but when i put hard-coded values 0.5 i get the color.

So am i using a wrong syntax passing values to the shader?

My testApp.cpp

  
#include "testApp.h"  
#include <stdio.h>  
#include <math.h>  
  
void testApp::setup() {  
	width = ofGetWidth();  
	height = ofGetHeight();  
  
	fbo.allocate(width, height);  
  
	setupNoise();  
	shader.load("noise.vert", "noise.frag");  
}  
  
void testApp::setupNoise() {  
	  
	shader.begin();  
	const int octaves = 8;  
	const float dropoff = .5;  
	float total = 0;  
	float weights[octaves];  
	float scaling[octaves];  
	for(int i = 0; i < octaves; i++) {  
		weights[i] = 1. / exp( dropoff * i);  
		scaling[i] = exp((float)i);  
		total += weights[i];  
	}  
	  
	shader.setUniform1fv("weights", weights, octaves);  
	shader.setUniform1fv("scaling", scaling, octaves);  
	shader.setUniform1f("normalization", 1.f / total);  
	shader.setUniform1f("seed", ofRandom(8, 32));  
	shader.setUniform1f("mycolor", 0.5);  
	shader.end();  
}  
  
void testApp::update() {  
	fbo.begin();  
	shader.begin();  
  
	float n = 32 * ofDist(mouseX, mouseY, width / 2, height / 2) / width;  
  
  
	glBegin(GL_TRIANGLE_STRIP);  
  
	glTexCoord2f(-n, -n);  
	glVertex2f(0, 0);  
  
	glTexCoord2f(-n, n);  
	glVertex2f(0, height);  
  
	glTexCoord2f(n, -n);  
	glVertex2f(width, 0);  
  
	glTexCoord2f(n, n);  
	glVertex2f(width, height);  
  
	glEnd();  
  
	shader.end();  
	fbo.end();  
}  
  
void testApp::draw() {  
	ofSetColor(255, 255, 255);  
	fbo.draw(0, 0, width, height);  
  
	ofSetColor(255, 0, 0);  
	ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, 20);  
}  
  

my Shader

  
const int octaves = 8;  
const float pi = 3.1415926535;  
  
uniform float seed;  
uniform float weights[octaves];  
uniform float scaling[octaves];  
uniform float normalization;  
uniform float mycolor;  
  
  
  
float rand(vec2 coord) {  
	return fract(sin(dot(coord.xy, vec2(12.9898, 78.233))) * 43758.5453);  
}  
  
vec2 cosMix(vec2 x, vec2 y, float a) {  
	return mix(x, y, (1. - cos(a * pi)) / 2.);  
}  
  
float cosMix(float x, float y, float a) {  
	return mix(x, y, (1. - cos(a * pi)) / 2.);  
}  
  
float bilinearRand(vec2 pos) {  
	vec2 f = fract(pos);  
	vec2 left = vec2(  
		rand(floor(pos)),  
		rand(floor(pos + vec2(0, 1))));  
	vec2 right = vec2(  
		rand(floor(pos + vec2(1, 0))),  
		rand(floor(pos + vec2(1, 1))));  
	vec2 vert = cosMix(left, right, f.x);  
	return cosMix(vert.x, vert.y, f.y);  
}  
  
float noise(vec2 pos) {  
	float total = 0.;  
	for(int i = 0; i < octaves; i++)  
		total += weights[i] * bilinearRand((pos + seed * float(i + 1)) * scaling[i]);  
	return total * normalization;  
}  
  
void main(void){  
	vec2 pos = gl_TexCoord[0].xy;  
	gl_FragColor = vec4(vec3(mycolor, mycolor, mycolor), 1.);  
}  
  

Thank you

you probably need to first load() the shader; after that you can set uniforms.

best
roxlu

omg i would never see this! thank you very much, you made my day! its workin!