「圍著頂部圓的每個點都需要兩個頂點」——在使用三角形帶構建圓柱側面時,為何每個圓周頂點需重複兩次?
圓柱側面展開為矩形,用三角形帶繪製時:
頂部0, 底部0, 頂部1, 底部1, 頂部2, 底部2 ...// 生成圓柱側面頂點 (三角形帶)
int sectors = 32;
float radius = 1.0f, height = 2.0f;
std::vector<Vertex> vertices;
for (int i = 0; i <= sectors; ++i) {
float angle = 2 * M_PI * i / sectors;
float x = radius * cos(angle);
float y = radius * sin(angle);
// 頂部頂點
vertices.push_back({x, y, height/2, 0, 0, 1, (float)i/sectors, 0}); // 法線向外
// 底部頂點
vertices.push_back({x, y, -height/2, 0, 0, -1, (float)i/sectors, 1}); // 法線向外
}
// 繪製
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
為了閉合圓柱,最後一個扇區需與第一個扇區連接。通常做法:
i <= sectors(即多生成一個頂點對,與第 0 個相同)。「每個點兩個頂點」實為「每個圓周角度位置生成一對頂部/底部頂點」,滿足幾何拓撲、光照法線、貼圖座標需求。原文描述略顯模糊,建議參考 OpenGL 幾何體生成教程(如 Songho OpenGL Cylinder)。