Vertex Declaration for Irrlicht

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Megel
Posts: 3
Joined: Mon May 19, 2008 6:41 pm

Vertex Declaration for Irrlicht

Post by Megel »

this if my method how to add vertex declaration to Irrlicht

IMeshBuffer:

Code: Select all

enum VertexType
{
Position, Normal, TexCoord ....
}

struct VertexChannel
{
     int Stride;
     char* VertexPointer;
     VertexType Type;
}

class IVertexDeclaration
{
    public:
    virtual int getStride();
    virtual int ElementOffset(VertexType type);
}

class MeshBuffer
{
    int indexCount;
    int vertexCount;
    IVertexDeclaration* Declaration;

    void* Indices;
    array<VertexChannel*> Channels;
public:

    template<typename VertexType>
    VertexType* GetVertexChannel(VertexType type)
    {         
         for(int i = 0; i < Channles.size(); i++)
              if(Channels[i]->Type == type)
                 return (VertexType*)Channles[i]->VertexPointer;
        
         VertexChannel* channel = VertexChannel();
         channel->Type = type;
         channel->Stride = sizeof(VertexType);
         channel->VertexPointer = new VertexType[vertexCount];
         Channels->Add(channel);
    }

}
Writing VertexData:

Code: Select all

char* lockData;

//for each channels
VertexChannel* channel = ...

for(int i = 0; i < vertexCount; i++)
{
    int offset = Declaration->ElementOffset(channel->Type);
    memcpy(lockData +  i * Declaration->getStride() + offset,
        channel->VertexPointer + i * channel->Stride, channel->Stride);
}
Creating Mesh:

Code: Select all

vector3df* position =  mesh->GetVertexChannel(VertexType::Position);
vector3df* normals = mesh->GetVertexChannel(VertexType::Normal);

// Filling data

P.S.
sorry for my english
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Ok...i don't get it there are some parts in your code that does not make any sense at all. for example:

Code: Select all

VertexChannel* channel = VertexChannel();
         channel->Type = type;
         channel->Stride = sizeof(VertexType);
         channel->VertexPointer = new VertexType[vertexCount];
         Channels->Add(channel); 
first u init a pointer wrong and then the vertexpointer is just filled with enums? huh?
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Megel
Posts: 3
Joined: Mon May 19, 2008 6:41 pm

Post by Megel »

Code: Select all

  template<typename T> 
    T* GetVertexChannel(VertexType type) 
    {          
         for(int i = 0; i < Channles.size(); i++) 
              if(Channels[i]->Type == type) 
                 return (T*)Channles[i]->VertexPointer; 
        
         VertexChannel* channel = VertexChannel(); 
         channel->Type = type; 
         channel->Stride = sizeof(VertexType); 
         channel->VertexPointer = new T[vertexCount]; 
         Channels->Add(channel); 
    }
this is xna style declaration. (MeshBuilder)
Last edited by Megel on Tue Nov 03, 2009 3:14 pm, edited 2 times in total.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

maybe working code would be easier to debug
Megel
Posts: 3
Joined: Mon May 19, 2008 6:41 pm

Post by Megel »

i tryed to use this declaration in my engine and is works fine.
i can insert this declaration in engine if someone will be interested.
but in will take a lot of time
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You do not allocate your vertex channel correctly, and what you have is not allowed by the C++ language. You'd need to write...

Code: Select all

VertexChannel* channel = new VertexChannel();
Your code has an implicit conversion from T* to char*, which is not allowed by the C++ language. You need an explicit cast.

Code: Select all

channel->VertexPointer = (char*)new T[vertexCount]; 
Your function falls of the end without returning the vertex data if it isn't found. You need to add the following to avoid undefined behavior.

Code: Select all

return (T*)channel->VertexPointer;
Travis
Post Reply