绘制n-point star opengl c ++

时间:2018-02-16 02:52:50

标签: c++ opengl

所以我试图用opengl绘制一个n-pointed星,当我绘制有3个点的多个星时,我遇到了一个问题。

任何其他人的输出看起来像这个十一点星:

img

九点星的输出是九点星:

img

我用来生成顶点的代码如下。真的可以使用帮助。

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

1 个答案:

答案 0 :(得分:0)

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

你将整数除以3,所以&#34;指数&#34;可能不匹配?

为什么要生成多个顶点?你不应该生成多个星点并从那里计算顶点吗?星号2*(star_points + 1)唯一顶点数。

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

OpenGL允许多种方法:

  1. 将每个三角形分别推入顶点缓冲区,如果共享了很多顶点,您将使用更多数据:在您的情况下,您将拥有6*star_points个顶点;
  2. 生成每个顶点一次,然后使用索引缓冲区从它们创建三角形或四边形;
  3. 创建一个三角形条或扇形(风扇在这里是个不错的选择,你有中心)。
  4. 你有错误的角度步长,例如对于三点星三角形的绘制顺序错误:

    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;
    

    因为每个三角形都是一个叶片的一半。循环本身也是错误的设计。它在连续的三角形之间留下间隙,它在&#34;绘制到原点&#34;期间增加角度。步。但是接下来它会围绕360度旋转并开始填补这些空隙..或者,如果你的结尾数量是3的倍数。