# Vertex coordinate when writing a shader

Hello!

If i place a sphere, make it’s R=10 and attach it to the node, which is then attached to the root node, and then make node.setLocalTranslation(5,5,5), I would get a sphere, which center is shifted 5 units in XYZ direction. Something like this:

[java]Sphere s = new Sphere(32,32,10);

Geometry g = new Geometry(“sphere”, s);

Node sphereNode = new Node();

sphereNode.attachChild(g);

rootNode.attachChild(sphereNode);

sphereNode.SelLocalTranclation(5,5,5);[/java]

Let’s assume that camera is at (0,0,0) and this position is defined by camPos variable, which is then passed to the shader. Now in the shader I need to make a ray from camera to the sphere vertex.

http://i.imgur.com/roUHR.png

Will this code below be correct?

[java]attribute vec4 inPosition;

uniform m_camPos

void main(void) {

vec3 vertexPos = vec3(g_WorldMatrix * inPosition) + m_camPos;

}[/java]

Will this code output, for example, vertexPos=(5,15,5) for the most upper vertex of the sphere?

It looks correct to me but if you want a ray from the camera to the fragment, you’d better compute its position in view space.

no need to send the camera position and just do this in the shader :

``` vec3 vertexPos = vec3(g_WorldViewMatrix * inPosition); ```

edit : not completely sure taht the vec3(vec4) constructor exists so you may have to do this

``` vec3 vertexPos = vec4(g_WorldViewMatrix * inPosition).xyz; ```

1 Like
@nehon said:
vec3 vertexPos = vec3(g_WorldViewMatrix * inPosition);

Oh thanks, using g_WorldViewMatrix works good!

@nehon said:
not completely sure taht the vec3(vec4) constructor exists...

It works ok, just takes the first 3 parameters, I read it from here :)

good to know

Also make sure that it works on every platform, iOS’s GLSL compiler is sometime picky with this kind of things

@nehon said:
good to know ;)
Also make sure that it works on every platform, iOS's GLSL compiler is sometime picky with this kind of things

umm.. ok, Windows and Mac will be tested today for sure :)
@nehon said:
iOS's GLSL compiler is sometime picky with this kind of things

iOS?? oO I am spending sleepless nights working on cross-compiling jME3 to iOS and you got it working already???? WTF!!
:D j/k I guess he means the Apple OpenGL stack here.. Which is really *very* picky as they try to make "OpenGL on Mac" work the same regardless of graphics card, hence they are basically working around multiple cards bugs I guess.. Good thing about that is that running a shader on mac is as good as running about every compliance check there is on it ;)

Hehe a mac with a amd card would result in a kinda fireproof checking then XD

@normen said:
iOS?? oO I am spending sleepless nights working on cross-compiling jME3 to iOS and you got it working already???? WTF!!
:D j/k I guess he means the Apple OpenGL stack here.. Which is really *very* picky as they try to make "OpenGL on Mac" work the same regardless of graphics card, hence they are basically working around multiple cards bugs I guess.. Good thing about that is that running a shader on mac is as good as running about every compliance check there is on it ;)

lol yeah i meant the OS thingy running on mac thingy computers... eheheh ... but you got it right ;)
...
...
Fanboy.... ;)