【发布时间】:2016-03-25 11:13:06
【问题描述】:
我正在学习 OpenGL,并且正在创建自己的球体模型。我能够画出一个完整的球体,尽管结果有些令人费解。我想知道是否有人可以解释(并且可能更正)我的代码。
基本原理:使用根据极坐标计算的笛卡尔坐标构建三角形。细分的数量告诉我生成球体点的 phi 或 theta 弧度的步骤。从特定点 P(phi, theta),我为 [phi, delta_phi], [theta, delta_tetha] 构建扇区的其他边缘,其中 phi 从 [0, pi] (180 度) 和 tetha 从 [0 , 2*pi] (360 度)。
这是我想出的代码(我使用的是 QT 对象,但它应该非常简单):
QVector3D polarToCarthesian(float rho, float phi, float theta)
{
float r = qSin(phi) * rho;
float y = qCos(phi) * rho;
float x = r * qSin(theta);
float z = r * qCos(theta);
return QVector3D{x, y, z};
}
void make_sector(QVector<QVector3D>& mesh, float phi, float theta, float rho, float deltaPhi, float deltaTheta)
{
QVector3D p1 = polarToCarthesian(rho, phi, theta);
QVector3D p2 = polarToCarthesian(rho, phi, theta + deltaTheta);
QVector3D p3 = polarToCarthesian(rho, phi + deltaPhi, theta);
QVector3D p4 = polarToCarthesian(rho, phi + deltaPhi, theta + deltaTheta);
// First Triangle
mesh.push_back(p1);
mesh.push_back(p1); // Normal
mesh.push_back(p3);
mesh.push_back(p3); // Normal
mesh.push_back(p2);
mesh.push_back(p2); // Normal
// Second Triangle
mesh.push_back(p2);
mesh.push_back(p2); // Normal
mesh.push_back(p3);
mesh.push_back(p3); // Normal
mesh.push_back(p4);
mesh.push_back(p4); // Normal
}
void build_sphere(QVector<QVector3D>& mesh, int ndiv)
{
const float PHI_MAX = static_cast<float>(M_PI);
const float THETA_MAX = static_cast<float>(M_PI) * 2;
const float delta_phi = PHI_MAX / ndiv;
const float delta_theta = THETA_MAX / ndiv;
for (int i = 0; i < ndiv; ++i) {
float phi = i * delta_phi;
for (int j = 0; j < ndiv; ++j) {
float theta = j * delta_theta;
make_sector(mesh, phi, theta, 1.0f, delta_phi, delta_theta);
}
}
}
// Then I can generate the sphere with
build_sphere(sphere_mesh, 10);
但是,除非我将 phi 的迭代从 ndiv 迭代更改为 3 * ndiv 迭代,否则我无法获得完整的球体。我不明白为什么! Phi 应该从 0 到 PI 变化以覆盖整个 Y 轴,而从 0 到 2 * pi 的 Theta 应该覆盖 XZ 平面。
有人可以解释发生了什么以及为什么3 * ndiv 有效吗?
【问题讨论】:
-
你是怎么画整个东西的?我强烈怀疑您只绘制了您生成的所有顶点的 1/3。然后,如果你生成 3 倍的必要顶点,那么这些顶点的 1/3 就足以覆盖整个球体。少了任何东西,零件就丢失了。
-
我将数据加载到顶点缓冲区对象,并使用 glDrawArrays(GL_TRIANGLES, 0, mesh.size() / 2) 绘制它(这是一半,因为正常值最终占据了向量的一半)。我将顶点值与 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0) 和正常值与 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 3 * sizeof (GLfloat));
-
@RetoKoradi 在另一台电脑上重写软件截屏后,我发现了这个bug。问题是我只在顶点缓冲区中加载了三分之一的模型,因为我使用了大小为 model.size() * sizeof(GLfloat) 而不是 model.size() * sizeof(QVector3D) 的 glBufferData。现在它按预期工作,我得到了一个完整的球体。 :)
-
虽然您的代码可能会以某种方式工作,但我认为您会发现实现此博客文章中描述的“icosphere”方法会更满意:blog.andreaskahler.com/2009/06/…。这让我省去了很多麻烦。
标签: c++ opengl math graphics 3d