Vertex attributes and buffers.

This topic is 1780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello,

I'm new here and learning Direct3D. Usually I don't ask questions since Internet is already filled with answers, but failed to find a satisfying answer to the question I'm about to ask. Was thinking and thinking and couldn't find an appropriate solution... could be there's none. However, the problem.

Lets say I have a cube. In a file I could store vertex data in a compact way - 8 different vertex positions, 6 different vertex normals, 14 different texture coordinates, and finally, face definitions via respected indices. That should be the case of Wavefront OBJ file if I'm not mistaken. When reading documentation for IASetVertexBuffers I thought I was in luck and in the program can split the data in same manner in different vertex buffers, but apparently only one index buffer apply to all of them??

When loading OBJ files, then if vertex normal is different, then vertex should be duplicated. So, in memory I would have 24 vertices instead of 8, and when adding the texture coordinate attribute, I'd have them again 24 instead of 14. Bumping aside from these numbers for a moment, I managed to get to a decision that it's mesh designer's responsibility that sharp edges should have duplicated vertices / different vertex normals. But returning to the cube, is that waste considered okay since in reality I'm dealing with more complex objects than cubes and with smooth edges... unless I want to build a brick wall with individual bricks in it, lol. If so, I wouldn't even need to have cube texture unwrapped like the cross on a Bible, but just 6 sperate panes will do.

So, is that how it's meant to be or is there some better approach?

Share on other sites
I've never heard of multiple index buffers being used. So you're assumption is correct as far as I know - vertices need to be duplicated for sharp/discontinuous edges.

Share on other sites

yeah when blasting geometry at the GPU you generally format it so you have fat vertices each with their attributes (like position, normal, texture coordinate, color, etc) all packed together one after the other.  so you have one buffer of pos0, normal0, texcoord0, color0, pos1, normal1, texcoord1, color1, pos2, normal2.... you get the idea.  then another buffer that are indices for your triangles so like 0, 1, 2, 0, 2, 3, ... each index here then grabs the contiguous blob of data for the vertex at that index.

you'll find a lot of software like max and maya and formats like OBJ don't specify meshes this way.  they'll specify them as you describe where each face or triangle has separate indices for each vertex component.  so positions 0,1,2 and normals 6,1,3 and texcoords 4,3,1.

when you load data like this you do indeed have to duplicate vertices when the same vertex is referenced but with different attributes.  while this does mean potentially having many duplicate copies of attributes (like the texture coordinate 0,0 used by many vertices) it keeps things a bit simpler and avoids the GPU running all around memory gathering up the attributes from many different locations and also keeps the APIs a bit simpler as well.  in the general case of a big complex mesh created by an artist there's unlikely to be much savings in indexing attributes separately.

Edited by blackpawn

Share on other sites

Use CreateInputLayout to describe each vertex then IASetInputLayout. It requires something like

D3D11_INPUT_ELEMENT_DESC layoutPosColor[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};


which describes each vertex. Here we have a 12 byte (4 float) position followed by a 12 byte vertex color. Then you SetVertexBuffer and either call Draw() to just draw them in the order you specified (requiring duplicate vertices) or SetIndexBuffer followed by DrawIndexed(). The second method allows you to only specify a vertex once and use it multiple times. The problem with this is that it will only have one set of texture coordinates an one normal. So if a vertex has two faces like the corner of a box each with different normals (the box is flat, not rounded at the corners) you have a problem.

To answer your question: yes, you need to specify extraneous data to do what you want. The box with 6 faces each with a different normal requires 24 vertices, each with a normal and texture coordinates.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.1577756255865097, "perplexity": 1481.2894702435906}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-05/segments/1516084889325.32/warc/CC-MAIN-20180120043530-20180120063530-00519.warc.gz"}