Jump to content
  • Advertisement
Sign in to follow this  
McZ

GLSL per pixel question

This topic is 4680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!