0% found this document useful (0 votes)
20 views12 pages

OpenGl Assg

The document contains OpenGL code to draw the first three letters of a name ('SYE') using Bresenham's and DDA line drawing algorithms. It also includes code to create the Olympic logo using a mid-point circle drawing algorithm and a classroom scene featuring a teacher and students. Each section provides detailed implementations for rendering graphics using OpenGL functions.

Uploaded by

thug9104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

OpenGl Assg

The document contains OpenGL code to draw the first three letters of a name ('SYE') using Bresenham's and DDA line drawing algorithms. It also includes code to create the Olympic logo using a mid-point circle drawing algorithm and a classroom scene featuring a teacher and students. Each section provides detailed implementations for rendering graphics using OpenGL functions.

Uploaded by

thug9104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2022115103

Draw first three letters of your name using OPgl. Use both DDA
and Bresenhams algorithm wherever needed
Output:

Code:

#include <GLUT/glut.h>

#include <iostream>
#include <cmath>

using namespace std;

void drawLineBresenham(int x1, int y1, int x2, int y2)


{
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;

while (true)
{
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glEnd();

if (x1 == x2 && y1 == y2)


break;

int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x1 += sx;
}
if (e2 < dx)
{
err += dx;
y1 += sy;
}
}
}

// Function to implement 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 xInc = dx / (float)steps;
float yInc = dy / (float)steps;
float x = x1;
float y = y1;

for (int i = 0; i <= steps; i++)


{
glBegin(GL_POINTS);
glVertex2i(round(x), round(y));
glEnd();
x += xInc;
y += yInc;
}
}

// Function to draw "SYE"


void drawSYE()
{
glColor3f(1.0, 1.0, 1.0); // White color

// Drawing 'S' using Bresenham


drawLineBresenham(50, 150, 100, 150);
drawLineBresenham(50, 150, 50, 100);
drawLineBresenham(50, 100, 100, 100);
drawLineBresenham(100, 100, 100, 50);
drawLineBresenham(100, 50, 50, 50);

// Drawing 'Y' using DDA


drawLineDDA(120, 150, 140, 120);
drawLineDDA(140, 120, 160, 150);
drawLineDDA(140, 120, 140, 50);

// Drawing 'E' using Bresenham


drawLineBresenham(180, 150, 230, 150);
drawLineBresenham(180, 150, 180, 50);
drawLineBresenham(180, 100, 220, 100);
drawLineBresenham(180, 50, 230, 50);
}

// OpenGL display function


void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawSYE();
glFlush();
}

void init()
{
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 300, 0, 200);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 400);
glutCreateWindow("Draw first three letter of my name 'SYE' using OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Draw the Olympics Logo using Mid-point circle drawing


algorithm and fill it with different pattern attributes.
Output:
Code:

#include <GLUT/glut.h>
#include <cmath>

void drawCircle(int xc, int yc, int r)


{
int x = 0, y = r, d = 1 - r;
while (x <= y)
{
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y);
glVertex2i(xc - x, yc + y);
glVertex2i(xc + x, yc - y);
glVertex2i(xc - x, yc - y);
glVertex2i(xc + y, yc + x);
glVertex2i(xc - y, yc + x);
glVertex2i(xc + y, yc - x);
glVertex2i(xc - y, yc - x);
glEnd();
x++;
if (d < 0)
d += 2 * x + 1;
else
{
y--;
d += 2 * (x - y) + 1;
}
}
}

void drawOlympicRings()
{

int r = 50;
int x1 = 100, y1 = 200;
int x2 = x1 + 60, y2 = y1;
int x3 = x2 + 60, y3 = y1;
int x4 = x1 + 30, y4 = y1 - 60;
int x5 = x2 + 30, y5 = y4;

glColor3f(0, 0, 1);
drawCircle(x1, y1, r);
glColor3f(1, 1, 0);
drawCircle(x2, y2, r);
glColor3f(0, 0, 0);
drawCircle(x3, y3, r);
glColor3f(0, 1, 0);
drawCircle(x4, y4, r);
glColor3f(1, 0, 0);
drawCircle(x5, y5, r);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawOlympicRings();
glFlush();
}

void init()
{
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 400);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 400);
glutCreateWindow("Olympic Rings");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

3. Create a class room scene using the algorithms that you have
learnt.
Output:

