Patch for Custom Vertex Types

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Patch for Custom Vertex Types

Post by Klasker »

I made a patch that adds support for custom vertex types in Irrlicht. The best part is that you can now use attribute variables in GLSL shaders, where previously only uniform input was possible.
NOTE: This is only implemented in OpenGL. DirectX (and software) are unchanged.

Listing the new possibilities:
  • You can have more than 2 texture coordinates per vertex.
  • You can use attributes in GLSL shaders.
  • You can use video::SColorf for vertex colors, if you prefer that.
  • You only need to store the information you need. If you vertices don't need a normal, you don't use a normal. If your position don't need a Z-coordinate, use can a core::vector2df for position instead.
So, how to use it. We start by declaring a struct for our new vertex type.

Code: Select all

struct SMyVertex
{
    static SVertexType VertexType;
    
    core::vector3df Position;
    core::vector3df Normal;
    video::SColor Color;
    core::vector2df TCoord1;
    core::vector3df TCoord2; // Note: it has 3 coordinates. This is not a problem :)
    f32 Stiffness;
};
Then, during program initialization, we initialize the SVertexType you noticed above.

Code: Select all

// .. (somewhere in program init)
CREATE_VTYPE( SMyVertex, SMyVertex::VertexType )
    V_POSITION( Position )
    V_NORMAL( Normal )
    V_COLOR( Color )
    V_TCOORD( TCoord1, 0 )
    V_TCOORD( TCoord2, 1 )
    V_SHADER( Stiffness, "Stiffness" )
END_VTYPE
After that, the vertex type is initialized and you need not worry about it anymore.

Finally, to render geometry using the vertex type there is a new overloaded version of drawIndexedTriangleList:

Code: Select all

void drawIndexedTriangleList(const void* vertices, const SVertexType& vertexType,
            s32 vertexCount, const u16* indexList, s32 triangleCount);
Religious C++ zealots might want to smite me for using macros to initialize the SVertexType, but it ensures that it is done correctly, and it doesn't do anything "strange" behind the scenes.

Anyway, you can download the patch and a small demo right here.

Hm.. normally I'd have posted a screenshot, but you can't really see anything from a screenshot in this case can you?
Post Reply