.-_'s picture

Per pixel lighting?

I 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
FingersLingers's picture
.-_ wrote:

(I sense this is a really simply question)

Yep it is rather.

                GL.Enable(EnableCap.FramebufferSrgb);

With the corrosponding Disable to stop ....

.-_'s picture

Derp. Thanks.

.-_'s picture

Adding the GL.Enable statement gives me this exception:

Unhandled Exception: OpenTK.Graphics.GraphicsErrorException: InvalidEnum
  at OpenTK.Graphics.OpenGL.ErrorHelper.CheckErrors () [0x000bd] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/Graphics/OpenGL/ErrorHelper.cs:118 
  at OpenTK.Graphics.OpenGL.ErrorHelper.Dispose () [0x00000] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/Graphics/OpenGL/ErrorHelper.cs:129 
  at OpenTK.Graphics.OpenGL.GL.Enable (EnableCap cap) [0x0000c] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/Graphics/OpenGL/GL.cs:47879 
  at Tetris.Game.OnRenderFrame (OpenTK.FrameEventArgs e) [0x00028] in /home/joseph/Projects/tk-tetris/Program.cs:96 
  at OpenTK.GameWindow.OnRenderFrameInternal (OpenTK.FrameEventArgs e) [0x00016] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/GameWindow.cs:1053 
  at OpenTK.GameWindow.RaiseRenderFrame (System.Diagnostics.Stopwatch render_watch, System.Double& next_render, OpenTK.FrameEventArgs render_args) [0x0011f] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/GameWindow.cs:539 
  at OpenTK.GameWindow.DispatchUpdateAndRenderFrame (System.Object sender, System.EventArgs e) [0x00018] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/GameWindow.cs:433 
  at OpenTK.GameWindow.Run (Double updates_per_second, Double frames_per_second) [0x000d5] in /home/joseph/Projects/tk-tetris/lib/opentk/Source/OpenTK/GameWindow.cs:411 
  at Tetris.GUIManager+<StartGame>c__AnonStorey0.<>m__0 () [0x0000c] in /home/joseph/Projects/tk-tetris/GUIManager.cs:113

Could it be hardware limitations? How would I check? I will post back with samplers result soon, but i'd like to get the colors working first so i can see the textures.

EDIT:
sqrt'ing the output of the fragment shader helps. Things are now bright enough to see, however, all objects are alpha blended. This did not happen with fixed function. Should I be using GL.Disable for blending before drawing something non-transparent?

EDIT 2:
Samplers have been added. Still no textures, though. Changes have been pushed to github.

FingersLingers's picture
.-_ wrote:

EDIT:
sqrt'ing the output of the fragment shader helps. Things are now bright enough to see, however, all objects are alpha blended. This did not happen with fixed function. Should I be using GL.Disable for blending before drawing something non-transparent?

Yes, you should disable Blending, at begining of render, draw everything which does not need belnding, then enable blending, draw what needs to be blended, Like text etc. and end your render loop, rinse and repeat.

.-_ wrote:

EDIT 2:
Samplers have been added. Still no textures, though. Changes have been pushed to github.

I'll chect it out and have a look, busy setting up the Mac with mono, so hopefully that will help me run the code and see what you see :D

I'll repost with the results.

.-_'s picture

What about the exception? Is enabling sRGB out needed?