Search code examples
openglpolygonpyopengltessellation

OpenGL - Draw non planar 3D polygon consisted of a set of points


I have a set of points that form a shape (specifically half a pipe, other similar shapes). I need a way to draw this Cylindrical face.

For example:

enter image description here

I want to draw this half-pipe, and I have the marked points ordered (drawn as red circles).

Having an approximation of Cylinder (with linear edges, and not exactly circular) is Fine.

My shapes are not always half pipes, or cylindrical, but are always closed polygons, where I have a set of points forming it.

(I'm using PyOpenGL, but it doesn't really matter)


Solution

  • You should learn how models are rendered in 3D, after that, you will be able to make anything you want.

    Each model consists of triangles, one triangle is 3 arbitrary vertices in space.

    You might want to download and launch a modelling program such as blender, and try to build what you want to build before attempting this, because you'll either have to build this by hand, or to write an algorithm that does this.

    Then go to wireframe view, or point view, and you'll see what you actually built. Just don't forget to properly triangulate first (blender can do it automatically if you for some reason used quads).

    On top of all that, you want to use modern openGL, which means you have to learn the pipeline...

    Well, there's no way to avoid this, you must read and learn: https://learnopengl.com/

    But I can sum it up for you:

    1. Get model data - in this case, if you want it to look like what you showed us, vertices and triangles will be enough;
    2. Load the vertex data and bind it to VAO, and also bind your triangles (indices, the numbers which specify vertex ordering in the VAO data) to EBO. Meanings are here.
    3. Now, you build matrices, such as projection matrix, which will be used to properly transform the vertices so it looks like a proper 3D object, is rotated however you want and so on;
    4. You render the EBO data using glDrawElements();

    Yes, this isn't supposed to be clear, but you will learn how to do it as you go along this tutorial.

    As for how to get data of actual half pipe... You need to take circle formula, pick an arc you want, say, PI * 2 / 3, or in other words, third of the circle, divide this arc into N parts, and then in every segment, you create a plane facing outwards from the circle, the plane is 2 triangles each (make 2 triangles that form a rectangle visually), in the end, you'll have your desired result.

    To really figure out how to do this, you need to learn how to build meshes by hand, then take a piece of paper, and do the math. Additionally, you can build entire thing by hand, it's just numbers, representing points in space, connected by lines, which enclose planes. Every single plane is a triangle.