ComputerGraphics LabFile (500105543)
ComputerGraphics LabFile (500105543)
ComputerGraphics LabFile (500105543)
Report
Autumn 2024
Name-:Pratiksha Bahuguna
SAP ID-:500105543
Course-:B.Tech(C.S.E.),DevOps
Roll No.-:R2142220134
3 Mid-Point Circle 9
4 Ellipse 11
Experiment 1
Introduction to OpenGL
2. Once these commands are executed, a cpp file will be made to display
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();
(GLdouble)SCREEN_HEIGHT); glBegin(GL_POINTS);
glVertex2f(50, 50); // This is now correct
/////////////////////////////////////////////////
////// glEnd();
glFlush();
}
Output
Experiment 2
Code
#include
<GL/glut.h>
#include <cmath>
(float)steps;
float yIncrement = dy / (float)steps;
float x =
x1; float y
= y1;
// 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
}
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");
Output
Ellipse
Code
#include <GL/glut.h>
#include
<iostream> using
namespace std;
void ellipse() {
glColor3f(0.0, 0.0, 0.0); // Set the
// 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);
// 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()
;
}
Boundary Fill
Code
#include
<GL/glut.h>
#include
<iostream>
// 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);
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
}
Flood Fill
Code
#include
<GL/glut.h>
float* color) {
glColor3fv(color);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
float currentColor[3];
getPixelColor(x, y, currentColor);
setPixel(x, y, fillColor);
void drawPolygon() {
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();
void init() {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutInitWindowPosition(100, 100);
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;
void
close_graph()
{ glutMainLoop
();
}
GLint inside(GLint
code) { return !
code;
}
GLint round(GLfloat a) {
return (GLint)(a +
0.5f);
}
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);
}
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>
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);
}
Output