[go: up one dir, main page]

0% found this document useful (0 votes)
2 views14 pages

Cg_lab1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

NAME: Aditya Baware

ROLL NO: 2022-IMT-007


COMPUTER GRAPHICS LAB ASSIGNMENT - 1

Q1. A

CODE:
#include <GL/glut.h>

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set window size to 128x128


glutInitWindowSize(128, 128);
glutInitWindowPosition(100, 100);
glutCreateWindow("128x128 Window");

glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Q1 B

CODE:
#include <GL/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set window size to 256x256


glutInitWindowSize(256, 256);
glutInitWindowPosition(100, 100);
glutCreateWindow("256x256 Window");

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

Q1 C

CODE:
include <GL/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set window size to 512x512


glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("512x512 Window");

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

Q1. D
CODE:
#include <GL/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set window size to 256x360


glutInitWindowSize(256, 360);
glutInitWindowPosition(100, 100);
glutCreateWindow("256x360 Window");

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

Q2. A
CODE:
#include <GL/glut.h>
#include <string.h>

// Function to draw text on the screen


void drawText(float x, float y, const char* text) {
glRasterPos2f(x, y);
for (int i = 0; i < strlen(text); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// (a) Single point with label P1


glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2f(50, 250); // P1
glEnd();
drawText(48, 240, "P1");

// (b) Three points in triangle orientation with labels P1, P2, P3


glPointSize(8.0);
glBegin(GL_POINTS);
glVertex2f(150, 250); // P1
glVertex2f(200, 300); // P2
glVertex2f(250, 250); // P3
glEnd();
drawText(148, 240, "P1");
drawText(198, 290, "P2");
drawText(248, 240, "P3");

// (c) Ten random points with labels P1 to P10


glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2f(50, 150); // P1
glVertex2f(80, 170); // P2
glVertex2f(110, 140); // P3
glVertex2f(140, 160); // P4
glVertex2f(170, 130); // P5
glVertex2f(200, 180); // P6
glVertex2f(230, 150); // P7
glVertex2f(260, 170); // P8
glVertex2f(290, 140); // P9
glVertex2f(320, 160); // P10
glEnd();
drawText(48, 140, "P1");
drawText(78, 160, "P2");
drawText(108, 130, "P3");
drawText(138, 150, "P4");
drawText(168, 120, "P5");
drawText(198, 170, "P6");
drawText(228, 140, "P7");
drawText(258, 160, "P8");
drawText(288, 130, "P9");
drawText(318, 150, "P10");

glFlush();
}
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
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the projection matrix
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Set the orthographic projection
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Computer Graphics Lab - Labeled Points");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Q2 B
CODE:
#include <GL/glut.h>
#include <string.h>

// Function to draw text on the screen


void drawText(float x, float y, const char* text) {
glRasterPos2f(x, y);
for (int i = 0; i < strlen(text); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
}
}

// Display function for the first figure (Line segment between P1 and P2)
void displayFigure1() {
glClear(GL_COLOR_BUFFER_BIT);

// Line segment between P1 and P2


glBegin(GL_LINES);
glVertex2f(50, 350); // P1
glVertex2f(150, 350); // P2
glEnd();
drawText(48, 340, "P1");
drawText(148, 340, "P2");

glFlush();
}

// Display function for the second figure (Letter "A" shape using 5 points)
void displayFigure2() {
glClear(GL_COLOR_BUFFER_BIT);
// Letter "A" shape using 5 points
glBegin(GL_LINES);
glVertex2f(50, 250); // P1
glVertex2f(75, 300); // P2

glVertex2f(75, 300); // P2
glVertex2f(100, 250); // P3

glVertex2f(62.5, 275); // P4
glVertex2f(87.5, 275); // P5
glEnd();
drawText(48, 240, "P1");
drawText(73, 310, "P2");
drawText(98, 240, "P3");
drawText(60, 265, "P4");
drawText(85, 265, "P5");

glFlush();
}

// Display function for the third figure (Star shape using 6 points)
void displayFigure3() {
glClear(GL_COLOR_BUFFER_BIT);

// Star shape using 6 points


glBegin(GL_LINES);
glVertex2f(200, 250); // P1
glVertex2f(225, 300); // P2

glVertex2f(225, 300); // P2
glVertex2f(250, 250); // P3

glVertex2f(250, 250); // P3
glVertex2f(200, 275); // P4

glVertex2f(200, 275); // P4
glVertex2f(250, 275); // P5

glVertex2f(250, 275); // P5
glVertex2f(200, 250); // P6
glEnd();
drawText(198, 240, "P1");
drawText(223, 310, "P2");
drawText(248, 240, "P3");
drawText(198, 265, "P4");
drawText(248, 265, "P5");
drawText(198, 240, "P6");

glFlush();
}

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
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the projection matrix
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Set the orthographic projection
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Window 1 - Line segment between P1 and P2


glutInitWindowSize(500, 500);
glutCreateWindow("Figure 1 - Line Segment");
init();
glutDisplayFunc(displayFigure1);

// Window 2 - Letter "A" shape using 5 points


glutInitWindowSize(500, 500);
glutCreateWindow("Figure 2 - Letter 'A'");
init();
glutDisplayFunc(displayFigure2);

// Window 3 - Star shape using 6 points


glutInitWindowSize(500, 500);
glutCreateWindow("Figure 3 - Star Shape");
init();
glutDisplayFunc(displayFigure3);

glutMainLoop();
return 0;
}

