ofShader point light, spot light, distant light

Hey there I’m wondering if someone with more experience using shaders can help me out…

I’m trying to use some shaders like:


varying vec4 diffuse,ambientGlobal,ambient;  
varying vec3 normal,lightDir,halfVector;  
varying float dist;  
void main()  
	vec4 ecPos;  
	vec3 aux;  
	normal = normalize(gl_NormalMatrix * gl_Normal);  
    /* these are the new lines of code to compute the light's direction */  
	ecPos = gl_ModelViewMatrix * gl_Vertex;  
	aux = vec3(gl_LightSource[0].position-ecPos);  
	lightDir = normalize(aux);  
	dist = length(aux);  
	halfVector = normalize(gl_LightSource[0].halfVector.xyz);  
    /* Compute the diffuse, ambient and globalAmbient terms */  
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;  
	/* The ambient terms have been separated since one of them */  
	/* suffers attenuation */  
	ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;  
	ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;  
	gl_Position = ftransform();  



varying vec4 diffuse,ambientGlobal, ambient;  
varying vec3 normal,lightDir,halfVector;  
varying float dist;  
void main()  
	vec3 n,halfV,viewV,ldir;  
	float NdotL,NdotHV;  
	vec4 color = ambientGlobal;  
	float att;  
	/* a fragment shader can't write a varying variable, hence we need  
     a new variable to store the normalized interpolated normal */  
	n = normalize(normal);  
	/* compute the dot product between normal and normalized lightdir */  
	NdotL = max(dot(n,normalize(lightDir)),0.0);  
	if (NdotL > 0.0) {  
		att = 1.0 / (gl_LightSource[0].constantAttenuation +  
        gl_LightSource[0].linearAttenuation * dist +  
        gl_LightSource[0].quadraticAttenuation * dist * dist);  
		color += att * (diffuse * NdotL + ambient);  
		halfV = normalize(halfVector);  
		NdotHV = max(dot(n,halfV),0.0);  
		color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular *  
	gl_FragColor = color;  

I’m wondering how to actually use this shaders with openFrameworks. I kind of get how to use a shader when there’s a sampler2Drect to bind a texture to, but I have no idea how to implement the code above.

Any help would be great, although I’m not so much after links to shader resources as a demo of how to use ofShader with an ofMesh or ofTexture and a shader like the one above…there are lot’s of shaders out there, but I just can’t figure out how to use 'em :wink:

I’m learning GLSL code and i’ve found time ago this code. It’seems not so difficult to set up at first sight… but the part in which i’ve found very difficulties is in the all management of the position and set up of the lights.
You have no uniform only varying variables in the frag and in the vert, so you don’t worry to set uniforms.
between shader.begin() and shader.end() you put all the declarations for the materials of the object and the light. pay attention to use :

glMaterialfv(GL_FRONT,GL_AMBIENT, mat_ambient);

for the object that you apply the material because in the shader there is gl_FrontMaterial.ambient .in this way the shader take the values of the ambient material of the object for the computation of the ambient term. the same for the diffuse, specular and shininess.
the same thing for the light (only one light!) you must setup: ambient, diffuse, specular ,linearAttenauation,constantAttenuation, quadraticAttenuation, position…
In this sector as i said i found more trouble…

i can make a try again with the code maybe this time i will be luky!

i have made some example with light and GLSL shader. If you are interested go to my github repo: https://github.com/kalwalt/OFexamples
Best regards

Thanks so much! I will check it out.

Nice! Lights in GLSL are a bit tricky, so it’s great to have some reference examples, point light in particular.