Opengl triangulation c++

56 Views Asked by At

the essence of the task is that triangles identical to a given triangle are built from the vertices of a given triangle in a staggered order, it turns out that they are built only inside the main one and are reduced

I tried to change the output via maas and drawchess, the triangles turned out to be too large and went beyond the screes:

#include <GL/glut.h>

int pointCount = 0;
float points[3][2];

void drawPoint() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_TRIANGLES);
    for (int i = 0; i < pointCount; i++) {
        glVertex2f(points[i][0], points[i][1]);
    }
    glEnd();

    glFlush();
}

void drawChess(float x1, float y1, float x2, float y2, float x3, float y3, int depth) {
    if (depth == 0) {
        glColor3f(0.0, 1.0, 1.0);
        glBegin(GL_TRIANGLES);
        glVertex2f(x1, y1);
        glVertex2f(x3, y3);  
        glVertex2f(x2, y2);
        glEnd();
        return;
    }

    float midx1 = (x1 + x2) / 2;
    float midy1 = (y1 + y2) / 2;
    float midx2 = (x2 + x3) / 2;
    float midy2 = (y2 + y3) / 2;
    float midx3 = (x3 + x1) / 2;
    float midy3 = (y3 + y1) / 2;

    drawChess(x1, y1, midx1, midy1, midx3, midy3, depth - 1);
    drawChess(midx1, midy1, x2, y2, midx2, midy2, depth - 1);
    drawChess(midx3, midy3, midx2, midy2, x3, y3, depth - 1);
}

void mouse(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        float xPos = (float)x / glutGet(GLUT_WINDOW_WIDTH) * 2 - 1;
        float yPos = -(float)y / glutGet(GLUT_WINDOW_HEIGHT) * 2 + 1;

        points[pointCount][0] = xPos;
        points[pointCount][1] = yPos;

        pointCount++;

        if (pointCount == 3) {
            glClear(GL_COLOR_BUFFER_BIT);
            glColor3f(1.0, 1.0, 1.0);

            glBegin(GL_TRIANGLES);
            for (int i = 0; i < pointCount; i++) {
                glVertex2f(points[i][0], points[i][1]);
            }
            glEnd();

            drawChess(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], 5);

            glFlush();
        }
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutCreateWindow("treugolnik");

    glClearColor(0.5, 0.5, 0.5, 0.5);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

    glutDisplayFunc(drawPoint);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;
}
0

There are 0 best solutions below