CG Manual 21 Scheme
CG Manual 21 Scheme
CG Manual 21 Scheme
#include <GL/glut.h>
#include <iostream>
using namespace std;
while (true) {
glBegin(GL_POINTS);
glVertex2i(x0, y0);
glEnd();
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}
glClear(GL_COLOR_BUFFER_BIT);
// Draw line using Bresenham's algorithm
glColor3f(1.0f, 1.0f, 1.0f);
drawLine(x1, y1, x2, y2);
glFlush();
}
// OpenGL initialization
void initializeOpenGL(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Bresenham's Line Algorithm");
glutDisplayFunc(display);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}
In this program:
C++ Program
#include <GL/glut.h>
#include <iostream>
// Global variables
int width = 800;
int height = 600;
float rectWidth = 100.0f;
float rectHeight = 50.0f;
float rectPositionX = (width - rectWidth) / 2.0f;
float rectPositionY = (height - rectHeight) / 2.0f;
float rotationAngle = 0.0f;
float scaleFactor = 1.0f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Apply transformations
glTranslatef(rectPositionX, rectPositionY, 0.0f);
glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);
glScalef(scaleFactor, scaleFactor, 1.0f);
// Draw rectangle
glColor3f(1.0f, 0.0f, 0.0f); // Red color
drawRectangle(0.0f, 0.0f, rectWidth, rectHeight);
glFlush();
}
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}
In this program:
The glMatrixMode(GL_PROJECTION) and glLoadIdentity() in
the initializeOpenGL function ensure that the projection matrix is properly set up.
We define a simple drawRectangle function that draws a rectangle using OpenGL
primitives.
The display function is responsible for rendering the scene. It applies translation
(glTranslatef), rotation (glRotatef), and scaling (glScalef) transformations to the
rectangle before drawing it.
The glTranslatef, glRotatef, and glScalef calls in the display function are applied
correctly within the GL_MODELVIEW matrix mode.
The glColor3f(1.0f, 0.0f, 0.0f) call sets the drawing color to red (RGB values: 1.0, 0.0,
0.0).
Keyboard input is handled by the keyboard function. Pressing keys ‘t’, ‘r’, ‘s’, and ‘u’
triggers translation, rotation, scaling, and reset operations, respectively.
The initializeOpenGL function sets up the GLUT window and initializes OpenGL
settings.
In main, we initialize the OpenGL context using initializeOpenGL and start the GLUT
main loop.
To compile and run this program, make sure you have GLUT installed and set up in your
development environment. Compile the program using a C++ compiler that links against GLUT
and OpenGL libraries (e.g., using -lglut -lGL -lGLU flags). When you run the compiled
program, a window will appear displaying a red rectangle. Use the keyboard keys (‘t’, ‘r’, ‘s’,
‘u’) to perform translation, rotation, scaling, and reset operations on the rectangle interactively.
Question 3
Basic geometric operations on the 3D object
Develop a program to demonstrate basic geometric operations on the 3D object
#include <GL/glut.h>
#include <iostream>
// Global variables
int width = 800;
int height = 600;
GLfloat rotationX = 0.0f;
GLfloat rotationY = 0.0f;
GLfloat scale = 1.0f;
// Front face
glColor3f(1.0f, 0.0f, 0.0f); // Red
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.0f, 1.0f, 0.0f); // Green
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.0f, 0.0f, 1.0f); // 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(1.0f, 1.0f, 0.0f); // Yellow
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(1.0f, 0.0f, 1.0f); // Magenta
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.0f, 1.0f, 1.0f); // Cyan
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();
}
// Function to handle display
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Apply transformations
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(rotationX, 1.0f, 0.0f, 0.0f);
glRotatef(rotationY, 0.0f, 1.0f, 0.0f);
glScalef(scale, scale, scale);
// Draw cube
drawCube();
glutSwapBuffers();
}
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}
In this program:
We define a drawCube function that draws a simple cube using GL_QUADS for each face.
The display function sets up the model-view matrix and applies translation (glTranslatef),
rotation (glRotatef), and scaling (glScalef) transformations to the cube before drawing it.
Keyboard input is handled by the keyboard function. Pressing keys ‘x’, ‘X’, ‘y’, and ‘Y’ rotates
the cube around the X and Y axes, while ‘+’ and ‘-‘ keys scale the cube up and down,
respectively.
The initializeOpenGL function sets up the GLUT window, initializes OpenGL settings
(including depth testing for 3D rendering), and registers display and keyboard callback
functions.
The main function initializes the OpenGL context using initializeOpenGL and starts the GLUT
main loop.
Compile and run this program, and you should see a window displaying a rotating and scalable cube.
Use the keyboard keys (‘x’, ‘X’, ‘y’, ‘Y’, ‘+’, ‘-‘) to perform rotation and scaling operations on the cube
interactively. The modifications made in this version should ensure that the cube is visible and that
the geometric transformations work as expected in a 3D context.
Question 6
Animation effects
Develop a program to demonstrate Animation effects on simple objects.
C++ Program
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
void displayHex(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix( );
glRotatef(rotTheta, 0.0, 0.0, 1.0);
glCallList(regHex);
glPopMatrix( );
glutSwapBuffers( );
glFlush( );
}
void rotateHex(void)
{
rotTheta += 3.0;
if(rotTheta > 360.0)
rotTheta -= 360.0;
glutPostRedisplay( );
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0,(GLsizei) newWidth,(GLsizei) newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(-320.0, 320.0, -320.0, 320.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glClear(GL_COLOR_BUFFER_BIT);
}
void mouseFcn(GLint button, GLint action, GLint x, GLint y)
{
switch(button) {
case GLUT_MIDDLE_BUTTON:
// Start the rotation.
if(action == GLUT_DOWN)
glutIdleFunc(rotateHex);
break;
case GLUT_RIGHT_BUTTON:
// Stop the rotation.
if(action == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
Write a Program to read a digital image. Split and display image into 4
quadrants, up, down, right and left.
import cv2
if image is None:
print("Failed to load the image.")
else:
# Split the image into quadrants
top_left, top_right, bottom_left, bottom_right = split_image(image)
import cv2
import numpy as np
if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)
# Rotation
angle = 45 # Rotation angle in degrees
center = (image.shape[1] // 2, image.shape[0] // 2)
# Center of rotation
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
# Rotation matrix
rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1], image.shape[0]))
# Scaling
scale_factor = 0.5 # Scaling factor (0.5 means half the size)
scaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor)
# Translation
translation_matrix = np.float32([[1, 0, 100], [0, 1, -50]])
# Translation matrix (100 pixels right, 50 pixels up)
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1],
image.shape[0]))
cv2.waitKey(0)
cv2.destroyAllWindows()
Question 9
Feature Extraction
Read an image and extract and display low-level features such as edges, textures using filtering
techniques.
import cv2
import numpy as np
cv2.waitKey(0)
cv2.destroyAllWindows()
Question 10
Blurring and smoothing are common image processing techniques used to reduce noise and detail
in images. OpenCV provides various functions to perform blurring and smoothing operations. Below
is a Python program using OpenCV to read an image, apply blur and smoothing filters, and display
the results:
import cv2
if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()