Untitled

Anonymous
plain_text
02/24/2026 9:16 AM
2.9 KB
15
Indexable
#include <GL/glut.h>

// Function to draw the triangle
void drawTriangle() {
    glBegin(GL_TRIANGLES);
        glVertex2f(-0.3, -0.3);
        glVertex2f(0.0, 0.3);
        glVertex2f(0.3, -0.3);
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    //  Original Triangle (Blue)
    glColor3f(0.0, 0.0, 1.0);
    drawTriangle();

    // Translated Triangle (Red) 
    glPushMatrix();
    glTranslatef(0.6, 0.0, 0.0);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_TRIANGLES);
    glVertex2f(-0.3, -0.3);
    glVertex2f(0.0, 0.3);
    glVertex2f(0.3, -0.3);
    glEnd();
    glPopMatrix();

    // Rotated Triangle (Green) 
    glPushMatrix();
    glRotatef(45.0, 0.0, 0.0, 1.0);
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_TRIANGLES);
    glVertex2f(-0.3, -0.3);
    glVertex2f(0.0, 0.3);
    glVertex2f(0.3, -0.3);
    glEnd();
    glPopMatrix();
    glFlush();

    //  Scaled Triangle (Red)
    glPushMatrix();
        glTranslatef(-0.6, 0.5, 0.0);
        glScalef(1.5, 0.5, 1.0);   // Scale X by 1.5 and Y by 0.5
        glColor3f(1.0, 0.0, 0.0);
        drawTriangle();
    glPopMatrix();

    //  Reflection (Green) - Reflect over X-axis
    glPushMatrix();
        glTranslatef(0.6, 0.5, 0.0);
        glScalef(1.0, -1.0, 1.0);  // Reflect over X-axis
        glColor3f(0.0, 1.0, 0.0);
        drawTriangle();
    glPopMatrix();

    //  Shearing (Yellow)
    glPushMatrix();
        glTranslatef(0.0, -0.6, 0.0);
        GLfloat shearMatrix[] = {
            1.0, 0.5, 0.0, 0.0,   // Shear X by 0.5
            0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 1.0
        };
        glMultMatrixf(shearMatrix);
        glColor3f(1.0, 1.0, 0.0);
        drawTriangle();
    glPopMatrix();

    glFlush();
}

void init() {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-1, 1, -1, 1);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Scaling, Reflection and Shearing Example");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
Editor is loading...
Leave a Comment