简体   繁体   English

绘制n点星形的opengl C ++

[英]Drawing n-pointed star opengl c++

So I am trying to draw a n-pointed star using opengl and I'm running across a problem when drawing stars that have multiple of 3 points. 因此,我尝试使用opengl绘制n点星,并且在绘制具有3点倍数的星时遇到一个问题。

The output for any other looks something like this eleven point star: 其他任何输出看起来都像这颗十一点星:

img

The output for something like a nine point star is nine point star: 九点星的输出为九点星:

img

The code I'm using to generate vertices is below. 下面是我用于生成顶点的代码。 could really use the help. 真的可以使用帮助。

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

You divide that in integer by three, so number of "points" may not match? 您将整数除以三,那么“点”的数量可能不匹配?

why you generate number of vertices? 为什么生成顶点数? Shouldn't you generate number of star points and calculate vertices from there?. 您不应该生成星点数量并从那里计算顶点吗? Number of unique vertices for star 2*(star_points + 1) . 星形2*(star_points + 1)唯一顶点数。

Why it so? 为什么会这样呢? Outline of star contains 2*(star_points) + 1 , three per each lobe, two of each shared between lobes. 星形轮廓包含2*(star_points) + 1 ,每个叶三个,在叶之间共享两个。 And you have center point. 而且您有中心点。

OpenGL allows several approaches: OpenGL允许以下几种方法:

  1. push every triangle separately into vertex buffer, you'll use more data if a lot of vertices are shared: in your case you'll have 6*star_points vertices; 将每个三角形分别推入顶点缓冲区,如果共享许多顶点,则将使用更多数据:在您的情况下,您将拥有6*star_points顶点;
  2. generate every vertex once, then use index buffer to create triangles or quads from them; 生成每个顶点一次,然后使用索引缓冲区从它们创建三角形或四边形;
  3. create a triangle strip or fan (fan is good choice here, you have the center). 创建一个三角形条或扇形(扇形是不错的选择,您有中心)。

You have wrong angle step, eg for 3-point star triangles are drawn in wrong order: 您的角度步长错误,例如,对于三点星形三角形,绘制顺序错误:

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

This doesn't look right 这看起来不对

DegToRad = 2 *M_PI / numPoints;

perhaps 也许

DegToRad = M_PI / numPoints;

because each triangle is half of a lobe. 因为每个三角形都是一个瓣的一半 The loop itself is wrong too by design. 循环本身在设计上也是错误的。 it leaves gaps between consecutive triangles, it increments angle during "drawing to origin" step. 它在连续的三角形之间留有间隙,在“从原点绘制”步骤中增加角度。 but next it would circle around the 360 degrees and begin fill those gaps..or not, if you have number of ends as a multiple of 3. 但是接下来,它会绕360度旋转并开始填补这些缝隙..或者如果您的端点数是3的倍数,则不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM