C++ 球体计算

C++ 球体计算,c++,opengl,C++,Opengl,因此,我使用此算法创建了所有正确的球体顶点: 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,

因此,我使用此算法创建了所有正确的球体顶点:

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

}
编辑1:

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

不能通过
gldrawArray
直接绘制索引。你必须把它们按正确的顺序带过来。 创建三角形索引列表并使用

另见


进一步注意,您的循环应该从
i=0
运行到
i,因为您显示的代码不够,可能是错误的部分。我们只看到顶点坐标的计算。它们显然是正确的,因为它们形成了一个球体。要渲染球体,需要在组中使用这些顶点。每个vertice被使用的次数不止一次,对于它所属的每个多边形(可能是三角形)使用一次。此外,“被提及”的顺序也非常重要。实际渲染的多边形可能更多,但朝向错误的方向。我在问题中添加了draw调用,并包括了如何将数据解析到glvertexattributepointer中。我的问题是,“我的数学错了吗?或者我没有以正确的方式将数据绘制到数组中,以使我的glvertexattributepointer函数正确读取数据吗?”找到一个使用相同API的示例,并将其与代码进行比较。检查顶点的顺序以及每次出现的次数。考虑简化到其他几何结构,然后向一个具有许多垂直方向的球体延伸。谢谢你的帮助,现在变得更有意义了,因为我对所有顶点的计算都是在整个位置上的,而且不会有三角形产生的顺序,导致我得到的奇异结果。谢谢你帮我看我应该做什么。
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glDrawArrays(GL_TRIANGLES, 0, 10000);
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
    }
}
// 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 );