NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

1. Implement Bresenham’s Line drawing algorithm for all types of slope.
#include<GL/glut.h>
#include<stdio.h>
int x1, y1, x2, y2;
void draw_pixel(int x, int y)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void bresenhams_line_draw(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
// x difference
int dy = y2 - y1;
// y difference
int m = dy/dx;
// slope
if (m < 1)
{
int decision_parameter = 2*dy - dx;
int x = x1;
// initial x
int y = y1;
// initial y
if (dx < 0)
// decide the first point and second point
{
x = x2;
// making second point as first point
y = y2;
x2 = x1;
}
draw_pixel (x, y);
// plot a point
while (x < x2)
// from 1st point to 2nd point
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y+1 - y);
}
else
{
x = x+1;
y = y;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y - y);
}
draw_pixel (x, y);
}
}
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
615CSL68 – Computer Graphics Lab Manual
else if (m > 1)
{
int decision_parameter = 2*dx - dy;
int x = x1;
// initial x
int y = y1;
// initial y
if (dy < 0)
{
x = x2;
y = y2;
y2 = y1;
}
draw_pixel (x, y);
while (y < y2)
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x+1 - x);
}
else
{
y = y+1;
x = x;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x- x);
}
draw_pixel(x, y);
}
}
else if (m == 1)
{
int x = x1;
int y = y1;
draw_pixel (x, y);
while (x < x2)
{
x = x+1;
y = y+1;
draw_pixel (x, y);
}
}
}
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
715CSL68 – Computer Graphics Lab Manual
void init()
{
glClearColor(1,1,1,1);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
// left ->0, right ->500, bottom ->0, top ->500
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
bresenhams_line_draw(x1, y1, x2, y2);
glFlush();
}
int main(int argc, char **argv)
{
printf( "Enter Start Points (x1,y1)n");
scanf("%d %d", &x1, &y1);
printf( "Enter End Points (x2,y2)n");
scanf("%d %d", &x2, &y2);
// 1st point from user
// 2nd point from user
glutInit(&argc, argv);
// initialize graphics system
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB ); //single buffered mode with RGB colour variants
glutInitWindowSize(500, 500);
// 500 by 500 window size
glutInitWindowPosition(220, 200);
// where do you wanna see your window
glutCreateWindow("Bresenham's Line Drawing"); // the title of your window
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutMainLoop();
// run forever
}


