---
title: "LOD地形设计（二）"
author: "hereson3"
date: "2010-01-05T05:02:35Z"
tags: []
categories: ["编程语言"]
slug: "lod"
draft: false
lastmod: "2010-01-05T05:03:09Z"
id: 562558
url: "https://kungsoft.iteye.com/blog/562558"
---

自从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 &gt;&gt;(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