
Per pixel lighting?
Posted Saturday, 14 April, 2012 - 23:36 by .-_ inI am trying to achieve per pixel lighting in my game. When I run my game, everything shows up solid black.
My VS:
varying vec2 texture_coordinate; void main() { vec3 normal, lightDir; vec4 diffuse; float NdotL; // Positions gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; texture_coordinate = vec2(gl_MultiTexCoord0); // Normals normal = normalize(gl_NormalMatrix * gl_Normal); lightDir = normalize(vec3(gl_LightSource[0].position)); NdotL = max(dot(normal, lightDir), 0.0); // Colors diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; gl_FrontColor = NdotL * diffuse; }
My FS:
varying vec2 texture_coordinate; uniform sampler2D color_texture; void main() { gl_FragColor = texture2D(color_texture, texture_coordinate) * gl_Color; }
I don't really know how to set gl_LightSource[0], I set them parameters on light source 0, and enable it.
Any help?


Comments
Re: Per pixel lighting?
Well, I'm now sure where things are going wrong for you bro....
Took your render code and slapped it into my sim environment (Changed it from a gl3.0 to gl2.0), with this code, like yours, but i needed to add a few more things.
I've attached 2 pictures, One which is blue, indicated the default shader is being used, as you can see I set the default color to blue,
The second is with the shader code, exactly as you have it in this post(The most recent one), and it's doing what it is supposed to.
You might want to check into your render routines and texture bindings perhaps.
Hope this helped bro :D
Happy Coding :D
Re: Per pixel lighting?
To Elaborate a bit more, now that I've seen the shader run, This is what it looks like at present,
As you can see the Lighting is uniform which is contributed to this line in the fragment shader
To get the result I think you are trying to achieve have a look at the next pic:
you can see the light is on the right hand side here, as the shading is brighter on the right, and duller on the left.
Here is the Modified Shaders, All thats been added is the interpolated Position, which is passed to the fragment shader from the vertex shader, and in the fragment shader, the light direction is calculated per fragment.
Vertex Shader
Fragment Shader
Happy Coding bro :D
Re: Per pixel lighting?
I'm beginning to think my normals might be the problem. I use XML serialization to store my models.
pastebin.com/Axuq76Lc
Re: Per pixel lighting?
Unfortunately, I'm not going to be much help there, Looking at that file, the normals look right, but i manage my stuff a bit different for gl3. Sry bro.
:(
Re: Per pixel lighting?
New shader doesn't work either. Perhaps there is something else in my rendering code? I host my code in a github repo; perhaps, if we take a look at all of the code, something will become apparent?
https://github.com/josephd/opentk-tetris
Re: Per pixel lighting?
I'll download it and have a look over the weekend, It's a long weekend here, so I'll only have internet on monday again :D
Re: Per pixel lighting?
K, So I Went through your code, I couldn't run it due to dependencies I don't have on my home machine, However, I grabbed a texture, a model and the Mesh Loader you are using and got this result with a few changes to the Shaders.
Thats using the Z.png texture with the blockGreen Mesh, as you can see the Normals etc. are all working correctly, the changes to the shaders, are to reflect how the mesh drawer is actually giving the information to the shaders, so the matching Vertex Shader is this one, the only change in it is the setting of GL_FrontColor.
The fragment shader then uses the Color from the Vertex shader to do the calcs, also I added in a bit of ambient light, so I could see the block itself.
Hope this helps to get you going again :D
EDIT: Also, the reason I Use the gl_Color in the fragment shader and not gl_FrontMaterial, is the MeshRenderer does not seem to be placing valid values for the shader to use, If I Override this in the code, it does work.
Re: Per pixel lighting?
https://www.dropbox.com/s/sua2qoerq746kf6/tetris.png
This is what the above shader gives me. It's a result, which is good, but everything is dark, and no textures are visible (I think, it's hard to tell). Any ideas?
Thanks
Re: Per pixel lighting?
Looks like your textures are not being addressed properly then.
Firstly, the dim colors:
For the dullness, thats sRBG out, you need to enable it, or in the shader just sqrt the output color to get a approximation of how it should look then.
Secondly, you might want to try these changes and see if it works for you:
Address the texture by creating a sampler for it your original texture loader
Needs to change to this
You're going to need to store the sampler ID for later use, Then when you load ad compile the Program, you need to store another uniform location
After that, when you render, you need to add the sampler to the uniform setting, here I'm binding the sampler to texture unit 0:
then all the places you bind the texture need to change slightly
And as long as i haven't missed something, That should get you working with samplers, and hopefully you should see something on the screen
Happy Coding
Re: Per pixel lighting?
How do I enable sRGB out? (I sense this is a really simply question)