0
\$\begingroup\$

So in the vertex shader I receive my vertex data as:

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
layout(location = 2) in vec2 texture_coordinate; // texture coordinates

In particular I would like to access the first 2 fields as if they were arrays (Imagine an SSBO).

The reasoning for this is because I need the mesh information not only to render the mesh itself but also to shade (shadows). An obvious solution is to simply pass the mesh data as well as an SSBO, but this means that now I have passed twice the same information into the GPU once as vertices for rendering and once as mesh data.

I would like to be able to access all the vertex data as if it was an array, is this possible?

\$\endgroup\$
1
\$\begingroup\$

The obvious answer is to pass the data once: as a storage buffer. Period.

Nobody's forcing you to use vertex attributes, after all. You can use gl_VertexID and gl_InstanceID to (mostly) access the right vertex data for what you're rendering.

Of course, this means that you now need to have tight coupling between the vertex format and the shader that reads it. And if you want to compress your vertex data in any way (using normalized integers or 16-bit floats or whatever), you'll have to uncompress it manually in the shader.

But otherwise no, you cannot simply pretend that a vertex attribute is an array.

\$\endgroup\$
  • \$\begingroup\$ If I don't use the vertex attributes how do I render triangles and other GL primitives? By this I mean with the attributes OpenGL does the work for me of generating the primitive fragments, without the attributes I don't know what to do \$\endgroup\$ – Makogan Jan 15 '18 at 6:24
  • 1
    \$\begingroup\$ @Makogan: "with the attributes OpenGL does the work for me of generating the primitive fragments" You seem to have a misunderstand of what the words you're using mean. "Attributes" are just the per-vertex/instance values taken from buffers defined by a VAO when you render. The data the rasterizer processes is the output values of the vertex processing stages. How you compute those output values is entirely up to you. \$\endgroup\$ – Nicol Bolas Jan 15 '18 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.