球体计算[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了球体计算[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

所以我使用这个算法创建了所有正确的球体顶点:

GLint total = 100;
GLfloat radius = 200;
GLfloat sphereVertices[30000];
for (int i = 0; i < total; i++)
{
    float lon = map(i, 0, total, -M_PI, M_PI);
    for (int j = 0; j < total; j++)
    {
        float lat = map(j, 0, total, -M_PI/2, M_PI/2);

        sphereVertices[(i * 300) + (j * 3)] = radius * sin(lon) * cos(lat);
        sphereVertices[(i * 300) + (j * 3) + 1] = radius * sin(lon) * sin(lat);
        sphereVertices[(i * 300) + (j * 3) + 2] = radius * cos(lon);
    }

}

但是当我使用GL_TRIANGLES和GL_TRIANGLE_STRIP绘制它时,我生成了这个结果:enter image description here

正如您所看到的,正在渲染的唯一三角形正在切割球体的中心。我的数学错了吗?或者我没有将我的数据绘制到我的数组中正确的方式让我的glVertexAttribPointer函数正确读取数据?

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);

编辑1:

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glDrawArrays(GL_TRIANGLES, 0, 10000);
答案

你无法直接通过glDrawArrays绘制这些内容。你必须按正确的顺序带他们。创建三角形索引列表并使用glDrawElements

另见How to map texture to sphere that rendered by parametric equation using points primitive

还要注意,你的循环应该从i = 0运行到i <= total,因为你想在球体的两个极点上创建顶点坐标。

GLint layers             = 100;
GLint circumferenceTiles = 100;
std::vector<GLfloat> sphereVertices;
va.reserve( (layers+1)*(circumferenceTiles+1)*3 );  // 3 floats: x, y, z 
for ( int i = 0; i <= layers; ++ i )
{
    GLfloat lon     = map(i, 0, layers, -M_PI, M_PI);
    GLfloat lon_sin = std::sin( lon );
    GLfloat lon_cos = std::cos( lon );
    for ( int j = 0; j <= circumferenceTiles; j ++ )
    {
        GLfloat lat     = map(j, 0, circumferenceTiles, -M_PI/2, M_PI/2);
        GLfloat lat_sin = std::sin( lat);
        GLfloat lat_cos = std::cos( lat);

        va.push_back( lon_cos * lat_cos ); // x
        va.push_back( lon_cos * lat_sin ); // y
        va.push_back( lon_sin );           // z
    }
}

您可以通过堆叠光盘来创建三角形:

enter image description here

// create the face indices 
std::vector<GLuint> ia;
ia.reserve( layers*circumferenceTiles*6 );
for ( GLuint il = 0; il < layers; ++ il )
{
    for ( GLuint ic = 0; ic < circumferenceTiles; ic ++ )
    {
      GLuint i0 = il * (circumferenceTiles+1) + ic;
      GLuint i1 = i0 + 1;
      GLuint i3 = i0 + circumferenceTiles+1;
      GLuint i2 = i3 + 1;

      int faces[]{ i0, i1, i2, i0, i2, i3 };
      ia.insert(ia.end(), faces+(il==0?3:0), faces+(il==layers-1?3:6));
    }
}

指定顶点数组对象:

GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sphereVertices.size()*sizeof(GLfloat), sphereVertices.data(), 
GL_STATIC_DRAW );

GLuint ibo;
glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ia.size()*sizeof(GLuint), ia.data(), GL_STATIC_DRAW );

GLuint v_attr_inx = 0;
glVertexAttribPointer( v_attr_inx , 3, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray( v_attr_inx );

glBindVertexArray( 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

绘制球体:

glBindVertexArray( vao );
glDrawElements( GL_TRIANGLES, (GLsizei)ia.size(), GL_UNSIGNED_INT, 0 );
glBindVertexArray( 0 );

以上是关于球体计算[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在绝对居中的球体中垂直居中文本[重复]

片段着色器中的球面映射

OpenGL拾取 - 光线/球体相交错误

计算球体贴片的边界球体

用JAVA编写一个计算立方体、球体和圆柱的体积

SceneKit 球体法线似乎随着相机移动而改变?