0

I have a question, what should I do when some part of the vertex not read by opengl? I mean when I run the code, it didn't follows my coordinate grid which i did on graph paper and even after I delete that vertex, it still showed the same output. I wanted to make maple leaf.

I want to make a some sort of curve using straight line by lower the y value for that specific vertex but when I run the code, instead of making the line to the coordinate I already set, it skipped that vertex I set and went to the other vertex and create a straight line.

Let say, I set for vertex A(-0.44, 0.16) B(-0.46, 0.16) C(-0.52, 0.18) D(-0.54, 0.24), instead of going from A to B to C, it skipped B and C and went for 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;
}
stealth
  • 15
  • 5
  • Not sure what the problem is, but your leaf has symmetry and has the left-side vertices in counter-clockwise order and the right-side vertices in clockwise order. The whole thing should be a single polygon in one winding order. Therefore you may need to reverse the order of the vertices that make up the right side of your leaf. If you imagine drawing a circle with vertices as numbers on an analog clock, it's as if you drew a circle by visiting vertices in this order: ( 12, 11, 10, 9, 8, 7, 6, 12, 1, 2, 3, 4, 5, 6 }. That creates a "bow-tie" error. – Wyck May 12 '22 at 04:58
  • oh yeah true2 i should've just follow from the last vertex on the left side and continue untll the top vertex right? – stealth May 12 '22 at 05:04
  • Yeah, I think you did a copy-paste of the left side vertices and negated the x coordinates manually. Your time-saving short-cut was a little too short. You still need to craft a coherent shape with consistent winding order and continuity between the halves. (reversing the order of the vertices on the right side would do the trick, but be cautious about the vertical line from (0.0, 0.0) to (0.0, 0.9) -- it's not necessary if the leaf is a closed shape.) – Wyck May 12 '22 at 05:09
  • i already try the other way around which is finishing the full polygon rather than making 2 poligon which symmetry which each other but the logic error still occur – stealth May 12 '22 at 05:10
  • What is the "logic error" what do you mean by that? "this 1st" means nothing to me. It doesn't describe what you expected vs what actually happened, or if you are getting some kind of error message. Please make sure all the details are in the question itself so that someone can craft an appropriate objectively correct answer. – Wyck May 12 '22 at 05:10
  • i want to make a some sort of curve using straight line by lower the y value for that specific vertex but when i run the code, instead of making the line to the coordinate i already set, it skipped that vertex i set and went to the other vertex and create a straight line. I'm soo sorry my english is not that good, its kinda hard for me to explain the situation. – stealth May 12 '22 at 05:17
  • .Let say, i set for vertex A(-0.44, 0.16) B(-0.46, 0.16) C(-0.52, 0.18) D(-0.54, 0.24), instead of going from A to B, it skipped B and C and went for D. – stealth May 12 '22 at 05:19
  • Ah I get it now. You are not tessellating your shape into triangles and **your shape is not convex** so default tesselation will do something unexpected (to you) in the concave parts. – Wyck May 12 '22 at 05:24
  • is it possible to tessellating maple leaf? I'm trying to make maple leaf but since this error occur I don't exactly know what that to do. I might just create another leaf which much easier than maple's. Anyway, thank you soo much for the explanation. I might need to make more research about tessellating and concave/convex. Thank you so much. – stealth May 12 '22 at 05:40
  • Maybe, you could use a [`GL_TRIANGLE_FAN`](https://en.wikipedia.org/wiki/Triangle_fan) instead of a `GL_POLYGON`, and introduce a center point in the middle of your leaf. In this case, even a maple leaf might be rendered correctly. – Scheff's Cat May 12 '22 at 05:49
  • @Scheff'sCat I plotted the data myself manually and there's no central point from which to construct a fan -- so tessellation is needed. – Wyck May 12 '22 at 05:56
  • @Wyck _and **introduce** a center point._ ;-) – Scheff's Cat May 12 '22 at 05:57
  • @Scheff'sCat Did you look at the data? Not every shape *has* such a possible point. It's not just a matter of a missing vertex. The topology of a shape may be that no such point is possible (imagine a donut or even a letter C). And indeed this so-called _Unifolié_ is such a shape because of its curved leaf tips. – Wyck May 12 '22 at 06:02
  • @Wyck I must admit I didn't look at the data. Seeing the shape in your answer now, I must admit a `GL_TRIANGLE_FAN` wouldn't cure it. Tessellating it manually is probably not that difficult. Making it by an algorithm seems more challenging. (I believe, I once made such an algorithm...) – Scheff's Cat May 12 '22 at 08:04
  • @Scheff'sCat, True. Although I wouldn't write the algorithm from scratch. I suggested using [_earcut_](https://github.com/mapbox/earcut.hpp) library or any such equivalent. TBH I would have drawn the contour in Blender and exported a mesh file, letting it do the tessellation for me. – Wyck May 12 '22 at 13:23

1 Answers1

4

This is your polygon:

enter image description here

Your shape is not convex and GL_POLYGON stipulates:

GL_POLYGON

Draws a single, convex polygon. Vertices 1 through N define this polygon.

To rasterize this contour, manually break your shape into triangles, or other primitive components such as a TRIANGLE FAN or TRIANGLE STRIP. This is called tessellation. Although GL_POLYGON looks tempting, it is for convex shapes only.

primitives

(original image source)

For arbitrary polygons, you'll need to tessellate the contour yourself. You can seek out a library for doing this such as: https://github.com/mapbox/earcut.hpp The good news is that you only have to do this once, and you can do it offline (not at run-time). Once your polygon is tessellated, you'll be able draw it with GL_TRIANGLES.

Wyck
  • 10,311
  • 6
  • 39
  • 60