3
\$\begingroup\$

I want to map out a 2D array of depth elements for the fragment shader to use to check depth against to create shadows. I want to be able to copy a float array into the GPU, but using large uniform arrays causes segfaults in openGL so that is not an option. I tried texturing but the best i got was to use GL_DEPTH_COMPONENT

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, smap);

Which doesn't work because that stores depth components (0.0 - 1.0) which I don't want because I have no idea how to calculate them using the depth value produced by the light sources MVP matrix multiplied by the coordinate of each vertex.

Is there any way to store and access large 2D arrays of floats in openGL?

\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

To pass big amounts of data for random access using textures is the correct way. Setting large amounts of uniforms is very slow and you only have a very limited amout of constant registers on most hardware.

You can use GL_R32F or GL_DEPTH_COMPONENT32F as a texture format, which has to be supported by your target hardware, to store a single single precision floating point value per sample.

A shadow map texture is normally done using a normalized depth between the near and far planes of your shadow light, eg by using the depth value after projective divide. This gives you are higher precision, fixed point textures are often more efficient than floating point textuers and are supported by a wider range of hardware.

\$\endgroup\$
2
  • \$\begingroup\$ If I had the rep I'd upvote, that was a very detailed answer thanks! I managed to fix it to work with openGL depths now, so I'm using that! Thanks! \$\endgroup\$
    – James
    Dec 8, 2012 at 19:16
  • \$\begingroup\$ How many floats would qualify as "big amounts of data"? Asking because I use uniform buffer objects for most data transfer. \$\endgroup\$
    – Ian Young
    Feb 20, 2018 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .