# Am I passing the correct matrices for my shader? openGL/C++

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.

• 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. – PaulHK Mar 20 '17 at 10:08
• I cant get it to handle the lightingdirection correctly – Charlie Mar 20 '17 at 11:14
• 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. – PaulHK Mar 20 '17 at 13:05
• @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. – Dan Hulme Mar 21 '17 at 11:19
• 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. – Dan Hulme Mar 21 '17 at 12:43

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