How can I find the z coordinate of the pixel currently being rendered in the Fragment Shader?
Currently I am using TerrainLighting.jme3d as reference. The only thing changed is that when the .frag calculates the diffuse color in line 566 I use [java]vec4 diffuseColor = vect4 (position.z, 0, 0, 1);[/java]
This is calculated in .vert the following way: (this is default, didn’t touch it)
ERROR: 0:571: error(#143) Undeclared identifier gl_Position
ERROR: 0:571: error(#216) Vector field selection out of range 'z'
ERROR: 0:571: error(#202) No matching overloaded function found vect4
WARNING: 0:571: warning(#402) Implicit truncation of vector from size 1 to size 4.
@kotoko said:
no not the clip space, what I need is the height of the points in the world (example: how high is this mountain? 1 Km)
Well...then you need the Y not the Z component, also you have to compute them in world space.
You have to include the WolrdMatrix global uniform and declare it as g_WorldMatrix.
then you go
[java]
...
varrying float height;
...
main(){
...
height = (g_WorldMatrix * vec4(inPosition, 1.0)).y;
...
[/java]
you'll have the height of the vertex in world space
It might help to see the latest code. The code pasted above has a few problems… like using maxHeight before it’s set, etc…
Also, just as an aside, your shader will flat out fail on some platforms because your floating point literals don’t have decimal places. Lots of cases of 0 instead of 0.0, etc.
the name is not the problem it’s the value it holds
[java]
int maxHeight = 255;
diffuseColor = vec4(0, i, maxHeight, alpha);
[/java]
this is wrong. the value is out of range and you are feeding a constructor that takes floats with an int.
It does not fail on your card, because I guess the drivers are permissive enough, but that will fail on numerous other cards/system.
Same for the maxRGB_1 that you pass to your interpolate method
maxHeight should be float and equal to 1.0
since we are in best practice and things that will fail on other cards :
Always write float values with a “.0” : 1 for a float value is invalid, it should be “1.0”. Same for 0. That could be the reason of your issue since integer math might kick in at some point leading to the “break” in the color on your terrain.
Branching in a shader is usually a bad idea, you have to avoid it if possible.