Ticket #887: catch-patch-vertex-height.patch

File catch-patch-vertex-height.patch, 4.4 KB (added by kromxp, 13 years ago)
  • source/graphics/Patch.h

     
    6161    // is already in the DrawList
    6262    bool m_bWillBeDrawn;
    6363
     64    bool TileHasVertexBelowHeight(ssize_t x, ssize_t z, float height) const
     65    {
     66        return m_VertexHeight[x+1][z]   < height
     67            || m_VertexHeight[x+1][z+1] < height
     68            || m_VertexHeight[x][z+1]   < height
     69            || m_VertexHeight[x][z]     < height;
     70    }
     71
     72    void RefreshVertexHeight();
     73    void RecalculateMinimumHeight();
     74    bool HasAnyVertexBelowHeight(float height) const { return m_MinVertexHeight < height; }
     75
     76    float GetVertexHeight( ssize_t x, ssize_t z) const { return m_VertexHeight[x][z]; }
     77
    6478public:
    6579    // minipatches (tiles) making up the patch
    6680    CMiniPatch m_MiniPatches[PATCH_SIZE][PATCH_SIZE];
     
    6882    int m_X,m_Z;
    6983    // parent terrain
    7084    CTerrain* m_Parent;
     85    // height of each vertex of the minipatches
     86    float m_VertexHeight[PATCH_SIZE+1][PATCH_SIZE+1];
     87    // lower vertex height in this patch
     88    float m_MinVertexHeight;
    7189
    7290    // draw state...
    7391    void setDrawState(bool value) { m_bWillBeDrawn = value; };
  • source/graphics/Patch.cpp

     
    5050    m_X=x;
    5151    m_Z=z;
    5252
     53    RefreshVertexHeight();
     54
    5355    InvalidateBounds();
    5456}
    5557
     58void CPatch::RefreshVertexHeight()
     59{
     60    ssize_t init_x = m_X * PATCH_SIZE;
     61    ssize_t init_z = m_Z * PATCH_SIZE;
     62
     63    ssize_t patch_x, patch_z;
     64    ssize_t map_x, map_z;
     65
     66    map_x = init_x;
     67    for ( patch_x = 0; patch_x < PATCH_SIZE+1; ++patch_x )
     68    {
     69        map_z = init_z;
     70        for ( patch_z = 0; patch_z < PATCH_SIZE+1; ++patch_z )
     71        {
     72            m_VertexHeight[patch_x][patch_z] = m_Parent->GetVertexGroundLevel(map_x, map_z);
     73            ++map_z;
     74        }
     75        ++map_x;
     76    }
     77}
     78
     79void CPatch::RecalculateMinimumHeight()
     80{
     81    float min_h = m_VertexHeight[0][0];
     82    ssize_t x,z;
     83    for ( x=0; x < PATCH_SIZE+1; ++x )
     84    {
     85        for ( z=0; z < PATCH_SIZE+1; ++z )
     86        {
     87            min_h = std::min(min_h, m_VertexHeight[x][z]);
     88        }
     89    }
     90    m_MinVertexHeight = min_h;
     91}
     92
    5693///////////////////////////////////////////////////////////////////////////////
    5794// CalcBounds: calculating the bounds of this patch
    5895void CPatch::CalcBounds()
  • source/renderer/TerrainRenderer.cpp

     
    528528            m->fancyWaterShader = h;
    529529        }
    530530    }
    531     CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); // TODO: stop using g_Game
    532531
    533532    CLOSTexture& losTexture = g_Renderer.GetScene().GetLOSTexture();
    534533   
     
    662661   
    663662    float repeatPeriod = (fancy ? WaterMgr->m_RepeatPeriod : 16.0f);
    664663
     664    // Some offsets used to go around counterclockwise while keeping code concise
     665    const int DX[] = {1,1,0,0};
     666    const int DZ[] = {0,1,1,0};
     667
    665668    glBegin(GL_QUADS);
    666669
    667670    for(size_t i=0; i<m->visiblePatches.size(); i++)
    668671    {
    669672        CPatch* patch = m->visiblePatches[i]->GetPatch();
     673        if ( ! patch->HasAnyVertexBelowHeight(WaterMgr->m_WaterHeight) )
     674        {
     675            continue;
     676        }
    670677
    671678        for(ssize_t dx=0; dx<PATCH_SIZE; dx++)
    672679        {
    673680            for(ssize_t dz=0; dz<PATCH_SIZE; dz++)
    674681            {
    675                 ssize_t x = (patch->m_X*PATCH_SIZE + dx);
    676                 ssize_t z = (patch->m_Z*PATCH_SIZE + dz);
    677 
    678                 // Some offsets used to go around counterclockwise while keeping code concise
    679                 const int DX[] = {1,1,0,0};
    680                 const int DZ[] = {0,1,1,0};
    681 
    682                 // is any corner of the tile below the water height? if not, no point rendering it
    683                 bool shouldRender = false;
    684                 for (int j = 0; j < 4; j++)
     682                if ( ! patch->TileHasVertexBelowHeight(dx, dz, WaterMgr->m_WaterHeight) )
    685683                {
    686                     float terrainHeight = terrain->GetVertexGroundLevel(x + DX[j], z + DZ[j]);
    687                     if (terrainHeight < WaterMgr->m_WaterHeight)
    688                     {
    689                         shouldRender = true;
    690                         break;
    691                     }
    692                 }
    693                 if (!shouldRender)
    694684                    continue;
     685                }
    695686
     687                ssize_t x = (patch->m_X*PATCH_SIZE + dx);
     688                ssize_t z = (patch->m_Z*PATCH_SIZE + dz);
     689
    696690                for (int j=0; j<4; j++)
    697691                {
    698692                    ssize_t ix = x + DX[j];
     
    701695                    float vertX = ix * CELL_SIZE;
    702696                    float vertZ = iz * CELL_SIZE;
    703697
    704                     float terrainHeight = terrain->GetVertexGroundLevel(ix, iz);
     698                    float terrainHeight = patch->GetVertexHeight(dx+DX[j], dz+DZ[j]);
    705699
    706700                    if (fancy)
    707701                    {