因此,我尝试使用opengl绘制n点星,并且在绘制具有3点倍数的星时遇到一个问题。

其他任何输出看起来都像这颗十一点星:

九点星的输出为九点星:

下面是我用于生成顶点的代码。真的可以使用帮助。

srand(time(nullptr));
numVertices = 6 * (rand() % 15 + 5);

int numPoints = numVertices / 3;
float r = 0.95;
drawMode = GL_FILL;
vPos = 0;
vColor = 1;
const float M_PI = 3.1415926535897;
float DegToRad;
cout<<numPoints<<endl;
//if(((numPoints/2)%3) !=0)
DegToRad = 2*M_PI / numPoints;
//  else
//    DegToRad = 2*M_PI / (numPoints);

glClearColor(0,0,0,1);
cout<<"Graphics initialized"<<"\n";
VertexData* vertices = (VertexData*)malloc((numVertices+1) * sizeof(VertexData));

for(int i = 0; i < numVertices; i++)
{
    float DegInRad = i * DegToRad;

    if(i % 3 !=0 )
    {
        if(i % 2 == 0)
        {
            cout<<"Drawing triangle point "<<i<<" at "<<r*cos(DegInRad+(M_PI/2))<<","<<r*sin(DegInRad+(M_PI/2))<<endl;
            vertices[i] = {{1.00,0.00,0.00}, {r*cos(DegInRad+(M_PI/2)), r*sin(DegInRad+(M_PI/2))}};
        }else {
            cout<<"Drawing triangle point "<<i<<" at "<<r/3*cos(DegInRad+(M_PI/2))<<","<<r/3*sin(DegInRad+(M_PI/2))<<endl;
            vertices[i] = {{1.00,1.00,1.00}, {r/3*cos(DegInRad+(M_PI/2)), r/3*sin(DegInRad+(M_PI/2))}};
        }

    }else{
        cout<<"Drawing triangle point to origin "<<i<<endl;
        vertices[i] = {{1.00,1.00,1.00}, {0.0, 0.0}};
    }
}

glEnable(GL_BLEND);
glGenVertexArrays(1, &VAO);

glBindVertexArray(VAO);

// Generate and bind (turn on) a buffer (storage location).
glGenBuffers(1, &Buffer);
glBindBuffer(GL_ARRAY_BUFFER, Buffer);

// Transfer the vertex information to the buffer.
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(VertexData), vertices, GL_STATIC_DRAW);

// Setup color data position information. Pointer to data on graphics card itself specifically the color.
//((position tag, number of colors, data-type identifier, to normalize or not, stride the number of bytes to the next color, where to start))
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_TRUE, sizeof(VertexData), BUFFER_OFFSET(0));

// Setup vertex data position information. Never Normalize the position
//vPosition is 0, number of data-types, data-type, to normalize, stride, start (number of bytes to) of the first position data
glVertexAttribPointer(vPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData),BUFFER_OFFSET(sizeof(vertices[0].rgb)));

// Set position indexes for shader streams. Turn on vPosition and vColor.
glEnableVertexAttribArray(vPos);
glEnableVertexAttribArray(vColor);

最佳答案

numVertices = 6 * (rand() % 15 + 5);

您将整数除以三,那么“点”的数量可能不匹配?

为什么生成顶点数?您不应该生成星点数量并从那里计算顶点吗? star 2*(star_points + 1)的唯一顶点数。

为什么会这样呢?星的轮廓包含2*(star_points) + 1,每个叶三个,叶之间共享两个。而且您有中心点。

OpenGL允许以下几种方法:
  • 将每个三角形分别推入顶点缓冲区,如果共享许多顶点,则将使用更多数据:在您的情况下,您将拥有6*star_points顶点;
  • 生成每个顶点一次,然后使用索引缓冲区从它们创建三角形或四边形;
  • 创建一个三角形带或扇形(扇形是不错的选择,您有中心)。

  • 您的 Angular 步数错误,例如对于三点星形三角形,绘制顺序错误:
    Drawing triangle point to origin 0
    Drawing triangle point 1 at -0.866025,0.5
    Drawing triangle point 2 at -2.59808,-1.5
    Drawing triangle point to origin 3
    Drawing triangle point 4 at 2.59808,-1.5
    Drawing triangle point 5 at 0.866025,0.5
    Drawing triangle point to origin 6
    Drawing triangle point 7 at -0.866025,0.5
    Drawing triangle point 8 at -2.59808,-1.5
    Drawing triangle point to origin 9
    Drawing triangle point 10 at 2.59808,-1.5
    Drawing triangle point 11 at 0.866025,0.5
    Drawing triangle point to origin 12
    Drawing triangle point 13 at -0.866025,0.5
    Drawing triangle point 14 at -2.59807,-1.5
    Drawing triangle point to origin 15
    Drawing triangle point 16 at 2.59808,-1.5
    Drawing triangle point 17 at 0.866025,0.500001
    

    这看起来不对
    DegToRad = 2 *M_PI / numPoints;
    

    也许
    DegToRad = M_PI / numPoints;
    

    因为每个三角形都是一个瓣的一半循环本身在设计上也是错误的。它在连续的三角形之间留有间隙,在“从原点绘制”步骤中增加 Angular 。但是接下来它会绕360度旋转并开始填补这些缝隙..或者如果您的端点数是3的倍数,那么不会。

    10-07 17:01