波动方程水面模拟(简版)
具体推导详见
Mathematics.for.3D.Game.Programming.and.Computer.Graphics.3Ed 的第十五章 布料和流体模拟
初始化水面顶点参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void waveNode::InitWaterData() 2 { 3 //float count = 10.0; 4 //极限0.7071 5 m_fC = 0.3; 6 m_fD = 1.0; 7 m_fT = 1.0; 8 m_fMU = 0.20; 9 m_fK1 = (4.0 - 8.0 * m_fC * m_fC * m_fT * m_fT / (m_fD * m_fD)) / (m_fMU * m_fT + 2.0); 10 m_fK2 = (m_fMU - 2.0) / (m_fMU + 2.0); 11 m_fK3 = (2.0 * m_fC * m_fC * m_fT * m_fT / (m_fD * m_fD)) / (m_fMU * m_fT + 2.0); 12 }
更新水面顶点高度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void waveNode::CalculateWave() 2 { 3 //边界不动 4 for (int y = 1; y < m_iNumTileY - 1; y++) 5 { 6 for (int x = 1; x < m_iNumTileX - 1; x++) 7 { 8 //当前和之前的顶点位置 9 pVERTEX pCntVertex = m_vecAllVertex.at(y * m_iNumTileX + x); 10 pVERTEX pPreVertex = m_vecAllVertexPre.at(y * m_iNumTileX + x); 11 osg::Vec3 cntPos = pCntVertex->pos; 12 osg::Vec3 prePos = pPreVertex->pos; 13 //前后左右当前顶点位置 14 osg::Vec3 leftPos = m_vecAllVertex.at(y * m_iNumTileX + x - 1)->pos; 15 osg::Vec3 rightPos = m_vecAllVertex.at(y * m_iNumTileX + x + 1)->pos; 16 osg::Vec3 upPos = m_vecAllVertex.at((y + 1) * m_iNumTileX + x)->pos; 17 osg::Vec3 downPos = m_vecAllVertex.at((y - 1) * m_iNumTileX + x)->pos; 18 19 float nextZ = m_fK1 * (cntPos.z()) + m_fK2 * (prePos.z()) + 20 m_fK3 * (leftPos.z() + rightPos.z() + upPos.z() + downPos.z()); 21 //更新上一次与这一次以及下一次的顶点位置信息 22 pPreVertex->pos = pCntVertex->pos; 23 pCntVertex->pos = osg::Vec3(cntPos.x(), cntPos.y(), nextZ); 24 } 25 } 26 }