0

I am trying to render the letter T, but it looks like its missing one point. When I am rendering a normal octagon, it appears like it should.

Here are the vertices for letter T and a normal octagon (the first three floats are (x,y,z) and the other three are (r,g,b) for color):

    GLfloat octagonVertices[] = {
         0.50f,  0.20f, 0.00f, 0.46f, 0.80f, 0.46f,
         0.50f, -0.20f, 0.00f, 0.46f, 0.80f, 0.46f,
         0.20f, -0.50f, 0.00f, 0.46f, 0.80f, 0.46f,
        -0.20f, -0.50f, 0.00f, 0.46f, 0.80f, 0.46f,
        -0.50f, -0.20f, 0.00f, 0.46f, 0.80f, 0.46f,
        -0.50f,  0.20f, 0.00f, 0.46f, 0.80f, 0.46f,
        -0.20f,  0.50f, 0.00f, 0.46f, 0.80f, 0.46f,
         0.20f,  0.50f, 0.00f, 0.46f, 0.80f, 0.46f
    };

    GLfloat tauVertices[] = {
        -0.01f,  0.45f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.68f,  0.45f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.68f,  0.36f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.39f,  0.36f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.39f, -0.45f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.28f, -0.45f, 0.00f, 1.00f, 0.00f, 0.00f,
         0.28f,  0.36f, 0.00f, 1.00f, 0.00f, 0.00f,
        -0.01f,  0.36f, 0.00f, 1.00f, 0.00f, 0.00f
    };

Here is my vertex buffer:

    glBindVertexArray(vao[1]);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(tauVertices), tauVertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const void*)(3 * 
    sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

And here is how I render my shape:

        glBindVertexArray(vbo[1]);
        glDrawArrays(GL_POLYGON, 0, 8);
leech
  • 367
  • 1
  • 4
  • 16
  • Use Triangles and stitch it. (4 triangles) – Rabbid76 Mar 23 '21 at 16:42
  • 1
    How did you get a data size of `3 * sizeof(tauVertices)`? How is the connectivity between the vertices given? Which type of geometry are you rendering (triangles, triangle-strip, ...)? – BDL Mar 23 '21 at 16:44
  • @Rabbid76 i have to do it with GL_POLYGON, not GL_TRIANGLES – leech Mar 23 '21 at 16:46
  • @leech Why do you have to? There is no version of OpenGL that can draw polygons, but cannot draw triangles. – Rabbid76 Mar 23 '21 at 16:49
  • @BDL i think 3 * was a mistake from a copy + paste, but still it didnt fix my problem. i am using GL_POLYGON – leech Mar 23 '21 at 16:50
  • @leech You can't see the correct answer. Anyway `GL_POLYGON` is deprecated. – Rabbid76 Mar 23 '21 at 16:51

1 Answers1

2

From the docs (emphasis mine):

GL_POLYGON

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

Since the letter T is not convex, you can't draw it as a single polygon.

The results when supplying a non-convex polygon are undefined. It might work one one specific hardware for one specific set of vertices, but in general it is impossible to know in advance what the output will be.

I highly recommend not to use GL_POLYGON rendering anyways: This functionality is not part of modern OpenGL and has been deprecated more than 10 years ago. The modern approach is to draw triangles which cover the polygon surface (e.g., by connecting the vertices correctly with an index buffer).

Also note, that the size of data you upload to the VBO looks wrong. 3 * sizeof(tauVertices) would upload three times as much data as tauVertices contains.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • but when i render greek letter gamma (Γ) as a hexagon, i was fine. Gamma is also an non-convex polygon. I fixed the size of data, too – leech Mar 23 '21 at 16:58
  • @leech: It is undefined what happens when you supply a non-convex polygon. It might work for some configurations on some driver version, it might even produce a different result everytime you run the code. It is impossible to say what exactly will happen. I highly recommend not to use polygon rendering mode anyway: It has been removed from the OpenGL standard more than 10 years ago. – BDL Mar 23 '21 at 17:03
  • so there isnt a way to render a non-convex shape with GL_POLYGON, right? – leech Mar 23 '21 at 17:10
  • @leech: No, there is no way to render a non-convex shape with GL_POLYGON. – BDL Mar 23 '21 at 17:12
  • what if i can split the letter in two convex shape? – leech Mar 23 '21 at 17:13
  • @leech: Sure, why not. But you have to render it then with two separate draw calls. But as Rabbid76 and I already told you: The way you try to use OpenGL is outdated for a decade now. If you have to split the letter anyway, why not split it into triangles? – BDL Mar 23 '21 at 17:16
  • i udnerstand what you say, but i have to do with polygons, because thats how it is asked. Thats the only reason, otherwise i would use triangles – leech Mar 23 '21 at 17:21
  • @leech Note that you can render a T as a triangle fan, if you choose the centre correctly – user253751 Mar 23 '21 at 20:30