虚幻地形高度图生成及测试

embedded/2024/10/18 16:50:15/

虚幻地形高度图生成及测试

虚幻引擎地形系统将高度数据存储在高度图中,这是一个灰阶图像,使用黑白色值来存储地貌高程。在高度图中,纯黑色值表示最低点,纯白色值表示最高点。支持16位灰阶PNG、8位灰阶r8及16位灰阶r16格式。

本文测试使用开源的opencvcgal库将地形网格体采样插值生成栅格,并写入到16位灰阶PNG图片中,结果可支持导入虚幻UE Landscape

示例代码

#include<iostream>
#include<cmath>#include<CGAL/Surface_mesh.h>
#include<CGAL/Surface_mesh/IO/PLY.h>#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Projection_traits_xy_3.h>
#include <CGAL/Delaunay_triangulation_2.h>#include <CGAL/Polygon_mesh_processing/locate.h>#include <opencv2/opencv.hpp>using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Projection_traits = CGAL::Projection_traits_xy_3<Kernel>;
using Point_2 = Kernel::Point_2;
using Point_3 = Kernel::Point_3;// Triangulated Irregular Network
using TIN = CGAL::Delaunay_triangulation_2<Projection_traits>;
using Mesh = CGAL::Surface_mesh<Point_3>;int main(){Mesh mesh;CGAL::IO::read_PLY("test.ply",mesh);CGAL::Bbox_3 bbox = CGAL::bbox_3(mesh.points().begin(), mesh.points().end());std::cout << "with:" << bbox.x_span() << "\nheight:" << bbox.y_span() << std::endl;int width = std::ceil(bbox.x_span());int height = std::ceil(bbox.y_span());width = std::max(width, height);height = width;// 特殊处理,虚幻地形表示的范围为512单位 (可略)//  int max_multiple = std::ceil(bbox.zmax() / 512);//  int min_multiple = std::ceil(std::abs(bbox.zmin()) / 512);//  min_multiple = bbox.zmin() < 0 ? min_multiple : 0;int maxHeight = max_multiple * 512;int minHeight = min_multiple * 512;int maxGray = (1 << 16) - 1;//65535 cv::Mat image(height, width, CV_16UC1);TIN tin (mesh.points().begin(), mesh.points().end());TIN::Face_handle location;for (std::size_t y = 0; y < height; ++y)for (std::size_t x = 0; x < width; ++x){Point_3 query(bbox.xmin() + x * (bbox.xmax() - bbox.xmin()) / double(width),bbox.ymin() + (height - y) * (bbox.ymax() - bbox.ymin()) / double(height),0); // not relevant for location in 2Dlocation = tin.locate(query, location);// Points outside the convex hull will be colored blackif (!tin.is_infinite(location)){std::array<double, 3> barycentric_coordinates= CGAL::Polygon_mesh_processing::barycentric_coordinates(Point_2(location->vertex(0)->point().x(), location->vertex(0)->point().y()),Point_2(location->vertex(1)->point().x(), location->vertex(1)->point().y()),Point_2(location->vertex(2)->point().x(), location->vertex(2)->point().y()),Point_2(query.x(), query.y()),Kernel());double height_at_query= (barycentric_coordinates[0] * location->vertex(0)->point().z()+ barycentric_coordinates[1] * location->vertex(1)->point().z()+ barycentric_coordinates[2] * location->vertex(2)->point().z());// 重新映射高度值到0~65535double height_ratio = (height_at_query + minHeight) / (maxHeight+minHeight);image.at<uint16_t>(y,x)=(uint16_t)(height_ratio* maxGray);}else {image.at<uint16_t>(y, x) = (uint16_t)0;}}cv::imwrite("output.png", image);return 0;
}

测试运行

cmake_minimum_required(VERSION 3.1...3.23)
project(main)
# cgal
find_package(CGAL REQUIRED)
# opencv 
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )create_single_source_cgal_program("main.cpp")
target_link_libraries(main PRIVATE ${OpenCV_LIBS})

编译&构建
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake --build build --config Debug

