gpt4 book ai didi

c++ - 实现opengl球体示例代码

转载 作者:行者123 更新时间:2023-11-30 02:01:27 24 4
gpt4 key购买 nike

尝试实现球体示例代码 I found here on stackoverflow我一直遇到问题。请注意,我没有使用绘制代码,而是使用此示例中的 void SolidSphere 函数来创建我的顶点数据。

这是我的代码,对我引用的示例做了一些小改动:

std::vector<GLfloat> vertices;
std::vector<GLfloat> normals;
std::vector<GLfloat> texcoords;
std::vector<GLushort> indices;

Sphere(shared_ptr<GL::Shader> _shader, shared_ptr<Material> _material, float radius, unsigned int rings, unsigned int sectors)
{
this->attachShader(_shader);
this->attachMaterial(_material);

/** Implementation from:
https://stackoverflow.com/questions/5988686/how-do-i-create-a-3d-sphere-in-opengl-using-visual-c?lq=1
**/

float const R = 1./(float)(rings-1);
float const S = 1./(float)(sectors-1);
int r, s;

vertices.resize(rings * sectors * 3);
//normals.resize(rings * sectors * 3);
texcoords.resize(rings * sectors * 2);
std::vector<GLfloat>::iterator v = vertices.begin();
//std::vector<GLfloat>::iterator n = sphere_normals.begin();
std::vector<GLfloat>::iterator t = 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;
}
}



indices.resize(rings * sectors * 4);
std::vector<GLushort>::iterator i = 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;
}
}


/************* THIS IS THE ADDED PART ********/
for (unsigned int i = 0; i < vertices.size(); i += 3) {
vertexCoords.push_back(glm::vec4(vertices[i], vertices[i+1], vertices[i+2], 1));
}

for (unsigned int i = 0; i < texcoords.size(); i += 2) {
textureCoords.push_back(glm::vec2(texcoords[i], texcoords[i+1]));
}
indexElements = indices;

cout << "Vertex vector size: " << vertexCoords.size() << endl;
cout << "Texture vector size: " << textureCoords.size() << endl;
cout << "Element index vector size: " << indexElements.size() << endl;
}

这里唯一的大变化是使用 M_PI/2 而不是 M_PI_2 和两个将值推送到我的代码使用的格式的循环:

vector <glm::vec4> vertexCoords;
vector <glm::vec2> textureCoords;
vector <GLushort> indexElements;

这是我正在渲染的图片:

enter image description here

所以我在球体顶部看到了最后一行..

我在这段代码中做错了什么会导致这种情况吗?

最佳答案

在这个循环中:

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;
}
}

您正在访问 r+1s+1,当 r = rings - 1s = sectors - 1 (即循环的最后一次迭代)将比您可用的数据高一个。

这将指向一个未初始化的内存区域。这可以包含任何内容,但在这种情况下,它似乎包含零,这可以解释该线是否指向原点。

当您绘制“下一个”四边形时,您只需提前一个迭代停止这些循环:

for(r = 0; r < rings-1; r++) {
for(s = 0; s < sectors-1; s++) {

关于c++ - 实现opengl球体示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080932/

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