GLSL per pixel question

Started by
20 comments, last by McZ 18 years, 2 months ago
I have found a bunch of nice tutorials for GLSL but none of them explain the lighting equation so that I understand. and none of them use textures on the objects. I have tried without any success to modify a vertex&fragment shader to use material properties when calculating a fragment color altough I don't understand what I'm doing.
Advertisement
It would help if said more information in your post....
vertex shader
varying vec3 normal, lightDir, halfVector;	void main(){		vec4 worldPos;	normal = normalize(gl_NormalMatrix * gl_Normal);			worldPos = gl_ModelViewMatrix * gl_Vertex;	lightDir = normalize(vec3(gl_LightSource[0].position - worldPos));		halfVector = normalize(gl_LightSource[0].halfVector.xyz);	gl_TexCoord[0] = gl_MultiTexCoord0;		gl_Position = ftransform();} 


fragment shader
varying vec3 normal, lightDir, halfVector;	void main(){	vec4 diffuseLight, specularLight;		float shine = 64.0;	vec3 n = normalize(normal);	float lightIntensity = max(dot(n, normalize(lightDir)), 0.0);	diffuseLight = lightIntensity * gl_LightSource[0].diffuse;	specularLight = pow(max(dot(n, normalize(halfVector)), 0.0), shine) * gl_LightSource[0].specular;	gl_FragColor = 	diffuseLight + specularLight + gl_LightSource[0].ambient;}


EDIT: Forgot to write my question. I have the above shaders. I found them in a tutorial some where the shader looks like it works as it should but I don't know how to get the texture (have only one at the moment) into the light calculation or the material color and how to add more lights.
I don't use GLSL much and havn't in quite a while so let's see how badly I do here. :D

You get textures into your fragment program by using samplers. For example, if you are interested in using 2D textures, you would use the sampler2D type. So before main at the top, you would say "uniform sampler2D texture;".

gl_LightSource is how you access each light. In the program you have, it's accessing the first light, gl_LightSource[0]. The second light would be accessed by using gl_LightSource[1] and so on (up to 8 lights).

To access the material info, use gl_FrontMaterial or gl_BackMaterial. You will probably only be interested in gl_FrontMaterial right now.

Finally, to access your texture and add in the lighting do this:

float4 texColor = texture2D( texture, coords );
float4 shade = diffuseLight + specularLight;

gl_FragColor = shade * texColor + gl_LightSource[0].ambient;


EDIT: had ambient in the wrong place. :)

-SirKnight
You should learn the basic parts of GLSL first before you apply it to any technique.
Thank you.

so to add more lights I just add them with +

like this:
gl_FragColor = shade * texColor + gl_LightSource[0].ambient + gl_LightSource[1].ambient;

and is it the same for the diffuse and specular of the light?
eviltwigflipper > I'm trying. and I have successfully created my own fragment shader that added the texture to the world. But I failed to combine light and texture.


So I have added the lines that SirKnight wrote. And now the textures is there and lighting but there is some kind of "fog" on everything now.
Any way you can upload a screenshot somewhere?

Just remember that lights are additive so all you do is compute the lighting for each light and add the results together (then multiply that by the texture).

Also I recommend you pick up the "Orange Book" (OpenGL Shading Language Programming Guide). It's a good reference for the GLSL language and how it works.

So a shader for multiple lights would be something along these lines:

float diffuseLight, specularLight;	float shine = 64.0;vec3 n = normalize(normal);float light = 0;for( int i = 0; i < NUM_LIGHTS; ++i ){      vec3 lightDir = normalize( gl_LightSource.position - gl_Vertex.xyz );    float lightIntensity = max(dot(n, lightDir), 0.0);    diffuseLight  = lightIntensity * gl_LightSource.diffuse;    specularLight = pow(max(dot(n, normalize(gl_LightSource.halfVector.xyz)), 0.0), shine) * gl_LightSource.specular;    light += (diffuseLight + specularLight);}gl_FragColor = light * texture;



-SirKnight
Here is a screenshot


EDIT: Here is a screenshot without shader
What's your ambient set to? An ambient too large can give a washed-out look like that.



-SirKnight

This topic is closed to new replies.

Advertisement