CameraPosition uniform?

Not too long ago, someone linked me to this JavaDoc page for shader development in jME: http://hub.jmonkeyengine.org/javadoc/com/jme3/shader/UniformBinding.html

One of the things I see in there is CameraPosition, which would definitely be a useful thing rather than passing in the camera position for some things. However, when I use it, it seems like it’s never filled and I’m always getting (0, 0, 0). I’ve added it to the parameters in my MatDef, and it’s set in GLSL as a uniform vec3. I’ve tried prefixing it with both g_ and m_ and both have the same results. Does this uniform not work anymore, am I missing something stupidly obvious, or is this some kinda bug?

Squinting to see your code but my farsight is busted.

1 Like

@pspeed Ha, ha. Here ya go lol.

The vertex shader it’s used in:


uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldMatrix;
uniform vec3 m_CameraPosition;

attribute vec4 inTangent;
attribute vec3 inPosition;
attribute vec3 inNormal;
attribute vec2 inTexCoord;

varying vec3 normal;
varying vec3 tangent;
varying vec2 texCoord;

#ifdef PARALLAX
    varying vec3 V;
#endif

void main() {
    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
    //Normals stuff
    normal = (g_WorldMatrix * vec4(inNormal, 0.0)).xyz; //World-space normals
    tangent = (g_WorldMatrix * vec4(inTangent.xyz, 0.0)).xyz; //World-space tangents
    texCoord = inTexCoord;

    #ifdef PARALLAX
        vec3 worldPosition = (g_WorldMatrix * vec4(inPosition, 1.0)).xyz;
        vec3 worldDirectionToCamera = normalize(m_CameraPosition - worldPosition);
        V = vec3(
            dot(worldDirectionToCamera, tangent),
            dot(worldDirectionToCamera, cross(normal, tangent)),
            dot(worldDirectionToCamera, normal)
        );
    #endif
}

The material definition:


MaterialDef DLighting {
    MaterialParameters {
        Color DiffuseColor
        Float Shininess
        Float Alpha
        Texture2D DiffuseMap
        Texture2D NormalMap
        Texture2D SpecularMap
        Texture2D AlphaMap
        Texture2D ParallaxMap
        Boolean Unshaded
    }
    Technique {
        VertexShader   GLSL150: Shaders/Deferred/DLighting.vert
        FragmentShader GLSL150: Shaders/Deferred/DLighting.frag
        WorldParameters {
            WorldViewProjectionMatrix
            WorldMatrix
            CameraPosition
        }
        Defines {
            NORMALMAP: NormalMap
            DIFFUSEMAP: DiffuseMap
            SPECULARMAP: SpecularMap
            ALPHA: Alpha, AlphaMap
            UNSHADED: Unshaded
            PARALLAX: ParallaxMap
        }
    }

  Technique PreNormalPass {

        VertexShader GLSL100 :   Common/MatDefs/SSAO/normal.vert
        FragmentShader GLSL100 : Common/MatDefs/SSAO/normal.frag

        WorldParameters {
            WorldViewProjectionMatrix
            WorldViewMatrix
            NormalMatrix
        }

        Defines {
            DIFFUSEMAP_ALPHA : DiffuseMap
        }
    }

    Technique PreNormalPassDerivative {

        VertexShader GLSL100 :   Common/MatDefs/MSSAO/normal.vert
        FragmentShader GLSL100 : Common/MatDefs/MSSAO/normal.frag

        WorldParameters {
            WorldViewProjectionMatrix
            WorldViewMatrix
            NormalMatrix
        }

        Defines {
            DIFFUSEMAP_ALPHA : DiffuseMap
            NUM_BONES : NumberOfBones
        }
    }
}

I’ve been messing with parallax and trying to implement it with my deferred rendering (which will already prove quite difficult due to the nature of deferred rendering). This is really my only hang-up, instead of having to pass in the camera position for every object in the scene manually.

All global uniforms are prefixed with g_… so this:
uniform vec3 m_CameraPosition;

Should be:
uniform vec3 g_CameraPosition;

…which will be available in DLighting.vert and DLighting.frag but not the other shaders (because they don’t have CameraPosition in their WorldParameters).

stick a breakpoint in the UniformBindingManager in the ‘Case CameraPosition’ statement, to make sure it is actually being called, and is what you expect.

If for some reason it doesn’t work (I never tried tbh), you can also just get the translation component of g_ViewMatrixInverse.

I use CameraPosition all the time. It definitely works if setup right.

@wezrule That might prove a bit easier, I’ll probably give that a shot.

@pspeed Hmm. Maybe I broke something? Or maybe it’s working flawlessly and I’m just crazy. I’ll figure something out.

@vinexgames said: @wezrule That might prove a bit easier, I'll probably give that a shot.

If you can’t get CameraPosition working then you will likely run into the exact same problem.

@vinexgames said: @pspeed Hmm. Maybe I broke something? Or maybe it's working flawlessly and I'm just crazy. I'll figure something out.

Well, I told you what was wrong with the code you posted. If you fix that and it still doesn’t work then post that code too.

Is your camera actually moving or are you moving the world instead? In that case, camera position and the translation of view matrix inverse will be 0,0,0 anyway.