On page 89 of David Wolff - OpenGL 4.0 Shading Language Cookbook book, it suggests a way to set up up a Phong shader in the fragment stage. It lists some different matrices to pass via uniform, and I suspect I might be passing slightly incorrect matrices, as my results are a bit off. This could be from somewhere else in the code of course, but I'd rather know for sure.

These are the matrices used that I'm curious about:

uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 MVP;

What exactly are these? At the moment I'm sending these matrices:

view*model //ModelViewMatrix;
transpose(inverse(model))) //top 3x3 row of modelview
projection*view*model //MVP;

The shader "works", but I can't get the lighting direction right.

  • $\begingroup$ They look fine to me, but be aware of row/column order of your C++ side matrices. Although as you say, your symptoms are only slightly wrong so it's probably some other issue. $\endgroup$ – PaulHK Mar 20 '17 at 10:08
  • $\begingroup$ I cant get it to handle the lightingdirection correctly $\endgroup$ – Charlie Mar 20 '17 at 11:14
  • $\begingroup$ You could fix your normals to point towards the camera (e.g. 0,0,-1) then try a forward light dir of 0,0,1 without using the normal matrix to at least eliminate that as a problem. $\endgroup$ – PaulHK Mar 20 '17 at 13:05
  • 1
    $\begingroup$ @Charlie If you've now worked out what is wrong, could you please write it up as an answer? It will be helpful to any future visitors who have a similar problem. $\endgroup$ – Dan Hulme Mar 21 '17 at 11:19
  • 1
    $\begingroup$ Don't edit the original to include the answer. It makes it harder for other visitors to see what the original problem was. They made it easy to answer your own question to avoid any confusion over what is the question and what is the answer. $\endgroup$ – Dan Hulme Mar 21 '17 at 12:43

I wasn't placing the lightposition into view space before passing it in.

vec4 LightPosition_view=view*LightPosition; //put lp into view space with the rest

When it comes to these things, you have to think in terms of spaces, and I was multiplying the view matrix and the model matrix, into view space, but not the lightposition itself.

I hope that helps anyone else facing the same/similar problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.