[2.1] populate a vertex buf struct

Problems building or running the engine, queries about how to use features etc.
Post Reply
rrl
Halfling
Posts: 78
Joined: Sun Jun 04, 2017 12:33 am

[2.1] populate a vertex buf struct

Post by rrl »

I've asked about this before, but I'd like to get into a bit more detail just in this one area. I've got a problem with vertex buffers that I haven't been able to get yet. I believe my issue is in understanding the filling in of vertex data. I'm using 2.1.
E.g., this is the vertex declaration ...

Code: Select all

Ogre::VertexElement2Vec vertexElements;
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3, Ogre::VES_POSITION));
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3, Ogre::VES_NORMAL));
And here the allocation happens for the 8 vertices required to make up the cube ...

Code: Select all

CubeVertices *cubeVertices = reinterpret_cast<CubeVertices*>(OGRE_MALLOC_SIMD(sizeof(CubeVertices) * 8, Ogre::MEMCATEGORY_GEOMETRY));
And here, 'struct CubeVertices' is copied into 'cubeVertices'.

Code: Select all

memcpy(cubeVertices, c_originalVertices, sizeof(CubeVertices) * 8);
What I don't get is how px, py, pz and nx, ny, nz (all within struct CubeVertices) end up in the variable vertexElements. I see that createVertexBuffer seems to do the work (arg 4 cubeVertices into arg 1 vertexElements), but it's not clear because here's what I'm trying to do.

I have a vertex struct similar to the following.

Code: Select all

struct CubeVertices
    {
        float px, py, pz;   //Position
        float nx, ny, nz;   //Normals

	std::vector<float> coords;
	std::vector<float> normals;

        CubeVertices() {}
        CubeVertices( float _px, float _py, float _pz,
                      float _nx, float _ny, float _nz ) :
            px( _px ), py( _py ), pz( _pz ),
            nx( _nx ), ny( _ny ), nz( _nz )
        {
        }
    };
Note that instead of individual float vars, I have vectors for each coords and normals. So using 'struct CubeVertices' (I'm really using a more complicated class), how is it that createVertexBuffer knows what member fields of the struct to stuff into vertexElements? Or better yet, how is it that the vertexElement is fed using the struct above?
mrmclovin
Gnome
Posts: 324
Joined: Sun May 11, 2008 9:27 pm
x 20

Re: [2.1] populate a vertex buf struct

Post by mrmclovin »

rrl wrote: Sun Dec 10, 2017 4:57 am how is it that createVertexBuffer knows what member fields of the struct to stuff into vertexElements? Or better yet, how is it that the vertexElement is fed using the struct above?
As I understand it, the vertex declartion descibes what order they should appear:

Code: Select all

Ogre::VertexElement2Vec vertexElements;
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3, Ogre::VES_POSITION));
vertexElements.push_back(Ogre::VertexElement2(Ogre::VET_FLOAT3, Ogre::VES_NORMAL));
.. means that the vertex buffer you create using this declaration should have the following memory space, for each vertex, in order you "push_back()" them, e.g.
1. Position, e.g. 3 floats
2. Plus normals, consisting of 3 floats

In this example, the struct CubeVertices match the declaration, since its float-members are structured in the same order as the declaration declares them:

Code: Select all

float px, py, pz;   //Position
float nx, ny, nz;   //Normals
I have vectors for each coords and normals
I think there is no easy way to utilize std::vector in your struct like you do because your struct now has the size of two std::vectors and they only store the pointer to the data they store. I'm not sure why you want to store them in dynamic vectors. If you are looking for a struct that can have a flexible number of floats at compile-time, then have a look on std::array (C++11).
Post Reply