【问题标题】:opengl glfw draw polygonopengl glfw 绘制多边形
【发布时间】:2018-09-29 10:19:28
【问题描述】:

我用openGL画了一个凹面,但是第一个凹面(主体)是错误的。 it has 4 points, but display is 3 points,为什么?

#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;
}

另外,我还有一个关于while(!glfwWindowShouldClose(window))的问题,如何终止渲染循环?

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

始终运行。错了。

【问题讨论】:

  • 要绘制凹多边形,您必须将多边形分成几个凸部分,或者您必须use the GL_STENCIL_TEST to render concave polygons
  • 你的第二个问题已经在 SO here得到回答
  • @Rabbid76 第一个问题,在我的代码中,剪辑多边形仍然是凹的,没有分割,但它可以正确显示。为什么?
  • @HYDK 有一些凹多边形可以正确绘制。它在很大程度上取决于几何形状和起点。如果可以通过GL_TRIIANGLE_FAN 绘制多边形,那么它可能会起作用。但这取决于硬件和驱动程序。
  • @Rabbid76 谢谢!

标签: opengl polygon glfw concave


【解决方案1】:

总结您的问题:

  • 要么将你的多边形分割成几个部分(例如手动分割,要么使用 tessellation 分割更大的部分)或使用 GL_STENCIL_TEST(参见上面 Rabbid76 的评论)。
  • 使用while(!glfwWindowShouldClose(window) &amp;&amp; glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS); 应该可以退出窗口。 代码建议取自 Window not closing GLFW

【讨论】:

  • 第二个问题,语句"printf("%f\t%f\n", coordX, coordY);"总是运行直到关闭窗口,但我只想打印一次 coordX 和 coordY。 OpenGL是否可以在运行1次时自动停止窗口?
  • 第一个问题,在我的代码中,裁剪多边形仍然是凹的,没有分割,但是可以正确显示。为什么?
  • 此外,您还应该提出其他问题作为新问题。
猜你喜欢
  • 2017-06-17
  • 2023-03-27
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2011-04-21
  • 2023-03-14
相关资源
最近更新 更多