Changeset 2507


Ignore:
Timestamp:
Feb 26, 2013, 5:22:00 PM (10 years ago)
Author:
touky
Message:

easymesh : Vertex attribute name can now be customize. Vertex declaration code simplification, ASSERT is more understandable.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/easymesh/easymesh.cpp

    r2506 r2507  
    142142    AddUniform("in_Damage");
    143143    AddUniform("u_Lights");
    144     AddAttribute("in_Vertex", VertexUsage::Position, 0);
    145     AddAttribute("in_Normal", VertexUsage::Normal, 0);
    146     AddAttribute("in_Color",  VertexUsage::Color, 0);
    147     if (with_UV)
    148         AddAttribute("in_TexCoord", VertexUsage::TexCoord, 0);
    149144}
    150145
     
    194189}
    195190
     191#define BUILD_VFLAG(bool_value, flag_value, check_flag) \
     192    bool bool_value = (check_flag & (1 << flag_value)) != 0; \
     193    check_flag &= ~(1 << flag_value);
     194#define BUILD_VFLAG_OR(bool_value, flag_value, check_flag) \
     195    bool_value = (bool_value || (check_flag & (1 << flag_value)) != 0); \
     196    check_flag &= ~(1 << flag_value);
     197#define BUILD_VFLAG_COUNT(bool_value, flag_value, check_flag, count_value) \
     198    BUILD_VFLAG(bool_value, flag_value, check_flag) \
     199    count_value += (int)bool_value;
     200
    196201//-----------------------------------------------------------------------------
    197202void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh)
    198203{
     204    uint16_t vflags = gpudata->m_vert_decl_flags;
     205
     206    BUILD_VFLAG(has_position,    VertexUsage::Position,     vflags);
     207    BUILD_VFLAG(has_normal,      VertexUsage::Normal,       vflags);
     208    BUILD_VFLAG(has_color,       VertexUsage::Color,        vflags);
     209    BUILD_VFLAG(has_texcoord,    VertexUsage::TexCoord,     vflags);
     210    BUILD_VFLAG_OR(has_texcoord, VertexUsage::TexCoordExt,  vflags);
     211    ASSERT(!vflags, String("Vertex Useage setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
     212
     213    if (has_position)   gpudata->AddAttribute(gpudata->GetInVertexName(),   VertexUsage::Position, 0);
     214    if (has_normal)     gpudata->AddAttribute(gpudata->GetInNormalName(),   VertexUsage::Normal, 0);
     215    if (has_color)      gpudata->AddAttribute(gpudata->GetInColorName(),    VertexUsage::Color, 0);
     216    if (has_texcoord)   gpudata->AddAttribute(gpudata->GetInTexCoordName(), VertexUsage::TexCoord, 0);
     217
    199218    SetupVertexData(gpudata->m_vert_decl_flags, src_mesh);
    200219
     
    227246
    228247//-----------------------------------------------------------------------------
    229 void GpuEasyMeshData::SetupVertexData(uint16_t vdecl_flags, EasyMesh* src_mesh)
     248void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
    230249{
    231250    for (int i = 0; i < m_vdatas.Count(); ++i)
    232         if (m_vdatas[i].m1 == vdecl_flags)
     251        if (m_vdatas[i].m1 == vflags)
    233252            return;
    234253
     
    244263    new_vbo->Unlock();
    245264
    246     uint16_t baseflag = (1 << VertexUsage::Position) | (1 << VertexUsage::Normal) | (1 << VertexUsage::Color);
    247     if (vdecl_flags == (baseflag | (1 << VertexUsage::TexCoordExt)) ||
    248         vdecl_flags == (baseflag | (1 << VertexUsage::TexCoord) |
    249                                    (1 << VertexUsage::TexCoordExt)))
     265    //Keep a count of the flags
     266    uint16_t saveflags = vflags;
     267    int flagnb = 0;
     268    BUILD_VFLAG_COUNT(has_position,   VertexUsage::Position,    saveflags, flagnb);
     269    BUILD_VFLAG_COUNT(has_normal,     VertexUsage::Normal,      saveflags, flagnb);
     270    BUILD_VFLAG_COUNT(has_color,      VertexUsage::Color,       saveflags, flagnb);
     271    BUILD_VFLAG_COUNT(has_texcoord,   VertexUsage::TexCoord,    saveflags, flagnb);
     272    BUILD_VFLAG_COUNT(has_texcoordExt,VertexUsage::TexCoordExt, saveflags, flagnb);
     273    ASSERT(!saveflags, String("Vertex Declaration setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
     274
     275    if (has_position && has_normal && has_color && has_texcoord && has_texcoordExt && flagnb == 5)
    250276    {
    251277        new_vdecl = new VertexDeclaration(
     
    269295        COPY_VBO;
    270296    }
    271     else if (vdecl_flags ==  (baseflag | (1 << VertexUsage::TexCoord)))
     297    else if (has_position && has_texcoord && has_texcoordExt && flagnb == 3)
     298    {
     299        new_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::TexCoord));
     300
     301        Array<vec3, vec4> vertexlist;
     302        for (int i = 0; i < src_mesh->m_vert.Count(); i++)
     303            vertexlist.Push(src_mesh->m_vert[i].m_coord, src_mesh->m_vert[i].m_texcoord);
     304
     305        vbo_data = &vertexlist[0];
     306        vbo_bytes = vertexlist.Bytes();
     307        m_vertexcount = vertexlist.Count();
     308
     309        COPY_VBO;
     310    }
     311    else if (has_position && has_texcoord && flagnb == 2)
     312    {
     313        new_vdecl = new VertexDeclaration(VertexStream<vec3,vec2>(VertexUsage::Position, VertexUsage::TexCoord));
     314
     315        Array<vec3, vec2> vertexlist;
     316        for (int i = 0; i < src_mesh->m_vert.Count(); i++)
     317            vertexlist.Push(src_mesh->m_vert[i].m_coord, src_mesh->m_vert[i].m_texcoord.xy);
     318
     319        vbo_data = &vertexlist[0];
     320        vbo_bytes = vertexlist.Bytes();
     321        m_vertexcount = vertexlist.Count();
     322
     323        COPY_VBO;
     324    }
     325    else if (has_position && has_normal && has_color && has_texcoord && flagnb == 4)
    272326    {
    273327        new_vdecl = new VertexDeclaration(
     
    291345        COPY_VBO;
    292346    }
    293     else if (vdecl_flags == baseflag)
     347    else if (has_position && has_normal && has_color && flagnb == 3)
    294348    {
    295349        new_vdecl = new VertexDeclaration(
     
    312366    }
    313367
    314     m_vdatas.Push(vdecl_flags, new_vdecl, new_vbo);
     368    m_vdatas.Push(vflags, new_vdecl, new_vbo);
    315369}
    316370
     
    338392    vdecl->Bind();
    339393
    340     uint16_t baseflag = (1 << VertexUsage::Position) | (1 << VertexUsage::Normal) | (1 << VertexUsage::Color);
    341     if (vflags == (baseflag | (1 << VertexUsage::TexCoord)) ||
    342         vflags == (baseflag | (1 << VertexUsage::TexCoordExt)) ||
    343         vflags == (baseflag | (1 << VertexUsage::TexCoord) |
    344                               (1 << VertexUsage::TexCoordExt)))
    345 
    346     {
    347         vdecl->SetStream(vbo, *gpu_sd.GetAttribute(lol::String("in_Vertex")),
    348                               *gpu_sd.GetAttribute(lol::String("in_Normal")),
    349                               *gpu_sd.GetAttribute(lol::String("in_Color")),
    350                               *gpu_sd.GetAttribute(lol::String("in_TexCoord")));
    351     }
    352     else if (vflags == baseflag)
    353     {
    354         vdecl->SetStream(vbo, *gpu_sd.GetAttribute(lol::String("in_Vertex")),
    355                               *gpu_sd.GetAttribute(lol::String("in_Normal")),
    356                               *gpu_sd.GetAttribute(lol::String("in_Color")));
    357     }
     394    BUILD_VFLAG(has_position,   VertexUsage::Position,    vflags);
     395    BUILD_VFLAG(has_normal,     VertexUsage::Normal,      vflags);
     396    BUILD_VFLAG(has_color,      VertexUsage::Color,       vflags);
     397    BUILD_VFLAG(has_texcoord,   VertexUsage::TexCoord,    vflags);
     398    BUILD_VFLAG_OR(has_texcoord,VertexUsage::TexCoordExt, vflags);
     399    ASSERT(!vflags, String("Vertex Stream setup is not implemented for : ") + VertexUsage::GetNameList(vflags) + String(", feel free to do so."));
     400
     401    int idx = 0;
     402    ShaderAttrib Attribs[4] = { lol::ShaderAttrib(), lol::ShaderAttrib(), lol::ShaderAttrib(), lol::ShaderAttrib() };
     403
     404    if (has_position)   Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInVertexName());
     405    if (has_normal)     Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInNormalName());
     406    if (has_color)      Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInColorName());
     407    if (has_texcoord)   Attribs[idx++] = *gpu_sd.GetAttribute(gpu_sd.GetInTexCoordName());
     408
     409    vdecl->SetStream(vbo, Attribs[0], Attribs[1], Attribs[2], Attribs[3]);
    358410
    359411    m_ibo->Bind();
  • trunk/src/easymesh/easymesh.h

    r2506 r2507  
    8383    //--
    8484    virtual void SetupShaderDatas(mat4 const &model) { UNUSED(model); }
     85    //--
     86    virtual lol::String GetInVertexName()   { return lol::String("in_Vertex");   }
     87    virtual lol::String GetInNormalName()   { return lol::String("in_Normal");   }
     88    virtual lol::String GetInColorName()    { return lol::String("in_Color");    }
     89    virtual lol::String GetInTexCoordName() { return lol::String("in_TexCoord"); }
    8590
    8691protected:
  • trunk/src/lol/gpu/vertexbuffer.h

    r2506 r2507  
    6060        Depth,
    6161        Sample,
     62
     63        Max
    6264    }
    6365    m_value;
    6466
     67private:
     68    static String GetName(Value v, bool use_simple)
     69    {
     70        String tmp = String("");
     71        if (!use_simple) tmp += "<";
     72        switch (v)
     73        {
     74            case Position:      { tmp += "Position";    break; }
     75            case BlendWeight:   { tmp += "BlendWeight"; break; }
     76            case BlendIndices:  { tmp += "BlendIndices";break; }
     77            case Normal:        { tmp += "Normal";      break; }
     78            case PointSize:     { tmp += "PointSize";   break; }
     79            case TexCoord:      { tmp += "TexCoord";    break; }
     80            case TexCoordExt:   { tmp += "TexCoordExt"; break; }
     81            case Tangent:       { tmp += "Tangent";     break; }
     82            case Binormal:      { tmp += "Binormal";    break; }
     83            case TessFactor:    { tmp += "TessFactor";  break; }
     84            case PositionT:     { tmp += "PositionT";   break; }
     85            case Color:         { tmp += "Color";       break; }
     86            case Fog:           { tmp += "Fog";         break; }
     87            case Depth:         { tmp += "Depth";       break; }
     88            case Sample:        { tmp += "Sample";      break; }
     89            default:            { tmp += "UNDEFINED";   break; }
     90        }
     91        if (!use_simple) tmp += ">";
     92        return tmp;
     93    }
     94
     95public:
     96    static String GetName(Value v)
     97    {
     98        return GetName(v, false);
     99    }
     100
     101    static String GetNameList(uint32_t v)
     102    {
     103        String tmp = String("<");
     104        int nb = 0;
     105        for (int i = 0; i < Max; ++i)
     106        {
     107            if (v & (1<<i))
     108            {
     109                if (nb != 0)
     110                    tmp += ", ";
     111                tmp += GetName(Value(i), true);
     112                ++nb;
     113            }
     114        }
     115        return tmp + ">";
     116    }
     117
    65118    inline VertexUsage(Value v) : m_value(v) {}
     119    inline VertexUsage(int v) : m_value((Value)v) {}
    66120    inline operator Value() { return m_value; }
    67121};
Note: See TracChangeset for help on using the changeset viewer.