CG LAB ASSIGNMENTmajor
CG LAB ASSIGNMENTmajor
GRAPHICS LAB
ASSIGNMENT
Lab Assignment 1
Question 1
Code:
#include <GL/glut.h>
// Draw a rectangle
glBegin(GL_POLYGON);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();
return 0;
}
OUTPUT-
Lab Assignment 2
Question 2.1
Code:
#include <GLUT/glut.h>
#include <cstdio> // Include for sprintf
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
float colors[][3] = {
{1.0f, 1.0f, 0.0f}, // Yellow
{0.0f, 0.0f, 1.0f}, // Blue
{0.0f, 0.0f, 0.0f}, // Black (default)
{0.0f, 1.0f, 0.0f} // Green
};
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(resolutions[i][0], resolutions[i][1]);
glutCreateWindow(title);
// Initialize the background color for this window
init(colors[i][0], colors[i][1], colors[i][2]);
glutDisplayFunc(display);
}
return 0;
}
Output:
Question 2.2
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2f(0.0f, 0.0f); // Draw the point at the center of the window
glEnd();
glFlush();
}
glutDisplayFunc(display);
}
Output:
Question 2.3
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init() {
glClearColor(0.9f, 0.9f, 0.98f, 1.0f); // Lavender background
glutMainLoop();
return 0;
}
Output:
Question 2.4
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the point size and color for the "C" and "G"
glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points
void init() {
glClearColor(0.9f, 0.9f, 0.98f, 1.0f); // Lavender background
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 2.5
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 2.6
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glutMainLoop();
return 0;
}
Output-
Question 2.7
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 2.8
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-
Question 2.9
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the point size and color for the points
glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points
// Draw the points (coordinates chosen to form a pentagon)
glBegin(GL_POINTS);
glVertex2f(3.0f, 5.0f); // Top point
glVertex2f(1.5f, 3.0f); // Top-left point
glVertex2f(2.0f, 0.5f); // Bottom-right point
glVertex2f(4.0f, 0.5f); // Bottom-right point
glVertex2f(4.5f, 3.0f); // Top-right point
glEnd();
// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 2.10
Code:
#include <GLUT/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Lab Assignment 3
Question 3.1
Code:
#include <GLUT/glut.h>
#include <cmath>
#include <iostream>
float x = x1;
float y = y1;
glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xIncrement;
y += yIncrement;
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Orthogonal projection
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512); // Set window size to 512x512
glutCreateWindow("Line Drawing Algorithms");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-
Question 3.2
Code-
#include <GLUT/glut.h>
#include <cmath>
#include <iostream>
#include <chrono> // Include chrono library for time measurement
using namespace std;
using namespace std::chrono;
float x = x1;
float y = y1;
glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xIncrement;
y += yIncrement;
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glFlush();
}
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Orthogonal projection
glMatrixMode(GL_MODELVIEW);
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-
Question 3.3
Code-
#include <GL/glut.h>
#include <iostream>
#include <cmath>
float x = x1;
float y = y1;
glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2f(x, y); // Direct coordinates, no need to normalize
x += xIncrement;
y += yIncrement;
}
glEnd();
}
// Initialization function
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, 512, 512); // Set the viewport size
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Set the orthographic projection
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("DDA Line Drawing for Various Shapes"); // Create the
window
// Initialize OpenGL
init();
Output:
Lab Assignment 4
Question 4.1
Code:
#include <GL/glut.h>
#include <cmath>
return 0;
}
Output:
Question 4.2
Code :
#include <GL/glut.h>
#include <cmath>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
color mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 50"); // Create window with title
return 0;
}
Output:
Question 4.2.a
Code:
#include <GL/glut.h>
#include <cmath>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 1"); // Create the window
return 0;
}
Output:
Question 4.2.b
Code:
#include <GL/glut.h>
#include <cmath>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 50"); // Create the window
return 0;
}
Output:
Question 4.3.a
Code:
#include <GL/glut.h>
#include <cmath>
dx = abs(dx);
dy = abs(dy);
glFlush();
}
// OpenGL Initialization
void initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: White
glColor3f(0.0, 0.0, 1.0); // Line color: Blue
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 400); // Coordinate system
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing");
initOpenGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 4.3.b
Code:
#include <GL/glut.h>
#include <cmath>
// Function to draw a pixel
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
dx = abs(dx);
dy = abs(dy);
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
// OpenGL Initialization
void initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: White
glColor3f(1.0, 0.0, 0.0); // Line color: Red
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 400); // Coordinate system
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing - Letter A");
initOpenGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 4.3
Code:
#include <iostream>
#include <GL/glut.h>
#include <cmath>
while (x > y) {
y++;
if (p > 0) {
x--;
p = p + 4 * (y - x) + 10;
} else {
p = p + 4 * y + 6;
}
glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY - x);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX - y, centerY + x);
glEnd();
}
}
// Display callback function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Bresenham's Circle Algorithm - Concentric Circles"); // Create
the window
return 0;
}
Output:
Lab Assignment 5
Question 5.1
Code:
#include <GL/glut.h>
#include <iostream>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm"); // Create the window
return 0;
}
Output:
Question 5.2
Code:
#include <GL/glut.h>
#include <cmath>
// Function to implement the Midpoint Line Algorithm
void midpointLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true) {
glBegin(GL_POINTS); // Start drawing points
glVertex2i(x0, y0); // Plot the pixel at the current point
glEnd();
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm - Letter A"); // Create the window
// Set the background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
// Set the viewport to cover the entire window
glViewport(0, 0, 400, 400);
return 0;
}
Output:
Question 5.3
Code:
#include <GL/glut.h>
#include <cmath>
while (true) {
glBegin(GL_POINTS); // Start drawing points
glVertex2i(x0, y0); // Plot the pixel at the current point
glEnd();
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm - Arrow Shape"); // Create the
window
return 0;
}
Output:
Question 5.4
Code:
#include <GL/glut.h>
#include <cmath>
while (x < y) {
x++;
if (d < 0) {
d += 2 * x + 1;
} else {
y--;
d += 2 * (x - y) + 1;
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Circle Algorithm"); // Create the window
return 0;
}
Output:
Question 5.5
Code:
#include <GL/glut.h>
#include <cmath>
// Region 1
while (dx < dy) {
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y); // First quadrant
glVertex2i(xc - x, yc + y); // Second quadrant
glVertex2i(xc + x, yc - y); // Fourth quadrant
glVertex2i(xc - x, yc - y); // Third quadrant
glEnd();
if (d1 < 0) {
x++;
dx = 2 * b2 * x;
d1 += dx + b2;
} else {
x++;
y--;
dx = 2 * b2 * x;
dy = 2 * a2 * y;
d1 += dx - dy + b2;
}
}
// Region 2
float d2 = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2;
while (y >= 0) {
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y); // First quadrant
glVertex2i(xc - x, yc + y); // Second quadrant
glVertex2i(xc + x, yc - y); // Fourth quadrant
glVertex2i(xc - x, yc - y); // Third quadrant
glEnd();
if (d2 > 0) {
y--;
dy = 2 * a2 * y;
d2 += a2 - dy;
} else {
y--;
x++;
dx = 2 * b2 * x;
dy = 2 * a2 * y;
d2 += dx - dy + a2;
}
}
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Ellipse Algorithm"); // Create the window
return 0;
}
Output:
Lab Assignment 6
Question 6.1
Code:
#include <GL/glut.h>
#include <cmath>
#include <string>
void drawCircleSection(float centerX, float centerY, float radiusX, float radiusY, const
std::string& label) {
glColor3f(0.996f, 0.502f, 0.506f);
glBegin(GL_POLYGON);
for (int i = 0; i <= 360; i++) {
float angle = i * PI / 180;
glVertex2f(centerX + cos(angle) * radiusX, centerY + sin(angle) * radiusY);
}
glEnd();
void drawEllipseSection(float rx, float ry, float centerX, float centerY, float angle) {
glColor3f(0.702f, 1.0f, 0.502f);
glBegin(GL_POLYGON);
for (int i = 0; i <= 360; i++) {
float rad = i * PI / 180;
float x = cos(rad) * rx;
float y = sin(rad) * ry;
rotatePoint(x, y, angle);
glVertex2f(x + centerX, y + centerY);
}
glEnd();
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300, 300, -300, 400, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Output:
Question 6.2
Code:
#include <GL/glut.h>
#include <cmath>
void drawArchInSquare() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, 0.0, 6.0, -1.0, 1.0);
}
Output:
Lab Assignment 7
Q 7.1.a
Code-
#include <GL/glut.h>
// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Tetrahedron"); // Create window
Output:
Q. 7.1.b
Code-
#include <GL/glut.h>
// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
// Set up the projection using glFrustum()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Hexahedron"); // Create window
Output-
Q. 7.1.c
Code-
#include <GL/glut.h>
// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Octahedron"); // Create window
Output:
Q. 7.1.d
Code-
#include <GL/glut.h>
#include <cmath>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Wireframe Dodecahedron");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-
Q 7.1.e
Code-
#include <GL/glut.h>
#include <cmath>
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Icosahedron"); // Create window
Output:
Q 7.2.a
Code-
#include <GL/glut.h>
#include <cmath>
float lat1 = M_PI * (-0.5 + (float)(i + 1) / STACKS); // Latitude angle for next
stack
float z1 = radius * sin(lat1); // Z position for next stack
float r1 = radius * cos(lat1); // Radius for next stack
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Set window size
glutCreateWindow("Wireframe Sphere"); // Create window
Output:
Q 7.2.b
Code-
#include <GL/glut.h>
#include <cmath>
Output:
Q 7.2.c
Code-
#include <GL/glut.h>
#include <cmath>
const int SLICES = 20; // Number of segments for the circular base
const int STACKS = 6; // Number of circular outlines along the height
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 100.0);
glMatrixMode(GL_MODELVIEW);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Wireframe Cylinder");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Lab Assignment 8
Question 8.1
Code-
#include <GL/glut.h>
void drawControlPoints() {
glPointSize(5.0);
glColor3f(1.0, 0.0, 0.0); // Red color for control points
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i) {
glVertex3fv(controlPoints[i]);
}
glEnd();
}
void drawBezierCurve() {
glColor3f(0.0, 0.0, 0.0); // Black color for the curve
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &controlPoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 100; ++i) {
glEvalCoord1f((GLfloat)i / 100.0); // Evaluate the curve at each point
}
glEnd();
glDisable(GL_MAP1_VERTEX_3);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawControlPoints();
drawBezierCurve();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 0.0, 500.0, -1.0, 1.0); // Set up orthographic projection
glMatrixMode(GL_MODELVIEW); // Switch back to model view matrix
}
Question 8.2
Code:
#include <GL/glut.h>
#include <cmath>
#include <string>
// Function to draw the outer square with perimeter markings, arrows, and scale
numbers
void drawOuterSquare() {
glBegin(GL_LINE_LOOP);
glVertex2f(0.0f, 0.0f); // Bottom-left
glVertex2f(1.0f, 0.0f); // Bottom-right
glVertex2f(1.0f, 1.0f); // Top-right
glVertex2f(0.0f, 1.0f); // Top-left
glEnd();
// Bottom side
drawArrow(0.0f, 0.0f, 0.5f, 0.0f, arrowSize);
drawArrow(0.5f, 0.0f, 1.0f, 0.0f, arrowSize);
// Right side
drawArrow(1.0f, 0.0f, 1.0f, 0.5f, arrowSize);
drawArrow(1.0f, 0.5f, 1.0f, 1.0f, arrowSize);
// Top side
drawArrow(1.0f, 1.0f, 0.5f, 1.0f, arrowSize);
drawArrow(0.5f, 1.0f, 0.0f, 1.0f, arrowSize);
// Left side
drawArrow(0.0f, 1.0f, 0.0f, 0.5f, arrowSize);
drawArrow(0.0f, 0.5f, 0.0f, 0.0f, arrowSize);
glEnd();
// X-axis labels
if (i > 0) // Avoid overlap with Y-axis label
drawText(pos, -0.05f, label);
// Y-axis labels
drawText(-0.05f, pos, label);
}
}
// Function to draw the inner pentagon with clockwise direction arrows and control
points
void drawInnerPentagon() {
float arrowSize = 0.03f;
// Top side
drawArrow(vertices[2][0], vertices[2][1], (vertices[2][0] + vertices[3][0]) / 2,
(vertices[2][1] + vertices[3][1]) / 2, arrowSize);
drawArrow((vertices[2][0] + vertices[3][0]) / 2, (vertices[2][1] + vertices[3][1]) / 2,
vertices[3][0], vertices[3][1], arrowSize);
glEnd();
// Draw outer square with direction arrows, perimeter markings, and scale numbers
drawOuterSquare();
// Draw inner pentagon with clockwise direction arrows and control points
drawInnerPentagon();
glFlush();
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutCreateWindow("Pentagon with Control Points and Direction Arrows");
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Output-
Lab Assignment 9
Question 9.1
Code:
#include <GL/glut.h>
#include <cmath>
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
// Draw sphere
glColor3f(0.0, 1.0, 1.0);
gluSphere(quadric, 2.0, 32, 32);
// Draw cylinder
glColor3f(1.0, 0.0, 1.0);
glTranslatef(0.0, 3.0, 0.0);
gluCylinder(quadric, 1.0, 1.0, 2.0, 32, 32);
glPopMatrix();
gluDeleteQuadric(quadric);
}
// Animate translation
if(animationDirection) {
transX += 0.05f;
if(transX >= 5.0f) animationDirection = false;
} else {
transX -= 0.05f;
if(transX <= -5.0f) animationDirection = true;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
switch(currentShape) {
case 1:
drawPyramid();
break;
case 2:
drawQuadricSurface();
break;
case 3:
drawNestedTransformation();
break;
case 4:
drawRotatingCubes();
break;
}
glutSwapBuffers();
}
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(0, update, 0);
glutMainLoop();
return 0;
}
Output-
Question 9.2
Code:
#include <GL/glut.h>
// Quadric object
GLUquadric* quadric;
// X-axis (Red)
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-5.0, 0.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
// Y-axis (Green)
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, -5.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);
// Z-axis (Blue)
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, -10.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the view (camera perspective)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4.0, 3.0, 10.0, // Eye position (further back for wider view)
0.0, 0.0, -5.0, // Look-at position (centered at the quadric surface)
0.0, 1.0, 0.0); // Up vector
// Transform the quadric object to the origin 10 units along the -ve Z-axis
glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f); // Shift closer to ensure it's visible
glColor3f(0.0, 0.5, 1.0); // Light blue color for quadric surface
drawQuadricSurface();
glPopMatrix();
glutSwapBuffers();
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Quadric Surface with Scaled Coordinate Axes");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Question 9.4
Code:
#include <GL/glut.h>
Output-
Lab Assignment 10
Question 10.1
Code:
#include <GL/glut.h>
#include <cmath>
void drawTriangle(float tx = 0.0f, float ty = 0.0f, float angle = 0.0f, float scaleX = 1.0f,
float scaleY = 1.0f) {
// Original coordinates of the triangle vertices
float vertices[3][2] = {
{6.0f, 2.0f},
{10.0f, 2.0f},
{8.0f, 4.0f}
};
void drawRectangle(float tx = 0.0f, float ty = 0.0f, float scaleX = 1.0f, float scaleY =
1.0f) {
// Original coordinates of the rectangle vertices
float vertices[4][2] = {
{-2.0f, -2.0f},
{2.0f, -2.0f},
{2.0f, 2.0f},
{-2.0f, 2.0f}
};
void drawRotationGuide() {
// Draw dashed line for rotation guide
glColor3f(0.5f, 0.5f, 0.5f); // Dark gray
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x00FF); // Dashed line pattern
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(3.0f, 3.0f); // Line to rotated position
glEnd();
glDisable(GL_LINE_STIPPLE);
// Draw θ label
glRasterPos2f(0.8f, 0.3f);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'θ');
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Part A: Translation
glViewport(0, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 20.0, 0.0, 10.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawGrid();
drawTriangle(); // Original triangle
drawTriangle(0.0f, -2.0f); // Translated triangle
// Part B: Rotation
glViewport(300, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawRotationGuide();
drawTriangle(0.0f, 0.0f, 0.0f, 0.3f, 0.3f); // Original triangle
drawTriangle(0.0f, 0.0f, 45.0f, 0.3f, 0.3f); // Rotated triangle
// Part C: Scaling
glViewport(600, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutSwapBuffers();
}
void init() {
glClearColor(0.9f, 0.9f, 0.8f, 1.0f);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(900, 600);
glutCreateWindow("2D Transformations - Manual");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q 10.2
Code:
#include <GL/glut.h>
#include <math.h>
// X-axis
glVertex2f(-10.0, 0.0);
glVertex2f(20.0, 0.0);
// Y-axis
glVertex2f(0.0, -10.0);
glVertex2f(0.0, 20.0);
glEnd();
}
glFlush();
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Q 10.3
Code:
#include <GL/glut.h>
#include <math.h>
// X-axis
glVertex2f(-10.0, 0.0);
glVertex2f(200.0, 0.0);
// Y-axis
glVertex2f(0.0, -10.0);
glVertex2f(0.0, 200.0);
glEnd();
}
xA_rotated *= scaleX;
yA_rotated *= scaleY;
xB_rotated *= scaleX;
yB_rotated *= scaleY;
xC_rotated *= scaleX;
yC_rotated *= scaleY;
// Adjust the rotated and scaled triangle to the new coordinates A'(125,125),
B'(125,175), C'(75,150)
// Define new target points directly
float A_prime_x = 125.0, A_prime_y = 125.0;
float B_prime_x = 125.0, B_prime_y = 175.0;
float C_prime_x = 75.0, C_prime_y = 150.0;
glFlush();
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output: