snake5 icon

editor mesh code (wip)

snake5 | PRO | 02/15/15 06:27:57 PM UTC | 0 ⭐ | 901 👁️ | Never ⏰ | []
C++ |

2.39 KB

|

None

|

0 👍

/

0 👎

struct EdEntMesh : EDGUILayoutRow
{
    FINLINE void Acquire(){ ++m_refcount; }
    FINLINE void Release(){ --m_refcount; if( m_refcount <= 0 ) delete this; }
    int32_t m_refcount;
    
    EdEntMesh() :
        m_refcount( 0 ),
        m_group( true, "Mesh properties" ),
        m_ctlPos( V3(0), 2, V3(-8192), V3(8192) ),
        m_ctlAngles( V3(0), 2, V3(0), V3(360) ),
        m_ctlScaleUni( 1, 2, 0.01f, 100.0f ),
        m_ctlScaleSep( V3(1), 2, V3(0.01f), V3(100.0f) )
    {
        tyname = "mesh";
        
        m_group.caption = "Mesh properties";
        m_ctlPos.caption = "Position";
        m_ctlAngles.caption = "Rotation";
        m_ctlScaleUni.caption = "Scale (uniform)";
        m_ctlScaleSep.caption = "Scale (separate)";
        
        m_group.Add( &m_ctlPos );
        m_group.Add( &m_ctlAngles );
        m_group.Add( &m_ctlScaleUni );
        m_group.Add( &m_ctlScaleSep );
        Add( &m_group );
    }
    
    const Vec3& Pos() const { return m_ctlPos.m_value; }
    const Vec3& RotAngles() const { return m_ctlAngles.m_value; }
    float ScaleUni() const { return m_ctlScaleUni.m_value; }
    const Vec3& ScaleSep() const { return m_ctlScaleSep.m_value; }
    
    void SetPosition( const Vec3& pos ){ m_ctlPos.SetValue( pos ); }
    
    EdEntMesh& operator = ( const EdEntMesh& o )
    {
        m_ctlPos.SetValue( o.Pos() );
        m_ctlAngles.SetValue( o.RotAngles() );
        m_ctlScaleUni.SetValue( o.ScaleUni() );
        m_ctlScaleSep.SetValue( o.ScaleSep() );
        return *this;
    }
    
    virtual int OnEvent( EDGUIEvent* e )
    {
        switch( e->type )
        {
        case EDGUI_EVENT_PROPEDIT:
            RegenerateMesh();
            break;
        }
        return EDGUILayoutRow::OnEvent( e );
    }
    
    void RegenerateMesh()
    {
        if( !cached_mesh )
            cached_mesh = GR_GetMesh( "meshes/test_table.ssm" );
        if( !cached_meshinst )
        {
            cached_meshinst = g_EdScene->CreateMeshInstance();
            cached_meshinst->mesh = cached_mesh;
            cached_meshinst->textures[ 0 ] = GR_GetTexture( "textures/metal0.png" );
        }
        cached_meshinst->matrix = Mat4::CreateSRT( m_ctlScaleSep.m_value * m_ctlScaleUni.m_value, DEG2RAD( m_ctlAngles.m_value ), m_ctlPos.m_value );
    }
    
    bool RayIntersect( const Vec3& rpos, const Vec3& rdir, float outdst[1] )
    {
        return RaySphereIntersect( rpos, rdir, m_ctlPos.m_value, 1, outdst );
    }
    
    EDGUIGroup m_group;
    EDGUIPropVec3 m_ctlPos;
    EDGUIPropVec3 m_ctlAngles;
    EDGUIPropFloat m_ctlScaleUni;
    EDGUIPropVec3 m_ctlScaleSep;
    
    MeshHandle cached_mesh;
    MeshInstHandle cached_meshinst;
};
typedef Handle< EdEntMesh > EdEntMeshHandle;

Comments