Create and rotate a triangle about the origin and a fixed point.
#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=0;
// don't rotate initially
float rotate_angle=0;
// initial angle
float translate_x=0,translate_y=0; // initial translation
void draw_pixel(float x1, float y1)
{
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(x1,y1);
// plot a single point
glEnd();
}
void triangle(int x, int y)
{
glColor3f(1,0,0);
glBegin(GL_POLYGON);
// drawing a Triangle
glVertex2f(x,y);
glVertex2f(x+400,y+300);
glVertex2f(x+300,y+0);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,1,1);
draw_pixel(0,0);
// mark origin point as white dot
// plot origin - white colour
if (where_to_rotate == 1) // rotate around origin
{
translate_x = 0;
// no translation for rotation around origin
translate_y = 0;
rotate_angle += 1;
// the amount of rotation angle
}
if (where_to_rotate == 2) // rotate around Fixed Point
{
translate_x = x;
// SET the translation to wherever the customer says
translate_y = y;
rotate_angle += 1;
// the amount of rotation angle
glColor3f(0,0,1);
// mark the customer coordinate as blue dot
draw_pixel(x,y);
// plot the customer coordinate - blue colour
}
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
615CSL68 – Computer Graphics Lab Manual
glTranslatef(translate_x, translate_y, 0);
// ACTUAL translation +ve
glRotatef(rotate_angle, 0, 0, 1);
// rotate
glTranslatef(-translate_x, -translate_y, 0); // ACTUAL translation -ve
triangle(translate_x,translate_y);
glutPostRedisplay();
glutSwapBuffers();
// what to rotate? – TRIANGLE boss
// call display function again and again
// show the output
}
void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}
void rotateMenu (int option)
{
if(option==1)
where_to_rotate=1;
// rotate around origin
if(option==2)
where_to_rotate=2; // rotate around customer's coordinates
if(option==3)
where_to_rotate=3; // stop rotation
}
int main(int argc, char **argv)
{
printf( "Enter Fixed Points (x,y) for Rotation: n");
scanf("%d %d", &x, &y);
// getting the user's coordinates to rotate
glutInit(&argc, argv);
// initialize the graphics system
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // SINGLE also works
glutInitWindowSize(800, 800);
// 800 by 800 size..you can change it
glutInitWindowPosition(0, 0);
// where do you wanna see your window
glutCreateWindow("Create and Rotate Triangle");
// title
init();
// initialize the canvas
glutDisplayFunc(display);
// call display function
glutCreateMenu(rotateMenu);
// menu items
glutAddMenuEntry("Rotate around ORIGIN",1);
glutAddMenuEntry("Rotate around FIXED POINT",2);
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
715CSL68 – Computer Graphics Lab Manual
glutAddMenuEntry("Stop Rotation",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
// run forever
}
*************************************


Program to draw a color cube and spin it using OpenGL transformation matrices.
#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[] = { -1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1
};
GLfloat colors[] =
{ 0, 0, 0,
1, 0, 0,
1, 1, 0,
0, 1, 0,
0, 0, 1,
1, 0, 1,
1, 1, 1,
0, 1, 1
};
GLubyte cubeIndices[] = {0,
2,
0,
1,
4,
0,
};
3,
3,
4,
2,
5,
1,
// white color
// red color .. so on for eight faces of cube
Anti-clockwise ->
visible face
Clockwise -> Non
visible face
2,
7,
7,
6,
6,
5,
1,
6,
3,
5,
7,
4
glDrawElements() draws a sequence of primitives by
hopping around vertex arrays with the associated array
indices. It reduces both the number of function calls
and
the
number
of
vertices
to
transfer.
glDrawElements() requires 4 parameters. The first one
is the type of primitive, the second is the number of
indices of index array, the third is data type of index
array and the last parameter is the address of index
array.
static GLfloat theta[]= {0, 0, 0}; // initial angles
static GLint axis=2;
// let us assume the right mouse button has been clicked initially
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef (theta[0], 1, 0, 0);
glRotatef (theta[1], 0, 1, 0);
glRotatef (theta[2], 0, 0, 1);
// first angle rotation via x axis
// second angle rotation via y axis
// third angle rotation via z axis
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices); // draw the cube
glutSwapBuffers();
// show the output
}
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
615CSL68 – Computer Graphics Lab Manual
void spinCube()
{
theta[axis] += 2;
// rotate every 2 degrees
if (theta[axis] > 360)
theta[axis] -= 360;
glutPostRedisplay();
// it the rotation angle crosses 360 degrees, make it 0 degree
// call display again
}
void mouse(int btn, int state, int x, int y)
{
if (btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis=0;
// x axis rotation
if (btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis=1;
// y axis rotation
if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
axis=2;
// z axis rotation
}
void myReshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
Maintaining the ASPECT RATIO,
i.e., whenever we change the
window size, our output should
remain same, not distorted
if(w<=h)
glOrtho (-2, 2, -2*(GLfloat)h/(GLfloat)w, 2*(GLfloat)h / (GLfloat)w, -10, 10);
else
glOrtho (-2*(GLfloat)w/(GLfloat)h, 2*(GLfloat)w / (GLfloat)h, -2, 2, -10, 10);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Spin a color cube");
glutReshapeFunc(myReshape); // calls myReshape whenever we change the window size
glutDisplayFunc(display); // call display function
glutIdleFunc(spinCube); // whenever we are idle, calls spinCube function
BMSIT & M, Bengaluru -560064 | Author: Mr. Shankar R, Asst. Prof, CSE
715CSL68 – Computer Graphics Lab Manual
glutMouseFunc(mouse); // calls mouse function whenever we interact with mouse
glEnable(GL_DEPTH_TEST); // enables depth – for 3D
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// enables colour and vertex properties
glVertexPointer(3, GL_FLOAT, 0, vertices); // glVertexPointer(size,type,stride,pointer)
glColorPointer(3, GL_FLOAT, 0, colors);
// glColorPointer(size,type,stride,pointer)
glColor3f(1, 1, 1);
glutMainLoop();

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.