3D cylinder with 2D lines in OpenGL

1

How to calculate an array of 3D points that form a 3D cylinder and draw the cylinder using 2D lines (connecting the points of the matrix and forming a mesh of the representation of the 3D object)?

Cylinder::Cylinder(int r, int h, Point *c):Solid(c) {
    this->r = r;
    this->h = h;

    float x, y, z;
    float step = 0.628;
    maxL = 0;
    maxC = 0;

    float auxH = h/2, incH = h/5;

    for (float a = 0.0; a <= 6.28; a +=step) {
        vector<Point*> pointsM;

        for (float t = -auxH; t < auxH; t += incH) {
            x = r * cos(a) + c->getX();
            y = r * sin(a) + c->getY();
            z = t + c->getZ();

            y += (incH / 2);

            Point *p = new Point(x, y, z);
            pointsM.push_back(p);
            if (maxL == 0) {
                maxC++; // o numero de colunas nao se altera na matriz
            }
        }
        matrix.push_back(pointsM);
        maxL++;
    }
}

void Cylinder::draw(Canvas2D *canvas) {
    canvas->color(1, 0, 0);

    for (int l = 0; l < maxL - 1; l++) {
        auto v = matrix.at(l);
        auto v1 = matrix.at(l + 1);

        for (int col = 0; col < maxC - 1; col++) {
            auto p = v.at(col);
            auto p1 = v.at(col + 1);
            auto pp1 = v1.at(col);
            auto pp2 = v1.at(col + 1);

            canvas->line(p->getX2d(), p->getY2d(), p1->getX2d(), p1->getY2d());
            canvas->line(p->getX2d(), p->getY2d(), pp1->getX2d(), pp1->getY2d());
            canvas->line(p->getX2d(), p->getY2d(), pp2->getX2d(), pp2->getY2d());
        }
    }
}
    
asked by anonymous 17.06.2018 / 01:41

0 answers