结果

测试地形网格
在这里插入图片描述

生成的灰度图
在这里插入图片描述

导入虚幻Landscape
在这里插入图片描述

参考

  1. https://dev.epicgames.com/documentation/zh-cn/unreal-engine/importing-and-exporting-landscape-heightmaps-in-unreal-engine
  2. https://blog.csdn.net/mrbaolong/article/details/141643001?spm=1001.2014.3001.5501

http://www.ppmy.cn/embedded/103143.html

相关文章

[学习笔记]《CSAPP》深入理解计算机系统 - Chapter 2 信息的表示和处理

总结一些第二章的一些关键信息 2.1 信息存储大小端补码/反码/原码 updating... 2.1 信息存储 8 位的块代表一个字节&#xff0c;作为最小的可寻址内存单位&#xff0c;而不是直接访问内存中单独的位。内存的每个字节都由唯一的一个数字来标识&#xff0c;成为它的地址&#xff…

LLM 应用开发入门 - 实现 langchain.js ChatModel 接入火山引擎大模型和实现一个 CLI 聊天机器人(下)

书接上回,我们已经实现了一个 langchain.js 接入火山引擎的 ChatModel。 本文我们实现将这个大模型接入到聊天 CLI 实现和大模型进行交互式问答 需求 我们希望这个简易的聊天 CLI 能够拥有以下功能 启动时由用户输入 prompt支持回答流式输出支持连续聊天和清空上下文聊天 C…

filezilla使用教程(window下filezilla使用教程)

filezilla使用教程&#xff08;window下filezilla使用教程&#xff09; 一、安装与配置 首先&#xff0c;你需要从FileZilla的官方网站下载并安装适合你操作系统的版本。安装完成后&#xff0c;打开FileZilla&#xff0c;你将看到一个简洁的用户界面。 在FileZilla中&#x…

Plus and Multiply(1500)

Plus and Multiply 题面翻译 有一个无穷大的正整数集合 S S S&#xff0c;该集合按下面所述方法生成&#xff1a; 数字 1 1 1 在集合 S S S 中。 若数字 x x x 在该集合中&#xff0c;那么数 x a x \times a xa 和数 x b xb xb 均在集合 S S S 中。&#xff08;其中…

【Python机器学习】NLP分词——利用分词器构建词汇表(五)——将词汇表扩展到n-gram

目录 n-gram概念 停用词 n-gram概念 n-gram是一个最多包含n个元素的序列&#xff0c;这些元素从由它们组成的序列&#xff08;通常是字符串&#xff09;中提取而成。一般来说&#xff0c;n-gram的“元素”可以使字符、音节、词&#xff0c;甚至是像A、T、G、C等表示DNA序列的…

R 语言学习教程,从入门到精通,R 绘图 散点图(25)

1、R 绘图 散点图 散点图是将所有的数据以点的形式展现在直角坐标系上&#xff0c;以显示变量之间的相互影响程度&#xff0c;点的位置由变量的数值决定&#xff0c;每个点对应一个 X 和 Y 轴点坐标。 散点图可以使用 plot() 函数来绘制&#xff0c;语法格式如下&#xff1a; …

【Rust光年纪】深度解读:Rust语言中各类消息队列客户端库详细对比

选择最佳 Rust 消息队列客户端库&#xff1a;全面对比与分析 前言 随着现代应用程序的复杂性不断增加&#xff0c;消息队列成为构建可靠、高性能系统的重要组件。本文将介绍一些用于Rust语言的消息队列客户端库&#xff0c;包括AMQP、Apache Kafka、NSQ、Apache Pulsar和Rock…

SpringMVC 笔记篇

1.1 执行流程 1.1.7 RequestMappingHandlerAdapter.afterPropertiesSet() 除了HandlerMapping之外&#xff0c;其他的有没有自己的初始化逻辑呢&#xff0c;也是有的&#xff0c;我们拿HandlerAdapter中的RequestMappingHandlerAdapter来举例&#xff0c;那么首先我们找到Requ…