Jump to content
  • Advertisement
Sign in to follow this  
reoxthen

Deciding the custom vertex structure

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I want to create a standard vertex structure that I can use to create custom meshes with a vertexbuffer. But in this point I can't decide what properties exactly I should use. I want to use vertex and pixel shaders. I have normal, position, color and texture info in my vertex structure but for example with this structure I can't make a multi-texture blending because my vertex standard have data for just 1 texture. How can I decide which data should my vertex standard include and what else can it include?

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately for "real world" scenarios you're going to have to break down and support multiple vertex formats, if only for memory/speed reasons. In a fantasy world, you could just make one huge fat vertex that had every single component known to man, but that leads to bloated vertex buffers, bad vertex fetch times, etc.

Generally it's useful to let your material define the vertex format that it requires. I.e., if your material requires 2 texcoords, normal, tangent, and position, it would somehow inform your generic geometry (as pulled from a modelling package presumably) that that's what it needed, and in what order, and what format (i.e., pos, normal, tangent, uv0, uv1, OR pos, tangent, normal, uv1, uv0, etc). DirectX introduced of the FVF for this (flexible vertex format), but have more recently switched to VertexDeclarations and descriptors, which makes more sense.

Share this post


Link to post
Share on other sites
first of all thanks for the reply.

I was planning to make a vertexbuffer object class to create, destroy and draw custom meshes from a vertex buffer. this multiple structure need is making things confusing now.

I have 2 questions here:

1- what type of vertex structure are ".x" files using?

2- do u have to pass the exact vertex structure to a vertex shader? I mean if u have color, texture and position info in your vertex structure and if your vertex shader only uses the position vector does this create a mismatch error?

Share this post


Link to post
Share on other sites
It depends on what data is in the x file. As it supports directx depending on the data contained within it could have data for different vertex structures.

Share this post


Link to post
Share on other sites
I'd suggest a functional approach. In a modern game the same mesh can be rendered using many shaders. For example, you may render a mesh with a diffuse texture and bump. So you will require a vertex with 2uv coords, normal, binormal and tangent vectors. But then you consider support for animations so you should start thinking about including bone weights. And you can go on and on. As you see, you can expand your requirements but the important thing here is that you use just what you need and only that.

Think about which kind of objects will your game use and use a format that will comply with those requirements. For example:

Players/NPC 2 Textures, Normal, Binormal, Tangent, Bone weights
Static objects 2 Textures, Normal, Binormal, Tangent
Particles 1 Textures

So, you should be able to build your vertex formats depending on your objective.

Luck!
Guimo

Share this post


Link to post
Share on other sites
Here's another angle... it can be easy to think of a "Vertex Buffer" as an array of some vertex struct, i.e.,


struct MyVertex
{
float px,py,pz;
float nx,ny,nz;
// etc.
};

MyVertex vertexBuffer[NUM_VERTS];


Instead, I suggest thinking of your vertex buffers as a raw stream which you can iterate through using a descriptor. I.e., decide what components your vertex buffer needs and create a little descriptor:


enum VertexAttribute
{
POSITION_XYZ,
NORMAL_XYZ,
NULL_TERMINATOR
};

VertexAttribute vertexDesc[]=
{
POSITION_XYZ,
NORMAL_XYZ,
NULL_TERMINATOR
};


Now, you can allocate your vertex buffer:

void* AllocVertexBuffer( VertexAttribute vertexDesc[], int numVerts );

This function would compute the size of a single vertex based on the array of VertexAttributes, then multiply that by the number of verts.

You could then also write functions for indexing into a raw vertex buffer based on the same vertex attribute descriptor.

This technique gets away from the idea of specific 'struct PosNormalTangent', 'struct PosNormalUv0Uv1Tangent' type of structures, and instead allows you to treat your vertex streams as compositions of attributes. It might be an unfamiliar way of thinking of it at first, but in the end I find it more flexible and liberating.

Also, note that what I posted is just a simple example. Your vertex descriptor might be more complex than just an enum. You might look up D3DVERTEXELEMENT9 and IDirect3DDevice9::CreateVertexDeclaration for an example of how D3D defines similar structures.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!