使用vc++6.0和opengl通过导入dem数据绘制TIN的代码
时间: 2024-03-24 07:40:31 浏览: 71
以下是使用VC++6.0和OpenGL绘制TIN的代码示例,其中包括导入DEM数据、生成TIN三角网格、绘制三角形等部分:
```c++
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_POINTS 1000000
#define MAX_TRIANGLES 1000000
// 定义点和三角形结构体
typedef struct {
double x, y, z;
} Point;
typedef struct {
int v1, v2, v3;
} Triangle;
// 定义全局变量
int num_points = 0;
Point points[MAX_POINTS];
int num_triangles = 0;
Triangle triangles[MAX_TRIANGLES];
// 读取DEM数据
void readDEM(const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Cannot open file: %s\n", filename);
exit(1);
}
// 读取点坐标
while (fscanf(fp, "%lf%lf%lf", &points[num_points].x, &points[num_points].y, &points[num_points].z) == 3) {
num_points++;
if (num_points >= MAX_POINTS) {
printf("Too many points.\n");
exit(1);
}
}
fclose(fp);
}
// 生成TIN三角网格
void generateTIN() {
// 添加3个初始点
triangles[0].v1 = 0;
triangles[0].v2 = 1;
triangles[0].v3 = 2;
num_triangles = 1;
// 逐个添加点并更新三角形
for (int i = 3; i < num_points; i++) {
Point p = points[i];
// 查找包含点p的三角形
for (int j = 0; j < num_triangles; j++) {
Triangle t = triangles[j];
Point p1 = points[t.v1];
Point p2 = points[t.v2];
Point p3 = points[t.v3];
// 判断点p是否在三角形t内
double d1 = (p.x - p1.x) * (p2.y - p1.y) - (p.y - p1.y) * (p2.x - p1.x);
double d2 = (p.x - p2.x) * (p3.y - p2.y) - (p.y - p2.y) * (p3.x - p2.x);
double d3 = (p.x - p3.x) * (p1.y - p3.y) - (p.y - p3.y) * (p1.x - p3.x);
if (d1 >= 0 && d2 >= 0 && d3 >= 0) {
// 点p在三角形t内,将t的三条边删除并添加3个新三角形
triangles[j] = triangles[num_triangles - 1];
num_triangles--;
triangles[num_triangles].v1 = t.v1;
triangles[num_triangles].v2 = t.v2;
triangles[num_triangles].v3 = i;
num_triangles++;
triangles[num_triangles].v1 = t.v2;
triangles[num_triangles].v2 = t.v3;
triangles[num_triangles].v3 = i;
num_triangles++;
triangles[num_triangles].v1 = t.v3;
triangles[num_triangles].v2 = t.v1;
triangles[num_triangles].v3 = i;
num_triangles++;
break;
}
}
}
}
// 绘制三角形
void drawTriangle(int i) {
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(points[triangles[i].v1].x, points[triangles[i].v1].y, points[triangles[i].v1].z);
glVertex3f(points[triangles[i].v2].x, points[triangles[i].v2].y, points[triangles[i].v2].z);
glVertex3f(points[triangles[i].v3].x, points[triangles[i].v3].y, points[triangles[i].v3].z);
glEnd();
}
// 绘制TIN三角网格
void drawTIN() {
glPointSize(2.0f);
glBegin(GL_POINTS);
glColor3f(0.0f, 1.0f, 0.0f);
for (int i = 0; i < num_points; i++) {
glVertex3f(points[i].x, points[i].y, points[i].z);
}
glEnd();
glLineWidth(1.0f);
for (int i = 0; i < num_triangles; i++) {
drawTriangle(i);
}
}
// 初始化OpenGL环境
void init() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
// 显示回调函数
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
drawTIN();
glPopMatrix();
glutSwapBuffers();
}
// 窗口大小改变回调函数
void reshape(int w, int h) {
glViewport(0, 0, w, h);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s demfile\n", argv[0]);
return 1;
}
readDEM(argv[1]);
generateTIN();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("TIN Viewer");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
```
其中,`readDEM`函数用于读取DEM数据,`generateTIN`函数用于生成TIN三角网格,`drawTriangle`函数用于绘制单个三角形,`drawTIN`函数用于绘制整个TIN三角网格,`init`函数用于初始化OpenGL环境,`display`函数用于绘制回调,`reshape`函数用于窗口大小改变回调。在主函数中,先调用`readDEM`和`generateTIN`函数生成TIN三角网格,然后使用glut创建窗口并注册回调函数,最后进入主循环等待事件。
阅读全文