<p>I wasn't placing the lightposition into view space before passing it in. </p>

<pre><code>vec4 LightPosition_view=view*LightPosition; //put lp into view space with the rest
</code></pre>

<p>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.</p>

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

CC BY-SA 3.0
2017-03-21T12:08:01.220Z
2017-03-29T20:33:10.220Z
2017-03-29T20:33:10.220Z
Charlie
<p>On page 89 of <a href="http://people.inf.elte.hu/plisaai/pdf/David%20Wolff%20-%20OpenGL%204.0%20Shading%20Language%20Cookbook%20(2).pdf" rel="nofollow noreferrer">David Wolff - OpenGL 4.0 Shading Language Cookbook</a> 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.</p>

<p>These are the matrices used that I'm curious about:</p>

<pre><code>uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 MVP;
</code></pre>

<p>What exactly are these? At the moment I'm sending these matrices:</p>

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

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

CC BY-SA 3.0
2017-03-20T10:08:24.443Z
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
CC BY-SA 3.0
2017-03-20T11:14:44.540Z
I cant get it to handle the lightingdirection correctly
Charlie
CC BY-SA 3.0
2017-03-20T13:05:11.300Z
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
CC BY-SA 3.0
2017-03-20T13:36:19.780Z
To add : Doing the above is to produce predictable results without using a normal matrix, you can then re-introduce the normal matrix to this scheme as a method for testing the normal matrix in your shader
PaulHK
CC BY-SA 3.0
2017-03-21T08:46:31.727Z
I was sending the wrong matrix in
Charlie
CC BY-SA 3.0
2017-03-21T11:19:14.023Z
@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
CC BY-SA 3.0
2017-03-21T12:08:46.737Z
@DanHulme I modified the values in the original, but ill post a clarification too
Charlie
CC BY-SA 3.0
2017-03-21T12:43:16.230Z
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
<opengl><c++><glsl><glm>
Am I passing the correct matrices for my shader? openGL/C++
Charlie
# Am I passing the correct matrices for my shader? openGL/C++

(Asked by: username_0 on 2017-03-20)

On page 89 of [David Wolff - OpenGL 4.0 Shading Language Cookbook](http://people.inf.elte.hu/plisaai/pdf/David%20Wolff%20-%20OpenGL%204.0%20Shading%20Language%20Cookbook%20(2).pdf) 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.

**Tags:** opengl, c++, glsl, glm

> **Comments:**
> **username_1**: 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.  
> **username_0**: I cant get it to handle the lightingdirection correctly  
> **username_1**: 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.  
> **username_1**: To add : Doing the above is to produce predictable results without using a normal matrix, you can then re-introduce the normal matrix to this scheme as a method for testing the normal matrix in your shader  
> **username_0**: I was sending the wrong matrix in  
> **username_2**: @username_0 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.  
> **username_0**: @username_2 I modified the values in the original, but ill post a clarification too  
> **username_2**: 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.  

---

## 1 answers

### Answer by username_0 on 2017-03-21. Score: 3 | Accepted answer

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.


---