Q2 C
CODE:
#include <GL/glut.h>
#include <string.h>

// Function to draw text on the screen


void drawText(float x, float y, const char* text) {
glRasterPos2f(x, y);
for (int i = 0; i < strlen(text); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
}
}

// Display function for the rectangle


void displayRectangle() {
glClear(GL_COLOR_BUFFER_BIT);

// Rectangle using 4 points


glBegin(GL_LINE_LOOP);
glVertex2f(50, 350); // P1
glVertex2f(150, 350); // P2
glVertex2f(150, 300); // P3
glVertex2f(50, 300); // P4
glEnd();
drawText(48, 360, "P1");
drawText(148, 360, "P2");
drawText(148, 290, "P3");
drawText(48, 290, "P4");

glFlush();
}

// Display function for the pentagon


void displayPentagon() {
glClear(GL_COLOR_BUFFER_BIT);

// Pentagon using 5 points


glBegin(GL_LINE_LOOP);
glVertex2f(200, 320); // P1
glVertex2f(230, 350); // P2
glVertex2f(260, 320); // P3
glVertex2f(245, 280); // P4
glVertex2f(215, 280); // P5
glEnd();
drawText(198, 310, "P1");
drawText(228, 360, "P2");
drawText(258, 310, "P3");
drawText(243, 270, "P4");
drawText(213, 270, "P5");

glFlush();
}

// Display function for the hexagon


void displayHexagon() {
glClear(GL_COLOR_BUFFER_BIT);

// Hexagon using 6 points


glBegin(GL_LINE_LOOP);
glVertex2f(300, 300); // P1
glVertex2f(330, 320); // P2
glVertex2f(360, 300); // P3
glVertex2f(360, 260); // P4
glVertex2f(330, 240); // P5
glVertex2f(300, 260); // P6
glEnd();
drawText(298, 310, "P1");
drawText(328, 330, "P2");
drawText(358, 310, "P3");
drawText(358, 250, "P4");
drawText(328, 230, "P5");
drawText(298, 250, "P6");

glFlush();
}

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
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the projection matrix

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Create Window 1 - Rectangle


int rectangleWindow = glutCreateWindow("Figure 1 - Rectangle");
glutSetWindow(rectangleWindow);
init();
glutDisplayFunc(displayRectangle);

// Create Window 2 - Pentagon


int pentagonWindow = glutCreateWindow("Figure 2 - Pentagon");
glutSetWindow(pentagonWindow);
init();
glutDisplayFunc(displayPentagon);

// Create Window 3 - Hexagon


int hexagonWindow = glutCreateWindow("Figure 3 - Hexagon");
glutSetWindow(hexagonWindow);
init();
glutDisplayFunc(displayHexagon);

glutMainLoop();
return 0;
}

You might also like