예제 #1
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void Rendering::renderOverlayItems(OpenGLContext* oglContext, bool useSoftwareRendering)
{
    OverlayItemRectMap itemRectMap;
    calculateOverlayItemLayout(&itemRectMap);
    
    OverlayItemRectMap::iterator it;
    for (it = itemRectMap.begin(); it != itemRectMap.end(); ++it)
    {
        OverlayItem* item = it->first;
        Recti rect = it->second;

        if (useSoftwareRendering)
        {
            item->renderSoftware(oglContext, rect.min(), Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height())));
        }
        else
        {
            item->render(oglContext, rect.min(),  Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height())));
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// Get the the overlay item (if any) at the given cursor position
//--------------------------------------------------------------------------------------------------
OverlayItem* Rendering::overlayItemFromWindowCoordinates(int x, int y)
{
    OverlayItemRectMap itemRectMap;
    calculateOverlayItemLayout(&itemRectMap);

    OverlayItemRectMap::iterator it;
    for (it = itemRectMap.begin(); it != itemRectMap.end(); ++it)
    {
        OverlayItem* item = it->first;
        Recti rect = it->second;

        if (item->pick(x, y, rect.min(), Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height()))))
        {
            return item;
        }
    }

    return NULL;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void Rendering::calculateOverlayItemLayout(OverlayItemRectMap* itemRectMap, OverlayItem::LayoutCorner corner, OverlayItem::LayoutDirection direction)
{
    const int border = 3;
    const Vec2i vpSize = Vec2i(static_cast<int>(m_camera->viewport()->width()), static_cast<int>(m_camera->viewport()->height()));
    const Vec2i vpPos  = Vec2i(m_camera->viewport()->x(), m_camera->viewport()->y());

    Vec2i cursor(0,0);
    switch (corner)
    {
        case OverlayItem::TOP_LEFT:     cursor.set(border, vpSize.y() - border); break;
        case OverlayItem::TOP_RIGHT:    cursor.set(vpSize.x() - border, vpSize.y() - border); break;
        case OverlayItem::BOTTOM_LEFT:  cursor.set(border, border); break;
        case OverlayItem::BOTTOM_RIGHT: cursor.set(vpSize.x() - border, border); break;
        default:                        cursor.set(border,border);
    }

    cursor += vpPos;

    // Adjust based on other already placed items
    OverlayItemRectMap::iterator it;
    for (it = itemRectMap->begin(); it != itemRectMap->end(); ++it)
    {
        Recti rect = it->second;

        if (rect.contains(cursor) && (direction == OverlayItem::VERTICAL))
        {
            if (corner == OverlayItem::BOTTOM_LEFT || corner == OverlayItem::BOTTOM_RIGHT)
            {
                cursor.y() += rect.height() + border;
            }
            else
            {
                cursor.y() -= rect.height() + border;
            }
        }
    }

    size_t numOverlayItems = m_overlayItems.size();
    size_t i;
    for (i = 0; i < numOverlayItems; i++)
    {
        OverlayItemLayout item = m_overlayItems.at(i);
        if ((item.corner == corner) && (item.direction == direction))
        {
            CVF_ASSERT(item.overlayItem.notNull());

            // Find this position and size
            Vec2i position = cursor;
            Vec2ui size = item.overlayItem->sizeHint();
            if ((corner == OverlayItem::TOP_RIGHT) || (corner == OverlayItem::BOTTOM_RIGHT))
            {
                position.x() -= size.x();
            }

            if ((corner == OverlayItem::TOP_LEFT) || (corner == OverlayItem::TOP_RIGHT))
            {
                position.y() -= size.y();
            }

            // Store the position in the map
            Recti rect(position.x(), position.y(), static_cast<int>(size.x()), static_cast<int>(size.y()));
            (*itemRectMap)[item.overlayItem.p()] = rect;

            // Find next position, moving the cursor
            if (direction == OverlayItem::HORIZONTAL)
            {
                if ((corner == OverlayItem::TOP_LEFT) || (corner == OverlayItem::BOTTOM_LEFT))
                {
                    cursor.x() += (size.x() + border);
                }
                else
                {
                    cursor.x() -= (size.x() + border);
                }
            }
            else if (direction == OverlayItem::VERTICAL)
            {
                if ((corner == OverlayItem::BOTTOM_LEFT) || (corner == OverlayItem::BOTTOM_RIGHT))
                {
                    cursor.y() += (size.y() + border);
                }
                else
                {
                    cursor.y() -= (size.y() + border);
                }
            }
            else
            {
                CVF_FAIL_MSG("Unhandled OverlayItem::LayoutDirection");
            }
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void Rendering::renderOverlayItems(OpenGLContext* oglContext, bool useSoftwareRendering)
{
    OverlayItemRectMap itemRectMap;
    calculateOverlayItemLayout(&itemRectMap);
    
    // Must setup a scissor to limit the overlay items to the current viewport, as they might setup a local viewport (e.g. navigation cube)
    GLboolean scissorWasOn = glIsEnabled(GL_SCISSOR_TEST);
    int scissorBox[4] = {0, 0, -1, -1};
    glGetIntegerv(GL_SCISSOR_BOX, scissorBox);
    glScissor(static_cast<GLsizei>(m_camera->viewport()->x()), static_cast<GLsizei>(m_camera->viewport()->y()), static_cast<GLsizei>(m_camera->viewport()->width()), static_cast<GLsizei>(m_camera->viewport()->height()));
    glEnable(GL_SCISSOR_TEST);

    OverlayItemRectMap::iterator it;
    for (it = itemRectMap.begin(); it != itemRectMap.end(); ++it)
    {
        OverlayItem* item = it->first;
        Recti rect = it->second;

        if (useSoftwareRendering)
        {
            item->renderSoftware(oglContext, rect.min(), Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height())));
        }
        else
        {
            item->render(oglContext, rect.min(),  Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height())));
        }
    }

    // Restore scissor settings
    if (!scissorWasOn) glDisable(GL_SCISSOR_TEST);
    glScissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]); 
}
예제 #5
0
//--------------------------------------------------------------------------------------------------
/// Get the the overlay item (if any) at the given cursor position
//--------------------------------------------------------------------------------------------------
OverlayItem* Rendering::overlayItemFromWindowCoordinates(int x, int y)
{
    OverlayItemRectMap itemRectMap;
    calculateOverlayItemLayout(&itemRectMap);

    const size_t numOverlayItems = m_overlayItems.size();
    for (size_t i = 0; i < numOverlayItems; i++)
    {
        OverlayItem* item = m_overlayItems.at(i);
        OverlayItemRectMap::iterator it = itemRectMap.find(item);
        if (it != itemRectMap.end())
        {
            Recti rect = it->second;
            if (item->pick(x, y, rect.min(), Vec2ui(static_cast<cvf::uint>(rect.width()), static_cast<cvf::uint>(rect.height()))))
            {
                return item;
            }
        }
    }

    return NULL;
}
예제 #6
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void Rendering::calculateOverlayItemLayoutForSchemeAndCorner(OverlayItemRectMap* itemRectMap, OverlayItem::LayoutScheme layoutScheme, OverlayItem::AnchorCorner anchorCorner)
{
    CVF_ASSERT(layoutScheme == OverlayItem::HORIZONTAL || layoutScheme == OverlayItem::VERTICAL);

    const int border = 3;
    const Vec2i vpSize = Vec2i(static_cast<int>(m_camera->viewport()->width()), static_cast<int>(m_camera->viewport()->height()));
    const Vec2i vpPos  = Vec2i(m_camera->viewport()->x(), m_camera->viewport()->y());

    Vec2i cursor(0,0);
    switch (anchorCorner)
    {
        case OverlayItem::TOP_LEFT:     cursor.set(border, vpSize.y() - border); break;
        case OverlayItem::TOP_RIGHT:    cursor.set(vpSize.x() - border, vpSize.y() - border); break;
        case OverlayItem::BOTTOM_LEFT:  cursor.set(border, border); break;
        case OverlayItem::BOTTOM_RIGHT: cursor.set(vpSize.x() - border, border); break;
        default:                        cursor.set(border,border);
    }

    cursor += vpPos;

    // Adjust starting cursor position based on other already placed items in this anchor corner
    // The assumption here is that for each corner, the horizontal layout has already been added to the map
    if (layoutScheme == OverlayItem::VERTICAL)
    {
        OverlayItemRectMap::iterator it;
        for (it = itemRectMap->begin(); it != itemRectMap->end(); ++it)
        {
            const OverlayItem* placedItem = it->first;
            const Recti placedItemRect = it->second;
            if (placedItem->anchorCorner() == anchorCorner && placedItemRect.contains(cursor))
            {
                if (anchorCorner == OverlayItem::BOTTOM_LEFT || anchorCorner == OverlayItem::BOTTOM_RIGHT)
                {
                    cursor.y() += placedItemRect.height() + border;
                }
                else
                {
                    cursor.y() -= placedItemRect.height() + border;
                }
            }
        }
    }

    const size_t numOverlayItems = m_overlayItems.size();
    for (size_t i = 0; i < numOverlayItems; i++)
    {
        OverlayItem* item = m_overlayItems.at(i);
        if ((item->anchorCorner() == anchorCorner) && (item->layoutScheme() == layoutScheme))
        {
            // Find this position and size
            Vec2i position = cursor;
            Vec2ui size = item->sizeHint();
            if ((anchorCorner == OverlayItem::TOP_RIGHT) || (anchorCorner == OverlayItem::BOTTOM_RIGHT))
            {
                position.x() -= size.x();
            }

            if ((anchorCorner == OverlayItem::TOP_LEFT) || (anchorCorner == OverlayItem::TOP_RIGHT))
            {
                position.y() -= size.y();
            }

            // Store the position in the map
            Recti rect(position.x(), position.y(), static_cast<int>(size.x()), static_cast<int>(size.y()));
            (*itemRectMap)[item] = rect;

            // Find next position, moving the cursor
            if (layoutScheme == OverlayItem::HORIZONTAL)
            {
                if ((anchorCorner == OverlayItem::TOP_LEFT) || (anchorCorner == OverlayItem::BOTTOM_LEFT))
                {
                    cursor.x() += (size.x() + border);
                }
                else
                {
                    cursor.x() -= (size.x() + border);
                }
            }
            else if (layoutScheme == OverlayItem::VERTICAL)
            {
                if ((anchorCorner == OverlayItem::BOTTOM_LEFT) || (anchorCorner == OverlayItem::BOTTOM_RIGHT))
                {
                    cursor.y() += (size.y() + border);
                }
                else
                {
                    cursor.y() -= (size.y() + border);
                }
            }
            else
            {
                CVF_FAIL_MSG("Unhandled OverlayItem::LayoutDirection");
            }
        }
    }
}