使用OpenGL进行三维地形动态显示程序设计的代码,目的要求:(1) 地形高程的随机生成(2) 地形平滑技术
时间: 2024-03-11 22:48:54 浏览: 67
好的,以下是使用OpenGL进行三维地形动态显示程序设计的代码,实现地形高程的随机生成和平滑技术:
注意:以下代码仅供参考,可能需要针对您的具体应用做出修改。
```c++
#include <iostream>
#include <vector>
#include <random>
#include <cmath>
// OpenGL头文件
#include <GL/glut.h>
// 地形网格的长宽
#define GRID_SIZE 100
// 地形高度图存储数组
std::vector<std::vector<float>> terrain(GRID_SIZE, std::vector<float>(GRID_SIZE));
// 随机数引擎
std::default_random_engine generator;
// 均值滤波
float smooth(std::vector<std::vector<float>> &src, int x, int y, int size) {
float sum = 0.0f;
int count = 0;
for (int i = x - size; i <= x + size; i++) {
for (int j = y - size; j <= y + size; j++) {
if (i >= 0 && i < GRID_SIZE && j >= 0 && j < GRID_SIZE) {
sum += src[i][j];
count++;
}
}
}
return sum / count;
}
// Diamond-Square算法生成高程图
void diamondSquare(int left, int top, int right, int bottom, float range) {
if (left == right || top == bottom) {
return;
}
int centerX = (left + right) / 2;
int centerY = (top + bottom) / 2;
float randValue = std::uniform_real_distribution<float>(-range, range)(generator);
terrain[centerX][centerY] = (terrain[left][top] + terrain[left][bottom] + terrain[right][top] + terrain[right][bottom]) / 4.0f + randValue;
diamondSquare(left, top, centerX, centerY, range / 2.0f);
diamondSquare(centerX, top, right, centerY, range / 2.0f);
diamondSquare(left, centerY, centerX, bottom, range / 2.0f);
diamondSquare(centerX, centerY, right, bottom, range / 2.0f);
}
// 地形高度图生成函数
void generateTerrain() {
// 生成四个角的高程值
terrain[0][0] = std::uniform_real_distribution<float>(-1.0f, 1.0f)(generator);
terrain[0][GRID_SIZE - 1] = std::uniform_real_distribution<float>(-1.0f, 1.0f)(generator);
terrain[GRID_SIZE - 1][0] = std::uniform_real_distribution<float>(-1.0f, 1.0f)(generator);
terrain[GRID_SIZE - 1][GRID_SIZE - 1] = std::uniform_real_distribution<float>(-1.0f, 1.0f)(generator);
float range = 1.0f;
while (range > 0.1f) {
diamondSquare(0, 0, GRID_SIZE - 1, GRID_SIZE - 1, range);
range /= 2.0f;
}
// 均值滤波
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
terrain[i][j] = smooth(terrain, i, j, 1);
}
}
}
// 显示回调函数
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// 视角位置
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// 绘制地形
glBegin(GL_TRIANGLES);
for (int i = 0; i < GRID_SIZE - 1; i++) {
for (int j = 0; j < GRID_SIZE - 1; j++) {
// 顶点1
glColor3f((terrain[i][j] + 1.0f) / 2.0f, (terrain[i][j] + 1.0f) / 2.0f, (terrain[i][j] + 1.0f) / 2.0f);
glVertex3f(i, terrain[i][j], j);
// 顶点2
glColor3f((terrain[i+1][j] + 1.0f) / 2.0f, (terrain[i+1][j] + 1.0f) / 2.0f, (terrain[i+1][j] + 1.0f) / 2.0f);
glVertex3f(i+1, terrain[i+1][j], j);
// 顶点3
glColor3f((terrain[i][j+1] + 1.0f) / 2.0f, (terrain[i][j+1] + 1.0f) / 2.0f, (terrain[i][j+1] + 1.0f) / 2.0f);
glVertex3f(i, terrain[i][j+1], j+1);
// 顶点2
glColor3f((terrain[i+1][j] + 1.0f) / 2.0f, (terrain[i+1][j] + 1.0f) / 2.0f, (terrain[i+1][j] + 1.0f) / 2.0f);
glVertex3f(i+1, terrain[i+1][j], j);
// 顶点3
glColor3f((terrain[i][j+1] + 1.0f) / 2.0f, (terrain[i][j+1] + 1.0f) / 2.0f, (terrain[i][j+1] + 1.0f) / 2.0f);
glVertex3f(i, terrain[i][j+1], j+1);
// 顶点4
glColor3f((terrain[i+1][j+1] + 1.0f) / 2.0f, (terrain[i+1][j+1] + 1.0f) / 2.0f, (terrain[i+1][j+1] + 1.0f) / 2.0f);
glVertex3f(i+1, terrain[i+1][j+1], j+1);
}
}
glEnd();
glutSwapBuffers();
}
// 窗口大小改变回调函数
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
}
// 主函数
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Terrain");
generateTerrain();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
```
以上代码使用Diamond-Square算法生成地形高程图,并使用均值滤波进行平滑处理。同时使用OpenGL绘制地形。颜色映射可以通过调整glColor3f函数中的参数来实现。
阅读全文