gpt4 book ai didi

opengl - 在opengl中计算球体

转载 作者:行者123 更新时间:2023-12-02 07:06:39 25 4
gpt4 key购买 nike

我想计算所有需要的顶点并用线连接它们,所以我最终想出了一个球体。有多少种方法可以做到?顶点之间的线也是直的;我怎样才能使它们“弯曲” 我知道我可以使用 glutWireSphere(),但我对实际计算顶点感兴趣。我想到的一种方法是将所有顶点手动放入一个数组中,但我想这不是它的完成方式。

最佳答案

复制并粘贴我最初在 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();
}
}

how can I make them "curved"

你不能。所有 OpenGL 图元都是“仿射”的,即平面的或直线的。通过绘制具有足够分辨率的短而直的部分来模拟曲率。

关于opengl - 在opengl中计算球体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7946770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com