Horde3D
http://www.horde3d.org/forums/

Need eye position in shader, is this correct?
http://www.horde3d.org/forums/viewtopic.php?f=1&t=1306
Page 1 of 1

Author:  zoombapup [ 15.11.2010, 17:38 ]
Post subject:  Need eye position in shader, is this correct?

I need the view space angle between the normal and my eye->model vector.

Here's how I'm doing it:

vec4 v;
v.xyz = viewer;
v.w = 0;
eyepos = gl_ModelViewMatrix * v;
rlpos = gl_ModelViewMatrix * gl_Vertex;
norm = gl_NormalMatrix * gl_Normal;

so eyepos should be the camera position
rlpos should be the new position of the suface
norm should be the normal of that position

I subtract eyepos from pos to get the surface->camera direction, normalize it, then dot it with the normal.

Only, it looks quite different to the same thing in rendermonkey. In that it does mostly what I want (creates a rim light), but doesnt seem to be view independant (like the view direction isnt being updated).

Anyone see anything wrong here? is viewer actually the camera position in world space or am I reading it wrong?

Author:  zoombapup [ 15.11.2010, 22:28 ]
Post subject:  Re: Need eye position in shader, is this correct?

that norm line is wrong. Wrong cut and paste. That actually barfs the engine when running a shader with it enabled. But it does the same thing (takes the normal and multiplies by the model view transform in a different form).

It looks like the viewer uniform doesnt seem to hold what I expect.

Is there a way to get the POSITION of the camera in a shader? (in view space) so I can dot it with the view space normal?

Author:  DarkAngel [ 14.12.2010, 08:27 ]
Post subject:  Re: Need eye position in shader, is this correct?

IIRC, the 'viewer' variable holds the position of the camera in world space.
You can multiply a vertex-position with 'worldMat' to convert it to world space, then perform a subtraction and normalize to get the viewer-direction in world-space.
You can then multiply the normal with 'worldNormalMat' to get the world space normal, and perform the dot product to get the angular difference.


In view space, the position of the camera should be (0,0,0) -- view space is relative to the camera.
So alternatively, if you want to work in view-space instead of world-space, just normalise the view-space vertex-position to get the direction from the camera to the vert.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/