我想计算所需的所有顶点并用线连接它们,所以我最终想出了一个球体。有多少种方法可以做到?并且顶点之间的线条也是直的;我该如何制作它们?弯曲"我知道我可以使用glutWireSphere(),但我对实际计算顶点感兴趣。我想到的一种方法是将所有顶点手动放在一个数组中,但我想这不是它完成的方式。
答案 0 :(得分:14)
复制并粘贴我最初在Creating a 3D sphere in Opengl using Visual C++
中编写的一些代码class SolidSphere
{
protected
std::vector<GLfloat> vertices;
std::vector<GLfloat> normals;
std::vector<GLfloat> texcoords;
std::vector<GLushort> indices;
public:
void SolidSphere(float radius, unsigned int rings, unsigned int sectors)
{
float const R = 1./(float)(rings-1);
float const S = 1./(float)(sectors-1);
int r, s;
sphere_vertices.resize(rings * sectors * 3);
sphere_normals.resize(rings * sectors * 3);
sphere_texcoords.resize(rings * sectors * 2);
std::vector<GLfloat>::iterator v = sphere_vertices.begin();
std::vector<GLfloat>::iterator n = sphere_normals.begin();
std::vector<GLfloat>::iterator t = sphere_texcoords.begin();
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
float const y = sin( -M_PI_2 + M_PI * r * R );
float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );
float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );
*t++ = s*S;
*t++ = r*R;
*v++ = x * radius;
*v++ = y * radius;
*v++ = z * radius;
*n++ = x;
*n++ = y;
*n++ = z;
}
sphere_indices.resize(rings * sectors * 4);
std:vector<GLushort>::iterator i = sphere_indices.begin();
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
*i++ = r * sectors + s;
*i++ = r * sectors + (s+1);
*i++ = (r+1) * sectors + (s+1);
*i++ = (r+1) * sectors + s;
}
}
void draw(GLfloat x, GLfloat y, GLfloat z)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x,y,z);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]);
glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]);
glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices);
glPopMatrix();
}
}
我怎样才能让它们“弯曲”
你做不到。所有OpenGL基元都是“仿射”,即平面或直线。通过绘制具有足够分辨率的短直线部分来模拟曲率。
答案 1 :(得分:4)
Paul Bourke实际上对sphere generation有一个很好的介绍。至于曲线,OpenGL中没有这样的东西。您只能通过添加更多中间连接点使它们显示为弯曲。
答案 2 :(得分:4)
实现这一目标的方法不止一种:a)icosphere生成和b)UV球体生成。可能有更多方法可以做到这一点。一些谷歌搜索让我this excellent post on icosphere generation。我找不到UV球体生成方法。
答案 3 :(得分:0)
添加三行来绘制代码
void draw(GLfloat x, GLfloat y, GLfloat z)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x,y,z);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]);
glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]);
glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices);
**glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);**
glPopMatrix();
}
答案 4 :(得分:0)
iconosphere可以解决这个问题。 不过,要制作一个球体,你必须细分它的三角形。
答案 5 :(得分:0)
在我看来,关于如何制作 icospheres 的教程很多,但关于使用极坐标的面近似方法的教程并不多。
这里是 Richard S. Wright Jr. 编写的 OpenGL Superbible 第 4 版书籍中稍微编辑过的代码示例。
由于它是固定功能管道的非常简单的用法(没有 glDrawElements 等...),我发现它对教育目的很有用。
堆栈被绘制为一系列三角形条。显然不是最佳性能,但它有效!
// For best results, put this in a display list
// Draw a sphere at the origin
void RenderSphere(const float fRadius, const int iStacks, const int iSlices)
{
const auto PI = (float)M_PI;
const auto PIx2 = (float)(M_PI * 2.0);
GLfloat drho = PI / (GLfloat)iStacks;
GLfloat dtheta = PIx2 / (GLfloat)iSlices;
GLfloat ds = 1.0f / (GLfloat)iSlices;
GLfloat dt = 1.0f / (GLfloat)iStacks;
GLfloat t = 1.0f;
GLfloat s = 0.0f;
for (int i = 0; i < iStacks; i++)
{
const GLfloat rho = (GLfloat)i * drho;
const GLfloat srho = (GLfloat)(std::sinf(rho));
const GLfloat crho = (GLfloat)(std::cosf(rho));
const GLfloat srhodrho = (GLfloat)(std::sinf(rho + drho));
const GLfloat crhodrho = (GLfloat)(std::cosf(rho + drho));
// Many sources of OpenGL sphere drawing code uses a triangle fan
// for the caps of the sphere. This however introduces texturing
// artifacts at the poles on some OpenGL implementations
glBegin(GL_TRIANGLE_STRIP);
s = 0.0f;
for (int j = 0; j <= iSlices; j++)
{
const GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
const GLfloat stheta = (GLfloat)(-std::sinf(theta));
const GLfloat ctheta = (GLfloat)(std::cosf(theta));
GLfloat x = stheta * srho;
GLfloat y = ctheta * srho;
GLfloat z = crho;
glTexCoord2f(s, t);
glNormal3f(x, y, z);
glVertex3f(x * fRadius, y * fRadius, z * fRadius);
x = stheta * srhodrho;
y = ctheta * srhodrho;
z = crhodrho;
glTexCoord2f(s, t - dt);
s += ds;
glNormal3f(x, y, z);
glVertex3f(x * fRadius, y * fRadius, z * fRadius);
}
glEnd();
t -= dt;
}
}
不幸的是,我找不到此源代码的原始在线存储库的链接,它非常古老。如果您知道在哪里可以找到它,请随时发布!