在opengl中未读取顶点[重复]

rggaifut  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(166)

此问题在此处已有答案

OpenGL GL_POLYGON concave polygon doesn't color in(1个答案)
6个月前关闭。
我有一个问题,当opengl无法读取顶点的某个部分时,我该怎么办?我的意思是,当我运行代码时,它没有遵循我在坐标纸上所做的坐标网格,即使我删除了那个顶点,它仍然显示相同的输出。我想做枫叶。
我想通过降低某个顶点的y值,用直线画出一条曲线,但是当我运行代码时,它没有画出我已经设定好的坐标,而是跳过了我设定的那个顶点,转到了另一个顶点,画出了一条直线。
假设我对顶点A设(-0.44,0.16)B(-0.46,0.16)C(-0.52,0.18)D(-0.54,0.24),它不是从A到B再到C,而是跳过B和C,去了D。


# include <windows.h>

# include <gl/glut.h>

void display() {

    glClearColor(0.3, 0.1, 0.1, 0.2);
    glClear(GL_COLOR_BUFFER_BIT);

    //glTranslatef(0.5, 0, 0);
    //glScalef(0.5, 0.5, 0);
    //glRotatef(0, 0, 0, 0);

    //Unifolié also known as Maple Leaf
    glBegin(GL_POLYGON);
    glColor3f(1, 0, 0);
    glVertex2f(0.00, 0.00);
    glVertex2f(0.00, 0.90);
    glVertex2f(-0.08, 0.70);
    glVertex2f(-0.12, 0.66);
    glVertex2f(-0.16, 0.66);
    glVertex2f(-0.20, 0.68);
    glVertex2f(-0.24, 0.74);
    glVertex2f(-0.22, 0.64);
    glVertex2f(-0.22, 0.62);
    glVertex2f(-0.24, 0.56);
    glVertex2f(-0.26, 0.54);
    glVertex2f(-0.30, 0.54);
    glVertex2f(-0.34, 0.56);
    glVertex2f(-0.36, 0.60);
    glVertex2f(-0.36, 0.56);
    glVertex2f(-0.38, 0.54);
    glVertex2f(-0.40, 0.54);
    glVertex2f(-0.42, 0.54);
    glVertex2f(-0.56, 0.60);
    glVertex2f(-0.52, 0.54);
    glVertex2f(-0.50, 0.50);
    glVertex2f(-0.48, 0.44);
    glVertex2f(-0.48, 0.42);
    glVertex2f(-0.50, 0.38);
    glVertex2f(-0.54, 0.34);
    glVertex2f(-0.48, 0.32);
    glVertex2f(-0.44, 0.28);
    glVertex2f(-0.42, 0.22);
    glVertex2f(-0.42, 0.20);
    glVertex2f(-0.44, 0.16);
    glVertex2f(-0.46, 0.16); //this 1st
    glVertex2f(-0.52, 0.18);
    glVertex2f(-0.54, 0.24);
    glVertex2f(-0.54, 0.20);
    glVertex2f(-0.56, 0.16);
    glVertex2f(-0.60, 0.14);
    glVertex2f(-0.62, 0.14);
    glVertex2f(-0.70, 0.16);
    glVertex2f(-0.76, 0.20);
    glVertex2f(-0.78, 0.24);
    glVertex2f(-0.76, 0.18);
    glVertex2f(-0.74, 0.14);
    glVertex2f(-0.70, 0.08);
    glVertex2f(-0.68, 0.06);
    glVertex2f(-0.66, 0.02);
    glVertex2f(-0.66, 0.00);
    glVertex2f(-0.68, -0.04);
    glVertex2f(-0.64, -0.02);
    glVertex2f(-0.62, -0.02);
    glVertex2f(-0.56, -0.04);
    glVertex2f(-0.52, -0.08);
    glVertex2f(-0.48, -0.16);
    glVertex2f(-0.48, -0.20);
    glVertex2f(-0.50, -0.26);
    glVertex2f(-0.52, -0.30);
    glVertex2f(-0.58, -0.34);
    glVertex2f(-0.42, -0.32);
    glVertex2f(-0.30, -0.28);
    glVertex2f(-0.18, -0.22);
    glVertex2f(-0.10, -0.20);
    glVertex2f(0.00, -0.22);

    glVertex2f(0.10, -0.20);
    glVertex2f(0.18, -0.22);
    glVertex2f(0.30, -0.28);
    glVertex2f(0.42, -0.32);
    glVertex2f(0.58, -0.34);
    glVertex2f(0.52, -0.30);
    glVertex2f(0.50, -0.26);
    glVertex2f(0.48, -0.20);
    glVertex2f(0.48, -0.16);
    glVertex2f(0.52, -0.08);
    glVertex2f(0.56, -0.04);
    glVertex2f(0.62, -0.02);
    glVertex2f(0.64, -0.02);
    glVertex2f(0.68, -0.04);
    glVertex2f(0.66, 0.00);
    glVertex2f(0.66, 0.02);
    glVertex2f(0.68, 0.06);
    glVertex2f(0.70, 0.08);
    glVertex2f(0.74, 0.14);
    glVertex2f(0.76, 0.18);
    glVertex2f(0.78, 0.24);
    glVertex2f(0.76, 0.20);
    glVertex2f(0.70, 0.16);
    glVertex2f(0.62, 0.14);
    glVertex2f(0.60, 0.14);
    glVertex2f(0.56, 0.16);
    glVertex2f(0.54, 0.20);
    glVertex2f(0.54, 0.24);
    glVertex2f(0.52, 0.18);
    glVertex2f(0.46, 0.16);
    glVertex2f(0.44, 0.16);
    glVertex2f(0.42, 0.20);
    glVertex2f(0.42, 0.22);
    glVertex2f(0.44, 0.28);
    glVertex2f(0.48, 0.32);
    glVertex2f(0.54, 0.34);
    glVertex2f(0.50, 0.38);
    glVertex2f(0.48, 0.42);
    glVertex2f(0.48, 0.44);
    glVertex2f(0.50, 0.50);
    glVertex2f(0.52, 0.54);
    glVertex2f(0.56, 0.60);
    glVertex2f(0.42, 0.54);
    glVertex2f(0.40, 0.54);
    glVertex2f(0.38, 0.54);
    glVertex2f(0.36, 0.56);
    glVertex2f(0.36, 0.60);
    glVertex2f(0.34, 0.56);
    glVertex2f(0.30, 0.54);
    glVertex2f(0.26, 0.54);
    glVertex2f(0.24, 0.56);
    glVertex2f(0.22, 0.62);
    glVertex2f(0.22, 0.64);
    glVertex2f(0.24, 0.74);
    glVertex2f(0.20, 0.68);
    glVertex2f(0.16, 0.66);
    glVertex2f(0.12, 0.66);
    glVertex2f(0.08, 0.70);
    glVertex2f(0.00, 0.90);
    glEnd();

    glFlush();

}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(10, 10);
    glutCreateWindow("Maple Leaf");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
j9per5c4

j9per5c41#

这是您的多边形:

你的形状不是凸的,GL_POLYGON规定:
GL_POLYGON
绘制一个单一的多边形。顶点1到N定义这个多边形。
要栅格化此轮廓,请手动将形状拆分为三角形或其他基本组件,如三角扇或三角条。这称为 * 镶嵌 *。虽然GL_POLYGON看起来很诱人,但它仅适用于凸形。

对于任意多边形,你需要自己镶嵌轮廓。你可以找到一个库来完成这项工作,例如:https://github.com/mapbox/earcut.hpp好消息是你只需要做一次,而且你可以离线做(不是在运行时)。一旦你的多边形被镶嵌,你就可以用GL_TRIANGLES来绘制它了。

相关问题