自從LOD地形第一節推出以來,受到不少朋友的關注,本人真是受寵若驚,無奈自己水平有限,怕寫不好讓大家對自己失望,我只能勉為其難,努力去寫,同時歡迎高人能手給于指正,大家共同學習,共同提高!
LOD地形的四叉樹算法原理就是對地形進行四叉樹分割,同時檢查該節點是否位于視截體內部,如果在視截體內部且滿足視距,周圍點高程誤差等條件時,則對該節點繼續分割,否則不予分割。其中重點是視截體的計算,以及地形的分割及渲染。下面介紹幾個系統中用到的類。
首先介紹標志節點是否分割的類Bit
類定義:
//該類根據節點的位置,為每個節點在標志段里相應位設一個標識。
/***********************************************************************
* Copyrights Reserved by QinGeSoftware
* Author : Qinge
* Filename : Bit.h 1.0
* Date: 2008-1-10
************************************************************************/
#pragma once
class Bit
{
public:
void SetScale(int nScale); //伸縮系數
void Set(int x, int y, BOOL bFlog=TRUE); //設置標志位
void Reset(); //標志清零
BOOL CreateBits(int nXBites, int nRows); //創建標志數組
BOOL IsTrue(int x, int y); //查詢該位標志
public:
Bit();
virtual ~Bit(void);
private:
unsigned char *m_pBits; //存儲位標志的指針
int m_nXBytes; //X方向的字節數
int m_nZRows; //Z方向的行數
int m_nScale; //伸縮系數
};
//類實現文件
/***********************************************************************
* Copyrights Reserved by QinGeSoftware
* Author : Qinge
* Filename : Bit.cpp 1.0
* Date: 2008-1-10
************************************************************************/
#include "StdAfx.h"
#include "Bit.h"
Bit::Bit(void)
{
m_pBits = NULL; //指針初始化為NULL
m_nXBytes = 0;
m_nZRows = 0;
m_nScale = 1; //不能初始化為0,因為是除數
}
Bit::~Bit(void)
{
if(m_pBits != NULL)
{
delete [] m_pBits; //釋放指針
m_pBits = NULL; //置為空,否則會成為野指針
}
}
BOOL Bit::CreateBits(int nXBites, int nRows)
{
//nXBits 必須是8的倍數
m_nXBytes = nXBites/8+1; //想想為什么加1
m_nZRows = nRows;
m_pBits = new unsigned char[m_nXBytes * m_nZRows]; //分配空間
memset(m_pBits, 0, m_nZRows * m_nXBytes); //標志段全部初始化0
return 0;
}
void Bit::SetScale(int nScale)
{
m_nScale = nScale; //提供操作私有變量的接口
}
void Bit::Set(int x, int y, BOOL bFlog )
{
x = x / m_nScale; //每隔m_nScale采樣
y = y / m_nScale;
unsigned char &c = m_pBits[y * m_nXBytes + x/8]; //獲得某字符的引用,注意賦值方式,否則
unsigned char d = 0x80; //后面改了白該。
d = d >>(x%8); //根據X值得不同,首位右移相應位數。移位
// 使得每個節點對應一位。
if(bFlog)
{
c|=d; //把字符C與X相應的位置為1
}
else
{
d = ~d; //和某節點對應的位為0,其余位為1
c &= d; //把字符C與X相應的位置為0
}
}
void Bit::Reset()
{
memset(m_pBits, 0, m_nXBytes * m_nZRows);
}
BOOL Bit::IsTrue(int x, int y)
{
x = x/m_nScale;
y = y/m_nScale;
unsigned char c = m_pBits[y*m_nXBytes+x/8]; //這次不是引用,想想為什么
unsigned char d = 0x80;
c = c << (x%8); //為什么不是d移位?
return c&d; //把與X對應的位返回,其余位為0
}
//該函數得到字符包含包含8個節點的標志,必須根據X的值進行移位方能找到對應的節點,這次是取得標識而不是設置標識,故不用引用。c移位而不是d移位,是為了把標識移到首位。然后和0x80進行位與操作得到BOOL值。d移位操作效果是一樣的,但不是左移而是右移。