Dynamic shaders

Hello i’d like to know if it was possible to send a vec3 in the update method to a vertex shader?

any ideas?


Just put it in the update method the way you would normally give a parameter to a material

Material#setVector3(String name, Vector3f value);

1 Like

thats freakin nice, i thought you could only do that at initialization.

Just be aware that you are modifying the material so if the material is shared it will change everything using that material.

Guys im’ having a problem, Idon’t understand why the Y axis in the shader is not the same as the Y axis in my 3d scene ?.??

what im trying to do is to medify the vertex Y position (Vertical position) depending on its distance to the camera, so if its far away, it has y very low

and if its close to the camera it has an Y very high.

I don’t understand why when I do :

gl_Position = g_WorldViewProjectionMatrix* vec4(position, 1.0);
float dist = gl_Position.x - Camera.x;
gl_Position.y -= (dist * dist) * 0.001;

why does it make the substraction on the LEFT RIGHT axis instead of the UP DOWN axis???

btw (0,1,0) is cnsidered UP in my scene.

can anyone exaplay why is this heppening, i’ve spend the whole afternoon on this :frowning:

g_WorldViewProjectionMatrix will put the position in view-projection space… which is not necessarily what you want and is already relative to the camera anyway (in a special homogenous projection space).

You may need to do some reading about what the different matrixes do because even if you were doing a world-view matrix then it is already camera relative.

You could try setting dist to gl_Position.z and see if that is a good enough effect for you. Not sure what you are actually trying to achieve so I can’t say for sure. Otherwise, you will need to use the g_WorldViewMatrix and then g_ProjectionMatrix after you’ve done your monkeying with the coordinate.

hello pspeed, is it possible to update the world position of a vertex without it being relative to camera?
Ohh wait a second I think i’m starting to get it,
I used the modelviewprojection Matric directly on inPosition, so i’m in projection view (screen coordinates…)

so I guess the answer is that I have to split the In position transformations into

vec4 worldCoord = modelMat * InPosition
vec4 viewCoord = viewMat *worldCoord

gl_Position = projectionMat * viewCoord

did I just understad how it works or i’m still a retard?

yep thats fine. Where possible use the matrices which have been multiplied for you already (modelview for instance, if you don’t need it in world space for anything)

but what is the difference wetween doing this :

vec4(InPosition, 1.0);
and this :
vec4(InPosition, 0.0);

0 will neglect the translation component, and so is only useful for pure rotational matrices, hence is ideal for use with direction vectors, which have no concept of a translation (but it is not so useful when multiplied by a MVP matrix). In your case, your vector is a position and you are multiplying it by a non-pure rotation matrix, so you should use 1.0. (In doubt, just get out a pen and paper, and work it out)