0

I using openGL to draw a concave, but first concave(subject) is mistake. it has 4 points, but display is 3 points, Why?

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

In addition, I have a problem about while(!glfwWindowShouldClose(window)), how to terminate the render loop?

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

is run always. It's mistake.

genpfault
  • 51,148
  • 11
  • 85
  • 139
HYDK
  • 11
  • 6
  • 3
    To draw concave polygons either you have to split the polygon into several convex parts or you have to [use the `GL_STENCIL_TEST` to render concave polygons](https://stackoverflow.com/questions/15284110/use-of-gl-stencil-test-to-render-concave-polygons). – Rabbid76 Apr 19 '18 at 05:31
  • Your second question is already answered on SO [here](https://stackoverflow.com/questions/43804024/window-not-closing-glfw) – YesThatIsMyName Apr 19 '18 at 07:36
  • @Rabbid76 First problem, in my code, clip polygon is still concave, not split, but it can display correct. Why? – HYDK Apr 19 '18 at 09:40
  • 1
    @HYDK There are some concave polygons which can be drawn correctly. It strongly depends on the geometry and the start point. If the polygon can be drawn by a `GL_TRIIANGLE_FAN`, probably it will work. But this is hardware and driver dependent. – Rabbid76 Apr 19 '18 at 12:26
  • @Rabbid76 Thank you! – HYDK Apr 20 '18 at 01:39

1 Answers1

0

To summarize your questions:

  • Either split your polygon into several parts (e.g. by hand or for bigger ones by tessellation) or use the GL_STENCIL_TEST (see comment from Rabbid76 above).
  • Using while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS); should work to exit the window. Code suggestion taken from Window not closing GLFW.
YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
  • Second problem, The statement "printf("%f\t%f\n", coordX, coordY);" always run until close the window, but I want to print the coordX and coordY only once. Whether OpenGL can auto stop the windows when run 1 time only? – HYDK Apr 19 '18 at 09:27
  • First problem, in my code, clip polygon is still concave, not split, but it can display correct. Why? – HYDK Apr 19 '18 at 09:38
  • Also you should ask additional questions as new questions. – YesThatIsMyName Apr 19 '18 at 10:38