Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ComputerGraphics LabFile (500105543)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Computer Graphics Lab

Report

Autumn 2024

Name-:Pratiksha Bahuguna
SAP ID-:500105543
Course-:B.Tech(C.S.E.),DevOps
Roll No.-:R2142220134

Program, Semester, Batch: B. Tech CSE DevOps-V-


B1
School of Computer Science,
University of Petroleum and Energy Studies,
Dehradun
S. No Experiment Date Pag Remark
e. s
No
1 Intro to OpenGL and installation 5-08-2024 3

2 Line Drawing using DDA 2-09-2024 6

3 Mid-Point Circle 9

4 Ellipse 11
Experiment 1

Introduction to OpenGL

1. Commands to install OpenGl are:

● $ sudo apt-get update

● $ sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev

2. Once these commands are executed, a cpp file will be made to display

black window using OpenGl.

Code
#include
<GL/glut.h>
#include <GL/gl.h>
#include
<GL/glu.h>

int SCREEN_WIDTH =
640; int SCREEN_HEIGHT
= 480;

void draw(void)
{
// glClearColor(0, 0, 0, 1);
// glClearColor(1.0f, 0.0f, 1.0f, 1.0f); // Magenta
glClearColor(0.0f, 0.5f, 0.0f, 1.0f); // Dark Green

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0.0, (GLdouble)SCREEN_WIDTH, 0.0,

(GLdouble)SCREEN_HEIGHT); glBegin(GL_POINTS);
glVertex2f(50, 50); // This is now correct
/////////////////////////////////////////////////
////// glEnd();
glFlush();
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(SCREEN_WIDTH,
SCREEN_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("First OpenGL Program");
glutDisplayFunc(draw);
glutMainLoo
p(); return
0;

Output
Experiment 2

Line Drawing using DDA

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

// Function to plot a point


void plotPoint(int x, int y)
{
glBegin(GL_POINTS)
; glVertex2i(x,
y); glEnd();
}

// DDA Line Drawing Algorithm


void drawLineDDA(int x1, int y1, int x2,
int y2) { int dx = x2 - x1;
int dy = y2 - y1;

int steps = abs(dx) > abs(dy) ? abs(dx) :

abs(dy); float xIncrement = dx /

(float)steps;
float yIncrement = dy / (float)steps;

float x =
x1; float y
= y1;

for (int i = 0; i <=


steps; i++) {
plotPoint(round(x),
round(y)); x += xIncrement;
y += yIncrement;
}
}

// OpenGL display
callback void display()
{
glClear(GL_COLOR_BUFFER_BIT);
// Setup
OpenGL void
initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background
color: white glColor3f(0.0, 0.0, 0.0); // Draw color:
black glPointSize(2.0);
gluOrtho2D(0, 500, 0, 500); // Set the viewport
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB); glutInitWindowSize(500,
500);
glutInitWindowPosition(100,
100); glutCreateWindow("DDA
Line Drawing"); initOpenGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output
Experiment 3

Mid-Point

Circle

Code
#include
<GL/glut.h>
#include

void circle() {
glColor3f(1.0, // Set the color
0.0, 0.0); to red
glPointSize(2.0); // Set point size
float r =
100; float x = // Midpoint algorithm decision

glBegin(GL_POINTS);
while (x <= y) // Loop until x equals y
(one octant)
{
// Plot the eight symmetry
points glVertex2i(x, y);
glVerte
x2i(-x, y);
glVertex2i(x, -
y);
glVertex2i(-x,
-y);

glVerte
x2i(y, x);
glVertex2i(-y,
x);
glVertex2i(y, -
x);
glVertex2i(-y,
-x);

x++;
if (p < 0) {
p += 2 * (x + 1) + 1;
}
else {
y--;
p += 2 * (x + 1) + 1 - 2 *
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the
window size
glutInitWindowPosition(100, 100); // Set the window
position glutCreateWindow("Mid-Point Circle using
OpenGL");

// Setup the viewing area


glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to
white glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION); // Switch to projection matrix


mode gluOrtho2D(-250, 250, -250, 250); // Set up the
orthogonal projection

// Register the display callback


function glutDisplayFunc(circle);
glutMainLoo

Output
Ellipse

Code
#include <GL/glut.h>
#include
<iostream> using
namespace std;

void ellipse() {
glColor3f(0.0, 0.0, 0.0); // Set the

float rx = 150; // Horizontal


float ry = 100; radius
float x = 0, y =

// Decision parameters
float p1 = ry*ry - rx*rx*ry + (0.25f)*rx*rx; // Initial decision
parameter for region 1
float dx = 2 * ry
* ry * x; float dy = 2
* rx * rx * y;

// Region 1:
Slope >= -1
glBegin(GL_POINTS);
while (dx < dy) {
// Plot symmetric points in all four
quadrants glVertex2i(x, y);
glVerte
x2i(-x, y);
glVertex2i(x, -
y);
glVertex2i(-x,
-y);

// Decision
variable update if (p1
< 0) {
x++;
dx
+= 2 * ry * ry;
p1 += dx + ry *
ry;
} else {
x
+
float p2 = (ry*ry) * (x + 0.5f) * (x + 0.5f) + (rx*rx) * (y - 1) * (y -
1)
- (rx*rx * ry * ry);

// Region 2: Slope <


-1 while (y >= 0) {
// Plot symmetric points in all four
quadrants glVertex2i(x, y);
glVertex2i(-x,
y); glVertex2i(x,
-y); glVertex2i(-
x, -y);

// Decision variable
update if (p2 > 0) {
y--;
dy -= 2 * rx *
rx; p2 += rx * rx
- dy;
} else {
y--
;
x+
+;
dx += 2 * ry *
ry; dy -= 2 * rx
* rx;
p2 += dx - dy + rx * rx;
}
}
glEnd();
glFlush()
;
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Ellipse Using OpenGL");

// Setup the viewing area


glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION); // Switch to projection matrix


mode gluOrtho2D(-250, 250, -250, 250); // Set up the orthogonal
projection

// Register the display callback


function glutDisplayFunc(ellipse);
glutMainLoop(
); return 0;
}
Output
Experiment 4

Boundary Fill

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

// Define the fill color (green) and boundary color (black)


float fillColor[3] = { 0.0, 1.0, 0.0 }; // Green color for fill
float boundaryColor[3] = { 0.0, 0.0, 0.0 }; // Black color for
boundary

// Function to set a pixel with a specific


color void setPixel(int x, int y, float*
color) {
glColor3fv(color);
glBegin(GL_POINTS)
; glVertex2i(x,
y); glEnd();
glFlush();
}

// Function to get the color of a pixel at coordinates


(x, y) void getPixelColor(int x, int y, float* color) {
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);
}

// Boundary Fill Algorithm (4-connected)


void boundaryFill4(int x, int y, float* fillColor, float*
boundaryColor) { float currentColor[3];
getPixelColor(x, y, currentColor);

// Check if the current pixel does not match the boundary or fill
color if ((currentColor[0] != boundaryColor[0] || currentColor[1]
!=
boundaryColor[1] || currentColor[2] != boundaryColor[2]) &&
(currentColor[0] != fillColor[0] || currentColor[1] !=
fillColor[1] || currentColor[2] != fillColor[2])) {

setPixel(x, y, fillColor);

// Recursively fill the surrounding pixels (4-connected


approach) boundaryFill4(x + 1, y, fillColor,
}
}

// Function to draw the


rectangle void drawRectangle()
{
glColor3f(0.0, 0.0, 0.0); // Black color for the rectangle boundary
glLineWidth(2.0);
glBegin(GL_LINE_LOOP);
glVertex2i(50, 50);
glVertex2i(150, 50);
glVertex2i(150, 100);
glVertex2i(50,
100); glEnd();
glFlush();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT)
; drawRectangle();
boundaryFill4(75, 75, fillColor, boundaryColor); // Starting point
inside the rectangle
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to
white glColor3f(0.0, 0.0, 0.0); // Set drawing color to
black glPointSize(1.0); // Set point size for drawing
gluOrtho2D(0, 200, 0, 200); // Set the coordinate system for
the
window
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB); glutInitWindowSize(200, 200);
glutInitWindowPosition(100, 100);
glutCreateWindow("Boundary Fill Algorithm -
Rectangle"); init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output

Flood Fill

Code
#include
<GL/glut.h>

// Define the fill color and background

float fillColor[3] = { 1.0, 1.0,


0.5 }; // Background color

// Function to set a pixel with a


specific color

void setPixel(int x, int y,

float* color) {

glColor3fv(color);
glBegin(GL_POINTS);

glVertex2i(x, y);

glEnd();

glFlush();

// Function to get the color of a pixel at coordinates (x, y)

void getPixelColor(int x, int y, float* color) {

glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);

// Flood Fill Algorithm (8-connected)

void floodFill8(int x, int y, float* fillColor, float* bgColor) {

float currentColor[3];

getPixelColor(x, y, currentColor);

// Check if the current pixel matches the background color


(not yet filled)

if ((currentColor[0] == bgColor[0]) && (currentColor[1] ==


bgColor[1]) && (currentColor[2] == bgColor[2])) {

setPixel(x, y, fillColor);

// Recursively fill the surrounding pixels (8-connected


approach)

floodFill8(x + 1, y, fillColor, bgColor);

floodFill8(x - 1, y, fillColor, bgColor);

floodFill8(x, y + 1, fillColor, bgColor);


floodFill8(x, y - 1, fillColor, bgColor);

floodFill8(x + 1, y + 1, fillColor, bgColor);

floodFill8(x - 1, y - 1, fillColor, bgColor);

floodFill8(x + 1, y - 1, fillColor, bgColor);

floodFill8(x - 1, y + 1, fillColor, bgColor);

// Function to draw the polygon

void drawPolygon() {

glColor3f(0.0, 0.0, 0.0); // Black color for the polygon boundary

glLineWidth(2.0);

glBegin(GL_LINE_LOOP);

glVertex2i(50, 75);

glVertex2i(75, 100);

glVertex2i(90, 90);

glVertex2i(100, 50);

glVertex2i(75, 65);

glEnd();

glFlush();

void display() {

glClear(GL_COLOR_BUFFER_BIT);
drawPolygon();

floodFill8(70, 70, fillColor, bgColor); // Starting point


inside the polygon

void init() {

glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white

glColor3f(0.0, 0.0, 0.0); // Set drawing color to black

glPointSize(1.0); // Set point size for drawing

gluOrtho2D(0, 200, 0, 200); // Set the coordinate system for


the window

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(200, 200);

glutInitWindowPosition(100, 100);

glutCreateWindow("Flood Fill Algorithm - 8 Connected");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

}
Output
Experiment 5 & 6

Cohen

Sutherland

Code
#include <GL/glut.h>
#define SCREEN_WIDTH
640
#define SCREEN_HEIGHT 480

typedef struct
{ GLfloat x,
y;
} Point;

const GLint WIN_LEFT_BIT =


0x01; const GLint WIN_RIGHT_BIT
= 0x02; const GLint
WIN_BOTTOM_BIT = 0x04; const
GLint WIN_TOP_BIT = 0x08;

void init_graph(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB); glutInitWindowSize(SCREEN_WIDTH,
SCREEN_HEIGHT);
glutInitWindowPosition(100, 100); // Center the window on screen
glutCreateWindow(argv[0]);
glClearColor(1.0, 1.0, 1.0,
0.0); glPointSize(1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);
}

void
close_graph()
{ glutMainLoop
();
}

void swap_points(Point* p1,


Point* p2) { Point t = *p1;
*p1 = *p2;
*p2 = t;
}
*y = t;
}

GLint inside(GLint
code) { return !
code;
}

GLint accept(GLint code1, GLint


code2) { return !(code1 |
code2);
}

GLint reject(GLint code1, GLint


code2) { return code1 & code2;
}

GLint encode(Point p1, Point win_min, Point


win_max) { GLint code = 0x00;
if (p1.x < win_min.x) code |=
WIN_LEFT_BIT; if (p1.x > win_max.x) code
|= WIN_RIGHT_BIT; if (p1.y < win_min.y)
code |= WIN_BOTTOM_BIT; if (p1.y >
win_max.y) code |= WIN_TOP_BIT; return
code;
}

GLint round(GLfloat a) {
return (GLint)(a +
0.5f);
}

void line_clip(Point p1, Point p2, Point win_min, Point


win_max) { GLint code1, code2;
GLint done = 0, plot_line =
0; GLfloat m = 0;
if (p1.x != p2.x) {
m = (p2.y - p1.y) / (p2.x - p1.x);
}
while (!done) {
code1 = encode(p1, win_min,
win_max); code2 = encode(p2,
win_min, win_max); if
(accept(code1, code2)) {
done = 1;
plot_line = 1;
} else if (reject(code1,
code2)) { done = 1;
} else {
if (inside(code1)) {
swap_points(&p1, &p2);
swap_codes(&code1,
&code2);
}
if (code1 & WIN_LEFT_BIT) {
p1.y += (win_min.x - p1.x) *
m; p1.x = win_min.x;
} else if (code1 &
WIN_RIGHT_BIT) { p1.y +=
(win_max.x - p1.x) * m; p1.x
= win_max.x;
} else if (code1 &
WIN_BOTTOM_BIT) { if (p1.x !=
p2.x)
p1.x += (win_min.y - p1.y) /
m; p1.y = win_min.y;
} else if (code1 &
WIN_TOP_BIT) { if (p1.x !=
p2.x)
p1.x += (win_max.y - p1.y) /
m; p1.y = win_max.y;
}
}
}
if (plot_line) {
glColor3f(1, 0,
0);
glLineWidth(2);
glBegin(GL_LINES)
;
glVertex2i(round(p1.x),
round(p1.y));
glVertex2i(round(p2.x),
round(p2.y)); glEnd();
glFlush();
}
}

void draw_window(Point win_min, Point


win_max) { glColor3f(0, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(round(win_min.x),
round(win_min.y));
glVertex2i(round(win_min.x),
round(win_max.y));
glVertex2i(round(win_max.x),
round(win_max.y));
glVertex2i(round(win_max.x),
round(win_min.y)); glEnd();
glFlush();
}

void init_clip() {
glClear(GL_COLOR_BUFFER_BIT
); Point win_min = { 60, 60
}; Point win_max = { 470,
290 }; draw_window(win_min,
win_max); Point p1 = { 50,
50 };
Point p2 = { 490, 310 };
glColor3f(0, 0,
1);
glBegin(GL_LINES)
;
glVertex2i(round(p1.x),
round(p1.y));
glVertex2i(round(p2.x),
round(p2.y)); glEnd();
line_clip(p1, p2, win_min, win_max);
}

int main(int argc,


char** argv) {
init_graph(argc, argv);
glutDisplayFunc(init_cli
p); close_graph();
return EXIT_SUCCESS;

Output
Sutherland Hodgeman

Code
#include <GL/glut.h>
#define SCREEN_WIDTH
640
#define SCREEN_HEIGHT
480 typedef struct {
GLfloat x, y;
} Point;
const GLint WIN_LEFT_BIT =
0x01; const GLint WIN_RIGHT_BIT
= 0x02; const GLint
WIN_BOTTOM_BIT = 0x04; const
GLint WIN_TOP_BIT = 0x08;
void init_graph(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCREEN_WIDTH,
SCREEN_HEIGHT); glutCreateWindow(argv[0]);
glClearColor(1.0, 1.0, 1.0,
0.0); glPointSize(1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);
}
void close_graph()
{ glutMainLoop();
}
void swap_points(Point* p1, Point*
p2) { Point t = *p1;
*p1 = *p2;
*p2 = t;
}
void swap_codes(GLint* x, GLint*
y) { GLint t = *x;
*x = *y;
*y = t;
}
GLint inside(GLint code)
{ return !code;
}
GLint accept(GLint code1, GLint
code2) { return !(code1 | code2);
}
GLint reject(GLint code1, GLint
}
GLint encode(Point p1, Point win_min, Point
win_max) { GLint code = 0x00;
if (p1.x < win_min.x) code |=
WIN_LEFT_BIT; if (p1.x > win_max.x) code
|= WIN_RIGHT_BIT; if (p1.y < win_min.y)
code |= WIN_BOTTOM_BIT; if (p1.y >
win_max.y) code |= WIN_TOP_BIT; return
code;
}
GLint round(GLfloat a)
{ return (GLint)(a +
0.5f);
}
void line_clip(Point p1, Point p2, Point win_min, Point
win_max) { GLint code1, code2;
GLint done = 0, plot_line =
0; GLfloat m = 0;
if (p1.x != p2.x) {
m = (p2.y - p1.y) / (p2.x - p1.x);
}
while (!done) {
code1 = encode(p1, win_min,
win_max); code2 = encode(p2,
win_min, win_max); if
(accept(code1, code2)) {
done = 1;
plot_line = 1;
}
else if (reject(code1,
code2)) { done = 1;
}
else {
if (inside(code1)) {
swap_points(&p1, &p2);
swap_codes(&code1,
&code2);
}
if (code1 & WIN_LEFT_BIT) {
p1.y += (win_min.x - p1.x) *
m; p1.x = win_min.x;
}
else if (code1 &
WIN_RIGHT_BIT) { p1.y +=
(win_max.x - p1.x) * m; p1.x =
win_max.x;
}
else if (code1 & WIN_BOTTOM_BIT)
{ if (p1.x != p2.x)
p1.x += (win_min.y - p1.y) /
m; p1.y = win_min.y;
}
else if (code1 & WIN_TOP_BIT) {
if (p1.x != p2.x)
p1.x += (win_max.y - p1.y) /
m; p1.y = win_max.y;
}
}
}
if (plot_line) {
glColor3f(1, 0,
0);
glLineWidth(2);
glBegin(GL_LINES)
;
glVertex2i(round(p1.x),
round(p1.y));
glVertex2i(round(p2.x),
round(p2.y)); glEnd();
glFlush();
}
}
void draw_window(Point win_min, Point
win_max) { glColor3f(0, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(round(win_min.x),
round(win_min.y));
glVertex2i(round(win_min.x),
round(win_max.y));
glVertex2i(round(win_max.x),
round(win_max.y));
glVertex2i(round(win_max.x),
round(win_min.y)); glEnd();
glFlush();
}
void init_clip() {
glClear(GL_COLOR_BUFFER_BIT
); Point win_min = { 60, 60
}; Point win_max = { 470,
290 }; draw_window(win_min,
win_max); Point p1 = { 50,
50 };
Point p2 = { 490, 310 };
glColor3f(0, 0, 1);
glBegin(GL_LINES);
glVertex2i(round(p1.x),
round(p1.y));
glVertex2i(round(p2.x),
round(p2.y)); glEnd();
line_clip(p1, p2, win_min, win_max);
}
int main(int argc, char**
argv) { init_graph(argc,
argv);
glutDisplayFunc(init_clip);
close_graph();
return EXIT_SUCCESS;}
Output
Experiment – 7 & 8
Performing 2D & 3D TRANSFORMATIONS on
objects Code (2D)
#include
<GL/glut.h>
#include
<iostream>
#include <cmath>
using namespace
std;
// Transformation parameters for
2D float angle, tx, ty, sx, sy,
shx, shy; float line_m, line_c;
void drawSquare() {
glBegin(GL_POLYGON);
glVertex2f(-0.5, -
0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5,
0.5); glEnd();
}
void applyTranslation() {
glTranslatef (tx, ty, 0.0f);
}
void applyRotation() {
glRotatef (angle, 0, 0, 1); // Rotate around Z-axis for 2D
}
void applyScaling() {
glScalef(sx, sy, 1.0f);
}
void
applyReflection(){
float reflectX[16] =
{
1 0, 0,
, 0,
0 -1, 0,
, 0,
0 0, 1,
, 0,
0 0, 0,
, 1
}
;
glMultMatrixf(reflectX);
}
void
applyReflectionY() {
float reflectY [16] =
{
-1, 0, 0, 0,
0 1 0 0
, , , ,
0 0 1 0
, , , ,
0 0 0 1
, , ,
};
glMultMatrixf(reflectY);
}
void applyReflectionLine() {
float d = sqrt(1 + line_m *
line_m); float
reflectionMatrix[16] = {
(1-line_m * line_m) / d, (2 * line_m) / d, 0, 0,
(2* line_m) / d, -(1 - line_m * line_m) / d, 0, 0, 0, 0,
1, 0, (2 * line_c) / d, (2 * line_m * line_c) / d, 0, 1
};
glMultMatrixf(reflectionMatrix);
}
void applyShear() {
float shearMatrix [16]
= { 1, shx, 0, 0,
shy, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glMultMatrixf(shearMatrix);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT
); glPushMatrix();
applyTranslation();
applyRotation();
applyScaling();
// Uncomment any of the following to test specific transformations:
// applyReflectionX();
// Reflect across X-axis
// applyReflectionY();
// Reflect across Y-axis
// applyReflectionLine();
// Reflect across line Y mX + c
// applyShear();
glColor3f(0, 1,
0); drawSquare();
glPopMatrix();
glFlush();
}
void init() {
glClearColor(0, 0, 0,
1);
glOrtho(-2, 2, -2, 2, -1,1);
}
int main(int argc, char** argv) {
cout << "Enter translation factors (tx
ty): "; cin >> tx >> ty;
cout << "Enter rotation angle (in degrees): ";
cin >> angle;
cout << "Enter scaling factors (sx
sy): "; cin >> sx >> sy;
cout << "Enter shearing factors (shx
shy): "; cin >> shx >> shy;
cout << "Enter the slope (m) and intercept (c) for reflection line
y = m*x + c: ";
cin >> line_m >>
line_c;
glutInit(&argc,
argv);
glutInitDisplayMode (GLUT_SINGLE |
GLUT_RGB); glutInitWindowSize(500,
500);
glutCreateWindow("2D Transformations with Reflection and
Shearing"); init();
glutDisplayFunc

Output
Code (3D)
#include <GL/glut.h>

float dx = 0.0f, dy = 0.0f, dz = -5.0f; // Translation factors


float thetaX = 0.0f, thetaY = 0.0f; // Rotation angles for X and Y
axes float scaleFactor = 1.0f; // Scaling factor

void drawCube() {
glBegin(GL_QUADS
);

// Front face
glColor3f(0.2f, 0.4f, 1.0f); // Light
blue glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f,
0.5f); glVertex3f(0.5f,
0.5f, 0.5f); glVertex3f(-
0.5f, 0.5f, 0.5f);

// Back face
glColor3f(0.2f, 0.3f, 0.8f); // Darker
blue glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);

// Left face
glColor3f(0.1f, 0.4f, 0.9f); // Medium
blue glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);

// Right face
glColor3f(0.3f, 0.4f, 0.9f); // Lighter
blue glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f,
0.5f); glVertex3f(0.5f,
0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -
0.5f);

// Top face
glColor3f(0.2f, 0.5f, 0.9f); // Another shade of
blue glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -
0.5f); glVertex3f(0.5f,
0.5f, 0.5f); glVertex3f(-
0.5f, 0.5f, 0.5f);

// Bottom face
glColor3f(0.2f, 0.3f, 0.7f); // Darkest
blue glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);

glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Apply transformations
glTranslatef(dx, dy, dz); //
Translation
glRotatef(thetaX, 1.0f, 0.0f, 0.0f); // Rotate around
X-axis glRotatef(thetaY, 0.0f, 1.0f, 0.0f); // Rotate
around Y-axis glScalef(scaleFactor, scaleFactor,
scaleFactor); // Scaling

drawCube();
glutSwapBuffers(
);
}

void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to
black glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0); // Set perspective
projection glMatrixMode(GL_MODELVIEW);
}

void handleKeys(unsigned char key, int x, int


y) { switch (key) {
case 'w': dy += 0.1f; break; // Move
up case 's': dy -= 0.1f; break; //
Move down case 'a': dx -= 0.1f; break;
// Move left case 'd': dx += 0.1f;
break; // Move right
case '+': scaleFactor += 0.1f; break; // Scale
up case '-': scaleFactor -= 0.1f; break; //
Scale down
case 'x': thetaX += 10.0f; break; // Rotate around
X-axis case 'y': thetaY += 10.0f; break; // Rotate
around Y-axis
}
glutPostRedisplay(); // Redraw after transformations
}

int main(int argc, char**


argv) { glutInit(&argc,
argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Transformations with Blue Cube on Black
Background"); init();
glutDisplayFunc(display);
glutKeyboardFunc(handleKeys);
glutMainLoop();
return 0;
}

Output

You might also like