Shared storage buffer object question

Greeting:
I set up a shared storage buffer object in the fragment shader (basically, followed the red book) because my program needed to exchange data with the shader.

Here’s the code in the shader:

layout(std430, binding = 0) buffer BufferObject 
{
   ivec2 coords; 
   uint objName; 
   float depth;
};

Here’s the code to access the object in the program:

   GLuint buf;
   glGenBuffers(1, &buf);
   glBindBuffer(GL_SHADER_STORAGE_BUFFER, buf);
   glBufferData(GL_SHADER_STORAGE_BUFFER, 8192, NULL, GL_DYNAMIC_COPY);
   glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buf);

   bufferData = (int*)glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_WRITE);

Now, to set the buffer variables I do:

   bufferData[0] = set coords.x;
   bufferData[1] = set coords.y;
   bufferData[2] = set objName;
   bufferData[3] = set depth;

All seems ok except I don’t think the last variable, depth, is being set properly. I suspect that’s because bufferData is cast as int* while depth is float.

What should I do in this case?

Thanks in advance,
Sam

what are you using the depth for? if its for trying to place the primitive further away from the screen that wont work. changing the depth like this wont result in a decrease or increase in the size of the primitive being drawn. to get depth perception you must use a perspective projection like frustum projection. or you could set w in your homogrnous coordinates so that all the components are divided by it resulting in a smaller primitive, best to use a frustum though

I understand that. What I am trying to do is detect the object clicked by the mouse. So, the ivec2 are click coords input from the app program. And as the frags are processed I check is (gl_FragCoord.x gl_FragCoord.y) match the mouse click coords. So far so good it’s working.

Now, the reason I need to read depth is when one object is aligned with another so I need to know which is closer, which, of course means which has lower depth. However, before I begin the “depth competition” I need to set depth to 1, the farthest value.

So, that’s the story. So I need to know how to access the depth value from the program.

Have you tried something like these?

((float*)bufferData)[3] = depth;
 bufferData[3] = (int)depth; 

You could use always use glMapBufferRange.