opengl glfw绘制多边形

opengl glfw绘制多边形,opengl,polygon,glfw,concave,Opengl,Polygon,Glfw,Concave,我用openGL画了一个凹面,但第一个凹面(主题)是错误的。 为什么? 它总是在运行。这是错误的。总结一下您的问题: 可以将多边形拆分为几个部分(例如,用手或用手将较大的多边形拆分),也可以使用GL_STENCIL_TEST(参见上面Rabbid76的注释) 使用同时(!GLFWWindowsShouldClose(窗口)和&glfwGetKey(窗口、GLFWU键、退出)!=GLFWU按)应该可以退出窗口。 代码建议取自 要绘制凹面多边形,您必须将多边形拆分为几个凸面部分,或者必须将其拆分。

我用openGL画了一个凹面,但第一个凹面(主题)是错误的。 为什么?


它总是在运行。这是错误的。

总结一下您的问题:

  • 可以将多边形拆分为几个部分(例如,用手或用手将较大的多边形拆分),也可以使用
    GL_STENCIL_TEST
    (参见上面Rabbid76的注释)
  • 使用
    同时(!GLFWWindowsShouldClose(窗口)和&glfwGetKey(窗口、GLFWU键、退出)!=GLFWU按)应该可以退出窗口。
    代码建议取自

要绘制凹面多边形,您必须将多边形拆分为几个凸面部分,或者必须将其拆分。您的第二个问题已经在SO@rabbi76上得到了回答。第一个问题,在我的代码中,剪辑多边形仍然是凹面的,没有拆分,但它可以正确显示。为什么?@HYDK有些凹多边形可以正确绘制。这在很大程度上取决于几何体和起点。如果多边形可以由
GL\u TRIIANGLE\u FAN
绘制,那么它可能会起作用。但这取决于硬件和驱动程序。@Rabbid76谢谢!第二个问题,语句“printf(“%f\t%f\n”,coordX,coordY);”始终运行到关闭窗口,但我只想打印一次coordX和coordY。OpenGL是否只能在运行1次时自动停止窗口?第一个问题,在我的代码中,剪辑多边形仍然是凹面的,没有分割,但它可以正确显示。为什么?你还应该问一些新问题。
#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <GLFW/glfw3.h>


struct Point2D
{
    double m_x;
    double m_y;

    Point2D(int x, int y)
    : m_x(x), m_y(y)
    {

    }
};

void render()
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    std::vector<Point2D> subject;
    subject.push_back(Point2D(0, 100));
    subject.push_back(Point2D(75, 0));
    subject.push_back(Point2D(200, 50));
    subject.push_back(Point2D(100, 50));

    std::vector<Point2D> clip;
    clip.push_back(Point2D(125, 175));
    clip.push_back(Point2D(-15, 50));
    clip.push_back(Point2D(70, 85));
    clip.push_back(Point2D(75, 25));

    std::vector<std::vector<Point2D> > vPolygon;
    vPolygon.push_back(subject);
    vPolygon.push_back(clip);

    //
    int sizei = (int)vPolygon.size();
    for (int i = 0; i < sizei; ++i)
    {
        if (0 != i)
        {
            continue;
        }

        glLineWidth(2.0);
        glBegin(GL_POLYGON);

        auto polygon = vPolygon[i];
        int sizej = (int)polygon.size();
        for (int j = 0; j < sizej; ++j)
        {
            auto coordX = polygon[j].m_x;
            auto coordY = polygon[j].m_y;

            printf("%f\t%f\n", coordX, coordY);

            //glColor3f(k & 0x04, k & 0x02, k & 0x01);
            glVertex2f((float)(coordX - 100.) / 200., (float)(coordY - 120.) / 200.);
        }

        glEnd();
    }
}


int main()
{    
    GLFWwindow* window;

    // Initialize the library
    if(!glfwInit())
    {
        std::cout << "Failed to initialize GLFW!\n";
        return -1;
    }

    // Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(640, 480, "base", NULL, NULL);
    if(!window)
    {
        glfwTerminate();

        std::cout << "Failed to create window using GLFW!\n";
        return -1;
    }

    // Make the window's context current
    glfwMakeContextCurrent(window);

    // init glew
    glewExperimental = GL_TRUE;
    if(glewInit())
    {
        std::cout << "Failed to init GLEW!\n";
        return -1;
    }

    // Loop until the user closes the window
    while(!glfwWindowShouldClose(window))
    {
        // if Esc is pressed, close the window
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        {
            glfwSetWindowShouldClose(window, GLFW_TRUE);
        }

        // render
        render();

        // Swap front and back buffers
        glfwSwapBuffers(window);

        // Poll for and process events
        glfwPollEvents();
    }

    glfwTerminate();

    exit(EXIT_SUCCESS);

    return 0;
}
printf("%f\t%f\n", coordX, coordY);