Polyscope 上手指南,从无知到绘制第一个mesh
Polyscope - C++
Polyscope是一个强大的模型展示框架。其基于广泛使用的 Dear imgui,后面的文章里,我会在Polyscope 的基础上添加 imgui 控件,一个用作展示,一个绘制我自己的窗体结构。
库的下载
github的托管库
由于其引用了大量其他的库,比如窗体程序glfw,ui控件程序 imgui等,git clone 的方法如下:
git clone --recursive git@github.com:nmwsharp/polyscope.git
–recursive 就可以一次把子库都下好了
Hello Triangle
我写这篇文章的目的就是教和我一样的新人,如何快速创建一个工程,网上的其他 example 都太大了,还需要其他依赖。
下载完成后,你的目录应该如下图:
该有的都有啦,创建自己的main工程吧!
建立自己的源码文件夹
首先,建立一个文件夹,我后面会做一个二叉树可视化工程,所以建立了一个bvh_viewer 文件夹。
修改 CMakeLists.txt
修改好的文件如下:
cmake_minimum_required(VERSION 3.5)
project(polyscope)
### Policy settings
cmake_policy(SET CMP0054 NEW) # don't implicitly dereference inside if()
## Project options
# Backend
set(POLYSCOPE_BACKEND_OPENGL3_GLFW "ON" CACHE BOOL "Enable openGL3_glfw backend")
set(POLYSCOPE_BACKEND_OPENGL_MOCK "ON" CACHE BOOL "Enable openGL_mock backend")
### Do anything needed for dependencies and bring their stuff in to scope
add_subdirectory(deps)
### Add things for this project
add_subdirectory(src)
# Create an executable
add_executable(
bvh_viewer
bvh_viewer/bvh_viewer.cpp
)
target_include_directories(bvh_viewer PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../deps")
target_include_directories(bvh_viewer PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../deps/args")
target_include_directories(bvh_viewer PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../deps/json/include")
target_include_directories(bvh_viewer PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(bvh_viewer polyscope)
和原先的文件对比可以发现加了下述几行:
add_executable(
bvh_viewer
bvh_viewer/bvh_viewer.cpp
)
此段代码是说,我需要生成一个名叫bvh_viewer的可执行文件,他包含一个cpp,路径为 bvh_viewer/bvh_viewer.cpp
target_include_directories(bvh_viewer PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../deps")
)
此段代码是增加include寻找路径
是为了后续服务用的,后面我有什么开源库,都可以添加到deps文件夹
target_link_libraries(bvh_viewer polyscope)
)
此段代码是说,我想要生成的 bvh_viewer 可执行文件,依赖于 polyscope 这个库(.lib,.a,还是.so 你就不要管了,cmake会帮你搞定依赖关系)
编写自己的 Hello World
我这个可以说是全网最通俗易懂,最精简的 hello world了!
bvh_viewer.cpp 全文如下
#include <vector>
#include "polyscope/polyscope.h"
#include "polyscope/surface_mesh.h"
int main()
{
// Initialize Polyscope
polyscope::init();
std::vector<glm::vec3> vertexPositions;
std::vector<std::vector<size_t>> faceIndices;
glm::vec3 p;
p[0] = 0.0;
p[1] = 0.0;
p[2] = 0.0;
vertexPositions.emplace_back(p);
p[0] = 0.0;
p[1] = 1.0;
p[2] = 0.0;
vertexPositions.emplace_back(p);
p[0] = 1.0;
p[1] = 0.0;
p[2] = 0.0;
vertexPositions.emplace_back(p);
std::vector<size_t> tri;
tri.clear();
tri.emplace_back(0);
tri.emplace_back(1);
tri.emplace_back(2);
faceIndices.emplace_back(tri);
auto psMesh = polyscope::registerSurfaceMesh("simple triangle", vertexPositions, faceIndices);
// Show the GUI
polyscope::show();
}
vertexPositions : 顶点buffer,任何face(姑且简单认为是三角形好了),都是由顶点组成的,这个buffer里存着所有的顶点的坐标信息
faceIndices:一个三角形有三个顶点,所以这是一个二维的vector,第一层是三角形,第二层是构成三角形的顶点,在vertexPositions 的下标。
在我的例程中, vertexPositions 就三个顶点。
faceIndices只有一个三角形,三个顶点的下标分别是0,1,2
按F5,享受第一个 hello world 吧
此时,你可以按照 Polyscope 介绍的,一项一项试验他所拥有的功能了