//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
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]); 
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RectTest, FromMinMax)
{
    {
        Rectd rect = Rectd::fromMinMax(Vec2d(-1, 2), Vec2d(10, 12));
        EXPECT_TRUE(rect.isValid());
        EXPECT_DOUBLE_EQ(-1, rect.min().x());
        EXPECT_DOUBLE_EQ(2,  rect.min().y());
        EXPECT_DOUBLE_EQ(10, rect.max().x());
        EXPECT_DOUBLE_EQ(12, rect.max().y());
        EXPECT_DOUBLE_EQ(11, rect.width());
        EXPECT_DOUBLE_EQ(10, rect.height());
    }

    {
        Recti rect = Recti::fromMinMax(Vec2i(-1, 2), Vec2i(10, 12));
        EXPECT_TRUE(rect.isValid());
        EXPECT_EQ(-1, rect.min().x());
        EXPECT_EQ( 2, rect.min().y());
        EXPECT_EQ(10, rect.max().x());
        EXPECT_EQ(12, rect.max().y());
        EXPECT_EQ(11, rect.width());
        EXPECT_EQ(10, rect.height());
    }

    {
        Rectui rect = Rectui::fromMinMax(Vec2ui(1, 2), Vec2ui(11, 22));
        EXPECT_TRUE(rect.isValid());
        EXPECT_EQ( 1, rect.min().x());
        EXPECT_EQ( 2, rect.min().y());
        EXPECT_EQ(11, rect.max().x());
        EXPECT_EQ(22, rect.max().y());
        EXPECT_EQ(10, rect.width());
        EXPECT_EQ(20, rect.height());
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
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);


    const size_t numOverlayItems = m_overlayItems.size();
    for (size_t i = 0; i < numOverlayItems; i++)
    {
        OverlayItem* item = m_overlayItems.at(i);

        Vec2i pos(0, 0);
        Vec2ui size(0, 0);

        if (item->layoutScheme() == OverlayItem::FIXED_POSITION)
        {
            pos = item->fixedPosition();
            size = item->sizeHint();
        }
        else
        {
            // Item should be laid out already - grab its pos/size from the layout map
            OverlayItemRectMap::iterator it = itemRectMap.find(item);
            if (it != itemRectMap.end())
            {
                Recti rect = it->second;
                pos = rect.min();
                size.set(static_cast<uint>(rect.width()), static_cast<uint>(rect.height()));
            }
        }

        if (!size.isZero())
        {
            if (useSoftwareRendering)
            {
                item->renderSoftware(oglContext, pos, size);
            }
            else
            {
                item->render(oglContext, pos, size);
            }
        }
    }


    // Restore scissor settings
    if (!scissorWasOn) glDisable(GL_SCISSOR_TEST);
    glScissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]); 
}
Exemple #4
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())));
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
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())));
        }
    }

    for (size_t i = 0; i < m_overlayItems.size(); i++)
    {
        OverlayItemLayout item = m_overlayItems.at(i);
        if ((item.corner == OverlayItem::UNMANAGED) )
        {
            Vec2ui size = item.overlayItem->sizeHint();
            Vec2i pos =  item.overlayItem->unmanagedPosition();

            if (useSoftwareRendering)
            {
                item.overlayItem->renderSoftware(oglContext, pos, size);
            }
            else
            {
                item.overlayItem->render(oglContext, pos,  size);
            }
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 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;
}
Exemple #7
0
void generate_cave(Location origin, const Recti& area) {
  set<Vec2i> dug;
  set<Vec2i> edge;

  Vec2i pos = area.min() + area.dim() / 2;
  const float floor_fraction = 0.5;
  size_t n = area.volume() * floor_fraction;

  dig(origin + pos);
  dug.insert(pos);
  for (auto a : hex_dirs) {
    if (dug.find(pos + a) == dug.end()) {
      edge.insert(pos + a);
    }
  }

  while (dug.size() < n && edge.size() > 0) {
    auto pick = rand_choice(edge);
    pos = *pick;

    int n_floor = 0;
    for (auto a : hex_dirs)
      if (dug.find(pos + a) != dug.end())
        n_floor++;

    // Decide whether to dig here. Prefer to dig in closed quarters and
    // destroy singleton pillars.
    if (n_floor < 6 && rand_int(n_floor * n_floor) > 1)
      continue;

    edge.erase(pick);

    dig(origin + pos);
    dug.insert(pos);
    for (auto a : hex_dirs) {
      if (dug.find(pos + a) == dug.end() && area.contains(pos + a)) {
        edge.insert(pos + a);
      }
    }
  }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
bool OverlayScalarMapperLegend::pick(int oglXCoord, int oglYCoord, const Vec2i& position, const Vec2ui& size)
{
    Recti oglRect(position, size.x(), size.y());

    OverlayColorLegendLayoutInfo layoutInViewPortCoords(oglRect.min(), Vec2ui(oglRect.width(), oglRect.height()));
    layoutInfo(&layoutInViewPortCoords);

    Vec2i legendBarOrigin = oglRect.min();
    legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().x());
    legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().y());

    Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.legendRect.width()), static_cast<uint>(layoutInViewPortCoords.legendRect.height()));

    if ((oglXCoord > legendBarRect.min().x()) && (oglXCoord < legendBarRect.max().x()) &&
        (oglYCoord > legendBarRect.min().y()) && (oglYCoord < legendBarRect.max().y()))
    {
        return true;
    }

    return false;
}
//--------------------------------------------------------------------------------------------------
/// 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;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RectTest, RectTypes)
{
    // Rectf
    {
        Rectf rect;
        EXPECT_FLOAT_EQ(0.0f, rect.min().x());
        EXPECT_FLOAT_EQ(0.0f, rect.min().y());
        EXPECT_FLOAT_EQ(0.0f, rect.width());
        EXPECT_FLOAT_EQ(0.0f, rect.height());

        EXPECT_FALSE(rect.isValid());
    }

    {
        Rectf rect(-1.0f, -2.0f, 5.0f, 10.0f);
        EXPECT_FLOAT_EQ(-1.0f, rect.min().x());
        EXPECT_FLOAT_EQ(-2.0f, rect.min().y());
        EXPECT_FLOAT_EQ(5.0f, rect.width());
        EXPECT_FLOAT_EQ(10.0f, rect.height());

        EXPECT_TRUE(rect.isValid());
    }

    // Recti
    {
        Recti rect;
        EXPECT_EQ(0, rect.min().x());
        EXPECT_EQ(0, rect.min().y());
        EXPECT_EQ(0, rect.width());
        EXPECT_EQ(0, rect.height());

        EXPECT_FALSE(rect.isValid());
    }

    {
        Recti rect(-1, -2, 5, 10);
        EXPECT_EQ(-1, rect.min().x());
        EXPECT_EQ(-2, rect.min().y());
        EXPECT_EQ(5, rect.width());
        EXPECT_EQ(10, rect.height());

        EXPECT_TRUE(rect.isValid());
    }

    // Rectui
    {
        Rectui rect;
        EXPECT_EQ(0, rect.min().x());
        EXPECT_EQ(0, rect.min().y());
        EXPECT_EQ(0, rect.width());
        EXPECT_EQ(0, rect.height());

        EXPECT_FALSE(rect.isValid());
    }

    {
        Rectui rect(1, 2, 5, 10);
        EXPECT_EQ(1, rect.min().x());
        EXPECT_EQ(2, rect.min().y());
        EXPECT_EQ(5, rect.width());
        EXPECT_EQ(10, rect.height());

        EXPECT_TRUE(rect.isValid());
    }
}