Code:

#include <GLUT/glut.h>

void drawCircle(int xc, int yc, int r)


{
int x = 0, y = r, d = 1 - r;
while (x <= y)
{
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y);
glVertex2i(xc - x, yc + y);
glVertex2i(xc + x, yc - y);
glVertex2i(xc - x, yc - y);
glVertex2i(xc + y, yc + x);
glVertex2i(xc - y, yc + x);
glVertex2i(xc + y, yc - x);
glVertex2i(xc - y, yc - x);
glEnd();
x++;
if (d < 0)
d += 2 * x + 1;
else
{
y--;
d += 2 * (x - y) + 1;
}
}
}

void drawTeacher()
{
// Teacher's body (Rectangle)
glColor3f(0.4f, 0.2f, 0.0f); // Brown color
glBegin(GL_QUADS);
glVertex2i(230, 230);
glVertex2i(270, 230);
glVertex2i(270, 300);
glVertex2i(230, 300);
glEnd();

// Teacher's head (Circle)


glColor3f(1.0f, 0.8f, 0.6f); // Skin color
drawCircle(250, 320, 20);

// Teacher's arms (Lines)


glColor3f(0.4f, 0.2f, 0.0f); // Brown color
glBegin(GL_LINES);
glVertex2i(230, 270); // Left arm
glVertex2i(180, 230);
glVertex2i(270, 270); // Right arm
glVertex2i(320, 230);
glEnd();
}

void drawStudent(int x, int y)


{
// Student's body (Rectangle)
glColor3f(0.4f, 0.2f, 0.0f); // Brown color
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + 40, y);
glVertex2i(x + 40, y + 50);
glVertex2i(x, y + 50);
glEnd();

// Student's head (Circle)


glColor3f(1.0f, 0.8f, 0.6f); // Skin color
drawCircle(x + 20, y + 60, 15);
}

void drawClassroom()
{
// Draw walls
glColor3f(0.8f, 0.8f, 0.8f); // Light grey walls
glBegin(GL_QUADS);
glVertex2i(50, 50); // Bottom-left corner
glVertex2i(450, 50); // Bottom-right corner
glVertex2i(450, 350); // Top-right corner
glVertex2i(50, 350); // Top-left corner
glEnd();
// Draw floor
glColor3f(0.6f, 0.4f, 0.2f); // Brown floor
glBegin(GL_QUADS);
glVertex2i(50, 50);
glVertex2i(450, 50);
glVertex2i(450, 100);
glVertex2i(50, 100);
glEnd();

// Draw blackboard
glColor3f(0.0f, 0.0f, 0.0f); // Black color
glBegin(GL_QUADS);
glVertex2i(100, 300);
glVertex2i(400, 300);
glVertex2i(400, 350);
glVertex2i(100, 350);
glEnd();

// Draw teacher
drawTeacher();

// Draw students
drawStudent(60, 150); // First student
drawStudent(120, 150); // Second student
drawStudent(180, 150); // Third student
drawStudent(240, 150); // Fourth student
drawStudent(300, 150); // Fifth student

// Draw benches (for students)


glColor3f(0.4f, 0.2f, 0.0f); // Brown color for benches
glBegin(GL_QUADS);
glVertex2i(60, 120); // First bench
glVertex2i(100, 120);
glVertex2i(100, 150);
glVertex2i(60, 150);
glEnd();
glBegin(GL_QUADS);
glVertex2i(120, 120); // Second bench
glVertex2i(160, 120);
glVertex2i(160, 150);
glVertex2i(120, 150);
glEnd();
glBegin(GL_QUADS);
glVertex2i(180, 120); // Third bench
glVertex2i(220, 120);
glVertex2i(220, 150);
glVertex2i(180, 150);
glEnd();
glBegin(GL_QUADS);
glVertex2i(240, 120); // Fourth bench
glVertex2i(280, 120);
glVertex2i(280, 150);
glVertex2i(240, 150);
glEnd();
glBegin(GL_QUADS);
glVertex2i(300, 120); // Fifth bench
glVertex2i(340, 120);
glVertex2i(340, 150);
glVertex2i(300, 150);
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawClassroom();
glFlush();
}

void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 400);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 400);
glutCreateWindow("Classroom Scene with Teacher and Students");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like