0% found this document useful (0 votes)
3 views16 pages

Coding Resource

The document contains several OpenGL programs demonstrating various graphics algorithms and transformations, including Bresenham's Line Algorithm, DDA Algorithm, Mid Point Circle Algorithm, 2D Translation, Scaling, Rotation, Shearing, Animation, and Sound integration. Each section includes initialization, rendering, and keyboard interaction for manipulating graphical objects. The code is structured to create a window and display graphics using GLUT, with specific functions for each algorithm and transformation.

Uploaded by

Joy Mojumder
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)
3 views16 pages

Coding Resource

The document contains several OpenGL programs demonstrating various graphics algorithms and transformations, including Bresenham's Line Algorithm, DDA Algorithm, Mid Point Circle Algorithm, 2D Translation, Scaling, Rotation, Shearing, Animation, and Sound integration. Each section includes initialization, rendering, and keyboard interaction for manipulating graphical objects. The code is structured to create a window and display graphics using GLUT, with specific functions for each algorithm and transformation.

Uploaded by

Joy Mojumder
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

1.

Bresenham Algorithm code

#include<GL/glut.h>
#include<cmath>
#include<algorithm>
#include<iostream>

using namespace std;

void init()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}
void bresenham(int x1, int y1, int x2, int y2)
{
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);

if(dx<=dy)
{
glFlush();
return;
}
int p = 2*dy - dx;
int x = x1;
int y = y1;

glBegin(GL_POINTS);
while(x<=x2)
{
glVertex2i(x,y);

if(p<0)
{
p = p + 2*dy;
}
else
{
p = p + 2*(dy - dx);
y = y + 1;
}
x = x + 1;

}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(8.0);
bresenham(10,10, 20, 10);

glFlush();
}

