Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: How does a shader itterate though lists of faces?

  1. #1
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47

    Question How does a shader itterate though lists of faces?

    So I have my mesh structured as such:

    list of vertex {x,y,z}
    list of normals {x,y,z}
    list of texture points {x,y}
    list of materials{....}
    list of faces {indexes of vertex[3], of normals[3], of texture[3], of material} //-1 is used to indicate nothing is there for textures or normals

    So this question is a bit of a 2 parter: 1) Do I need to stick to a standard data structure for the vertex shader to understand? 2)for custom lists that's I'm not iterating through, how do I pass them over to the shader?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,404
    Shaders don't iterate through faces; that part is performed implicitly by the implementation.

    For each primitive, each vertex is processed by the vertex shader, unless that vertex has been processed previously and the results still in the vertex cache. Then the values of any non-flat outputs are interpolated across the primitive, and the fragment shader invoked for each fragment which passes all of the fragment tests (pixel ownership, scissor, stencil, depth).

    The above is somewhat simplified, and ignores tessellation and geometry shaders, clipping, and probably a few other things.

    The data describing the mesh is split between vertex attributes (glVertexAttribPointer() etc) and the topology specified by the element array passed to glDrawElements().

  3. #3
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47
    So what's the least redundant way to relay that mesh while still being able to apply transforms to the model? I'm guessing most games aren't doing all that math on the CPU when they animate a mesh.

    If my goal is to have a model that makes use of the earlier mentioned attributes as well as have a bump map and texture map where I can animate the mesh without have to manipulate it at the application level (CPU), what should I make use of?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,404
    Quote Originally Posted by MrUNOwen View Post
    So what's the least redundant way to relay that mesh while still being able to apply transforms to the model? I'm guessing most games aren't doing all that math on the CPU when they animate a mesh.
    Skeletal animation (skinning) is usually performed in the vertex shader.

    The attributes for each vertex typically contain the vertex position in the "bind pose", as well as a set of <bone,weight> pairs (often stored as a uvec4+vec4 pair: 4 bone indices and 4 weights). An array of bone transformations is passed as either a uniform variable or a texture (earlier versions of OpenGL were quite limited in the total amount of space for uniform variables, so larger arrays were often stored in textures).

    The bone transformations may be stored as matrices, a quaternion (rotation) plus vector (translation), or dual quaternions. The last option is more complex mathematically, but works better with vertices which are in the middle of a joint (blending between rotations).

  5. #5
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47
    So uniforms don't change per call to render. Is there a way I'm sub-diving the mesh so only one bone is looked at or is there a way to tie the bone to a vertex?

  6. #6
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47
    Just was searching around and saw some weird things that may work for me. Is there a way I can say "these 10 integers = 1 vertex" and also send uniforms representing the those lists of vert/norm/tex/mat/etc...? So I still use the index values, but by vertex rather than face.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,404
    Quote Originally Posted by MrUNOwen View Post
    So uniforms don't change per call to render. Is there a way I'm sub-diving the mesh so only one bone is looked at or is there a way to tie the bone to a vertex?
    You'd pass the associated bone (or a list of bone/weight pairs for blended skinning) as vertex attributes.

    Quote Originally Posted by MrUNOwen View Post
    Just was searching around and saw some weird things that may work for me. Is there a way I can say "these 10 integers = 1 vertex" and also send uniforms representing the those lists of vert/norm/tex/mat/etc...? So I still use the index values, but by vertex rather than face.
    Don't do that. While it's possible to render an OBJ-style structure (where a face has separate position, normal and texture coordinate indices for each vertex), it's likely to be slower than converting the data to the format that OpenGL wants (a single index for each combination of attributes). Rendering the OBJ-style format directly (by passing indices as vertex attributes and storing the positions, normals and texture coordinates in uniform arrays or textures) requires the vertex shader to obtain the data via dependent fetches, whereas passing the actual data as attributes will have the hardware fetch the data prior to invoking the vertex shader.

    However, you'll probably have to take this approach for materials, as passing all of the material parameters via vertex attributes is likely to increase the memory usage too much.

  8. #8
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47
    Quote Originally Posted by GClements View Post
    You'd pass the associated bone (or a list of bone/weight pairs for blended skinning) as vertex attributes.....
    the format that OpenGL wants (a single index for each combination of attributes).
    That's kind of what I meant by "these 10 integers = 1 vertex" where those integers are indexes to lists of vertex,texture pts, normals & material (I arbitrarily said 10). So do I pass those indexes as a VBO or is there some other mechanism? It's been a while so I need to refresh on all these.

    Also I'm a little rusty with my OpenGL jargon, I see in/out is interchangeable with attribute/varying. Which one is common practice now? Also in terms of uniform vs attribute. Since I'm not altering the mesh, do I stick to uniform over attribute?

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,404
    Quote Originally Posted by MrUNOwen View Post
    That's kind of what I meant by "these 10 integers = 1 vertex" where those integers are indexes to lists of vertex,texture pts, normals & material (I arbitrarily said 10). So do I pass those indexes as a VBO or is there some other mechanism?
    VBO. You need to use glVertexAttribIPointer() (note the "I") for integer attributes.

    Quote Originally Posted by MrUNOwen View Post
    Also I'm a little rusty with my OpenGL jargon, I see in/out is interchangeable with attribute/varying. Which one is common practice now?
    In terms of GLSL syntax, attribute/varying are required for GLSL versions up to 1.20, but are deprecated in favour of in/out in 1.30 and later.

    Quote Originally Posted by MrUNOwen View Post
    Also in terms of uniform vs attribute. Since I'm not altering the mesh, do I stick to uniform over attribute?
    No. Uniform variables are constant for the duration of a draw call. Anything which changes per vertex needs to be an attribute.

  10. #10
    Intern Newbie
    Join Date
    Oct 2010
    Posts
    47
    At the moment I'm a little confused because I don't know what's being fed element by element and what's just sending over the whole list and individual elements are accessed like an array.
    So just to make sure we're on the same page:

    I'm sending indexes as attributes such that for the first triangle i'd have {1,1,1}{2,2,2}{3,3,3} where the first bit the vertex shader would be dealing with is {1,1,1} where I'd be getting the vertex, normal and and texture at index 1 of their corresponding list of values.

    Am I going off the rails? It's been a while so I forget how all the pieces come together.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •