简体   繁体   English

如何使用 glut openGL 绘制一个 4 尖星

[英]How to draw a 4 pointed star using glut openGL

I want to draw a 4pointed star using GLUT and openGL in C++.我想在 C++ 中使用 GLUT 和 openGL 绘制一个四角星。 Here is my code这是我的代码

glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0.0f,6.0f,0.0f);
    glVertex3f(1.0f,4.0f,0.0f);
    glVertex3f(3.0f,3.0f,0.0f);
    glVertex3f(1.0f,2.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(-1.0f,2.0f,0.0f);
    glVertex3f(-3.0f,3.0f,0.0f);
    glVertex3f(-1.0f,4.0f,0.0f);

glEnd();

The problem is the shape directly goes to 3,3 from 0,6问题是形状从 0,6 直接变为 3,3

can anyone help me how to fix this, screenshot谁能帮我解决这个问题,截图

I want something like this desired output我想要这样的期望输出

The 1st point of the the GL_TRIANGLE_FAN primitiv is always held fixed (See Triangle primitives ). GL_TRIANGLE_FAN primitiv 的第一个点始终保持固定(参见三角形基元)。 Just start the GL_TRIANGLE_FAN primitiv at one of the "inner" points:只需在“内部”点之一启动GL_TRIANGLE_FAN primitiv:

glBegin(GL_TRIANGLE_FAN);
    
    glVertex3f(1.0f,4.0f,0.0f);
    glVertex3f(3.0f,3.0f,0.0f);
    glVertex3f(1.0f,2.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(-1.0f,2.0f,0.0f);
    glVertex3f(-3.0f,3.0f,0.0f);
    glVertex3f(-1.0f,4.0f,0.0f);

    glVertex3f(0.0f,6.0f,0.0f);

glEnd();

You are creating a triangle fan but setting its central vertex (the initial one) at 0,6,0.您正在创建一个三角形扇形,但将其中心顶点(初始顶点)设置为 0,6,0。

You probably want to change your geometry so that your central vertex is at the origin (for symmetry).您可能想要更改几何形状,以便您的中心顶点位于原点(为了对称)。 It also works to move the first vertex down to the bottom as @Rabbid76 shows.正如@Rabbid76 所示,它还可以将第一个顶点向下移动到底部。

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

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