int main (int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham's Line Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

[Link] ALGORITHM

#include <GL/glut.h>
#include <cmath>
#include<algorithm>

using namespace std;

void init (){


glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}

void dda(float x1, float y1, float x2, float y2){


float dx = x2-x1;
float dy = y2-y1;

int steps = max(abs(dx), abs(dy));

float x_inc = dx / steps;


float y_inc = dy / steps;

float x = x1;
float y = y1;

glBegin(GL_POINTS);
for(int i = 0; i<= steps; i++){
glVertex2f(round(x), round(y));
x = x + x_inc;
y = y + y_inc;
}
glEnd();
}

void Display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(3.0);
dda(0, 0, 0, -56);
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Digital Different Analyser");
init();
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
[Link] Point Circle Algorithm

#include <GL/glut.h>
#include <cmath>
#include <algorithm>
using namespace std;
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
}
void circle(int xr, int yr, int r)
{
int x = 0;
int y = r;
int p = 1 - r;
glBegin(GL_POINTS);
while(x<=y){
glVertex2i(xr+x,yr+y);
glVertex2i(xr-x,yr+y);
glVertex2i(xr+x,yr-y);
glVertex2i(xr-x,yr-y);
glVertex2i(xr+y,yr+x);
glVertex2i(xr-y,yr+x);
glVertex2i(xr+y,yr-x);
glVertex2i(xr-y,yr-x);
x++;
if(p<0){
p = p + 2*x + 1;
}
else{
y--;
p = p + 2*x - 2* y + 1;
}
}
glEnd();
}
void circleFilled(GLfloat rx, GLfloat ry, GLfloat cx, GLfloat cy)
{
glBegin(GL_TRIANGLE_FAN);
glVertex2f(cx, cy);
for (int i = 0; i <= 100; i++) {
float angle = 2.0f * 3.1416f * i / 100;
float x = rx * cosf(angle);
float y = ry * sinf(angle);
glVertex2f(x + cx, y + cy);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(4.0);

circle(0, 0, 30);

glColor3f(1.0, 0.0, 0.0);

circleFilled(4, 4, 17, 10);


glFlush();
}
int main(int agrc, char **argv)
{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Circle");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
4.2D Translation code

#include <GL/glut.h>
#include <iostream>

using namespace std;

float X1 =-4, Y1=-4;


float X2 =4, Y2=4;

float Tx =1;
float Ty =1;

float movx =0, movy=0;

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void rec(float x1, float y1, float x2, float y2){


glBegin(GL_QUADS);

glColor3f(1.0,0.0,0.0);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);

glEnd();

void keyboard(unsigned char key, int x, int y){

switch (key){
case 'd':movx =movx+Tx;
break;
case 'a':movx =movx-Tx;
break;

case 'w':movy =movy+Ty;


break;
case 's':movy =movy-Ty;
break;
}
glutPostRedisplay();

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glTranslatef(movx, movy, 0);


rec(X1,Y1,X2,Y2);

glutSwapBuffers();
}
int main(int agrc, char **argv)
{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();

return 0;

/////////////////////////////////////////////////////////////////////////////

5.2D Scaling
#include <GL/glut.h>
#include <iostream>

using namespace std;


float X1 =-4, Y1=-4;
float X2 =4, Y2=4;

float Sx =1;
float Sy =1;

float Scale =0.1;

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void rec(float x1, float y1, float x2, float y2){


glBegin(GL_QUADS);

glColor3f(1.0,0.0,0.0);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);

glEnd();

void keyboard(unsigned char key, int x, int y){

switch (key){
case 'd':Sx =Sx+Scale;
break;
case 'a':Sx =Sx-Scale;
break;

case 'w':Sy =Sy+Scale;


break;
case 's':Sy =Sy-Scale;
break;
}
glutPostRedisplay();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glScalef(Sx,Sy,1);

rec(X1,Y1,X2,Y2);

glutSwapBuffers();
}
int main(int agrc, char **argv)
{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();

return 0;
}

/////////////////////////////////////////////////////////////////////////////

6.2D ROTATION
#include <GL/glut.h>
#include <iostream>

using namespace std;

float X1 =-4, Y1=-4;


float X2 =4, Y2=4;

float anglestep =05;

float angle =0;

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void rec(float x1, float y1, float x2, float y2){


glBegin(GL_QUADS);

glColor3f(1.0,0.0,0.0);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);

glEnd();

void keyboard(unsigned char key, int x, int y){

switch (key){
case 'd':angle =angle+anglestep;
break;
case 'a':angle =angle-anglestep;
break;

}
glutPostRedisplay();

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotatef(angle,0,0,1);

rec(X1,Y1,X2,Y2);

glutSwapBuffers();
}
int main(int agrc, char **argv)
{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();

return 0;

/////////////////////////////////////////////////////////////////////////////

7.2D Shearing
#include <GL/glut.h>
#include <iostream>

using namespace std;

float X1 = -20, Y1 = -20;


float X2 = 20, Y2 = 20;

float shx = 0.0, shy = 0.0;


float shear = 0.1;

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void rec(float x1, float y1, float x2, float y2) {


glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);
glEnd();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'd': shx = shx + shear; break;
case 'a': shx = shx - shear; break;
case 'w': shy = shy + shear; break;
case 's': shy = shy - shear; break;

}
glutPostRedisplay();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

float shearMatrix[16] = {
1.0, shy, 0.0, 0.0,
shx, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
glMultMatrixf(shearMatrix);
rec(X1, Y1, X2, Y2);
glutSwapBuffers();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("2D Shearing");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
[Link] code
#include <GL/glut.h>
#include <iostream>

using namespace std;

float x1 = -15, y1 = -15;


float x2 = 5, y2 = 5;
float dif = abs(x1-x2);
float t=0.5f;

void rec(float x1, float y1, float x2, float y2){


glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);

glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);
glEnd();
}

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

rec(x1, y1, x2, y2);


glutSwapBuffers();
}

void update(int value){


x1=x1+t;
x2=x2+t;

if( x2>100){
t=-t;
}

else if (x1<-100){
t=-t;

}
glutPostRedisplay();
glutTimerFunc(16, update,0);
}

int main(int agrc, char **argv)


{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation");
init();
glutDisplayFunc(display);
glutTimerFunc(0, update , 0);
glutMainLoop();

return 0;

//////////////////////////////////////////////
[Link] add code
#include <GL/glut.h>
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "[Link]")

using namespace std;

float X1 = -5, Y1 = -5;


float X2 = 5, Y2 = 5;

float t = 0.5f;

void rec(float x1, float y1, float x2, float y2){


glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);

glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);

glEnd();
}

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
rec(X1, Y1, X2, Y2);
glutSwapBuffers();
}
void PlaySound(){
PlaySound(TEXT("C://Users//Administrator//Downloads//ahem_x.wav"), NULL,
SND_FILENAME | SND_ASYNC);
}
void update(int value){
X1 = X1 + t;
X2 = X2 + t;

if(X1>100){
float recw = X2 - X1;

X1 = -100;
X2 = X1 + recw;
PlaySound();

}
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
int main(int agrc, char **argv)
{
glutInit(&agrc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation");
init();
glutDisplayFunc(display);
glutTimerFunc(0, update, 0);
glutMainLoop();

return 0;

You might also like