0% found this document useful (0 votes)
10 views3 pages

OpenGL Transformation Code

This document provides a C++ implementation of OpenGL code for applying translation, rotation, and reflection transformations to vertices. It includes the definition of vertices, a composite transformation matrix, and functions for applying transformations and displaying the original and transformed polygons. The code sets up the OpenGL environment and runs a display loop to visualize the transformations.

Uploaded by

Feven Fevita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

OpenGL Transformation Code

This document provides a C++ implementation of OpenGL code for applying translation, rotation, and reflection transformations to vertices. It includes the definition of vertices, a composite transformation matrix, and functions for applying transformations and displaying the original and transformed polygons. The code sets up the OpenGL environment and runs a display loop to visualize the transformations.

Uploaded by

Feven Fevita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

OpenGL Transformation Code

This document contains the C++ implementation of the OpenGL code for applying
translation, rotation, and reflection transformations to a set of vertices.

#include <GL/glut.h>
#include <iostream>
#include <cmath>

#define PI 3.14159265

// Define the vertices


struct Vertex {
float x, y;
};

// Original vertices before transformation


Vertex V[5] = {
{2, 2},
{4, 2},
{5, 4},
{3, 5},
{1, 4}
};

// Composite transformation matrix


float compositeMatrix[3][3] = {
{0.7071, 0.7071, -0.7071},
{-0.7071, 0.7071, 5.6568},
{0, 0, 1}
};

// Function to multiply vertex by composite matrix


Vertex applyTransformation(const Vertex &v) {
Vertex result;
result.x = compositeMatrix[0][0] * v.x + compositeMatrix[0][1] * v.y + compositeMatrix[0]
[2];
result.y = compositeMatrix[1][0] * v.x + compositeMatrix[1][1] * v.y + compositeMatrix[1]
[2];
return result;
}
// Function to display the original and transformed polygons
void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw original polygon


glColor3f(1.0, 0.0, 0.0); // Red color for original shape
glBegin(GL_POLYGON);
for (int i = 0; i < 5; i++) {
glVertex2f(V[i].x, V[i].y);
}
glEnd();

// Draw transformed polygon


glColor3f(0.0, 1.0, 0.0); // Green color for transformed shape
glBegin(GL_POLYGON);
for (int i = 0; i < 5; i++) {
Vertex transformed = applyTransformation(V[i]);
glVertex2f(transformed.x, transformed.y);
}
glEnd();

glFlush();
}

// Set up the OpenGL environment


void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // White background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 10.0, -10.0, 10.0); // Define the coordinate system
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Composite Transformation with OpenGL");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like