Depth as the distance to the Camera Plane


#1

Hello Everyone,

I am currently trying to get the distance from my camera plane (using an orthographic camera) to an object that I am rendering in order to derive the depth from it. Essentially I am trying to do the following:

varying float distToCamera;

void main()
{
    vec4 cs_position = glModelViewMatrix * gl_Vertex;
    distToCamera = -cs_position.z;
    gl_Position = gl_ProjectionMatrix * cs_position;
}

as shown within this source:

However, within Cinder and GLSL, whenever I try to access either the gl_Pixel or glModeViewMatrix, my program crashes. So I am wondering if there is a replacement for these two variables or what I am trying to access in Cinder? I have had a look at the gitHub source through the link: https://github.com/cinder/Cinder/blob/master/src/cinder/gl/GlslProg.cpp But I can’t find anything conclusive.

I am fine with the fragment shader, it’s just this part in the vertex shader that’s giving me trouble. I am new to Cinder and OpenGL (to a lesser degree) so any help would be much appreciated! :slight_smile:

Thanks!


#2

I have found my solution, just in case someone finds this thread in the future.

Basically, using the following link to get a list of possible Cinder uniform variables: https://github.com/cinder/Cinder/blob/master/src/cinder/gl/GlslProg.cpp, the following translation will work:

uniform mat4 ciModelViewProjection;
uniform mat4 ciModelView;

varying float distToCamera;

void main()
{
    vec4 cs_position = ciModelView * ciPosition;
    distToCamera = -cs_position.z;

    gl_Position = ciModelViewProjection * ciPosition;
}

Basically, when Cinder says ciModelView, what they mean is glModelMatrix * glViewMatrix.

Hope that helps.


#3

Just by way of explanation, most of the built-in shader variables have been deprecated. As you found, cinder provides the most commonly used ones for you.