Showing posts with label computer graphics. Show all posts
Showing posts with label computer graphics. Show all posts

Sunday, January 11, 2015

Modelling Transformations in OpenGL

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

/* This function is to draw a triangle  */
void draw_triangle()
{
  glBegin(GL_LINES);
  glVertex2f(-0.40, -0.25);
  glVertex2f(-0.60, -0.25);

  glVertex2f(-0.60, -0.25);
  glVertex2f(-0.50, 0.25);

  glVertex2f(-0.40, -0.25);
  glVertex2f(-0.50, 0.25);
  glEnd();
  glFlush();
}

void draw()
{
  glClear(GL_COLOR_BUFFER_BIT);

  /* Draw a triangle using solid lines */
  draw_triangle();                   /* solid lines */

  /* The same triangle is drawn again, but with a dashed  */
  /* line stipple and translated (to the left along the  */
  /* negative x­axis) */
  glEnable(GL_LINE_STIPPLE);         /* dashed lines */
  glLineStipple(1, 0xF0F0); 
  glLoadIdentity();
  glTranslatef(-0.30, 0.0, 0.0);
  draw_triangle();

  /* A triangle is drawn with a long dashed line stipple,  */
  /* with its height (y­axis) halved and its width (x­axis)  */
  /* increased by 50%  */
  glLineStipple(1, 0xF00F);          /*long dashed lines */
  glLoadIdentity();
  glScalef(1.5, 0.5, 1.0);
  draw_triangle();

  /* A rotated triangle(rotated at 90 degree w.r.t the z-axix), 
  made of dotted lines, is drawn */
  glLineStipple(1, 0x8888);          /* dotted lines */
  glLoadIdentity();
  glRotatef (90.0, 0.0, 0.0, 1.0);
  draw_triangle();
  glDisable (GL_LINE_STIPPLE);

  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  /* Set fill color to white */
  glColor3f(1.0, 1.0, 1.0);
  gluOrtho2D(0.0 , 1.0 , 0.0 , 1.0);
  /* glViewport() command is used to define the rectangle of */
  /* the rendering area where the final image is mapped */
  glViewport(0.0, 0.0, 1.0, 1.0);
  /* glMatrixMode specifies the mode of transformation */
  glMatrixMode(GL_MODELVIEW);
  /* set the current matrix to the identity matrix */
  glLoadIdentity();
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Transformation");
  Init();
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}

Output
======

















Written by

Tuesday, December 23, 2014

2D scaling Without Using OpenGL Function For Scaling

Program to draw a square of side 100 units at the center of the screen and scale it such that it enlarges to a square of side 150 units without using OpenGL function for scaling


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

int i;
float scalex, scaley, a[10], b[10], x, y, square_side;

void line(float x, float y, float a, float b)
{
  glBegin(GL_LINES);
  glVertex2f(x, y);
  glVertex2f(a, b);
  glEnd();
}

void display_square()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0.0 , 640.0 , 0.0 , 480.0);
}

void scale()
{
  // Find the translated rectangle vertices
  for(i = 0; i < 4; i++)
  {
    a[i] = a[i] * scalex; b[i] = b[i] * scaley;
  }
}

void draw()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original square vertices
  // (x, y) represents the upper left point
  x = 270.0; y = 290.0;

  a[0] = x; b[0] = y;
  a[1] = x + square_side; b[1] = y;
  a[2] = x + square_side; b[2] = y - square_side;
  a[3] = x; b[3] = y - square_side;
  // Draw original square
  display_square();

  // Perform scaling
  scale();
  // Draw scaled square using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_square();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  square_side = 100;
  printf("\n************************************************\n");
  printf("Length of the side of the square: %f\n", square_side);

  scalex = 1.5;
  scaley = 1.5;
  printf("\n************************************************\n");
  printf("Scaling factor along both directions: %f\n", scalex);
  printf("\n************************************************\n");

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Scaling");
  Init();
  glutDisplayFunc(draw);
  glutMainLoop();
}


Written by

2D Rotation Without Using OpenGL Function For Rotation

Program to perform 2D rotation without using OpenGL function for rotation

#include<stdio.h>
#include<math.h>
#include<GL/glut.h>

int x, y, p, q, xa, ya, ra, i, a[10], b[10], da, db;
float dx, dy, theta;

void line(int x, int y, int a, int b)
{
  glVertex2d(x, y);
  glVertex2d(a, b);
}

void display_rectangle()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void rotate()
{
  // Find the vertices of the rotated rectangle
  theta = (float)(ra*(3.14/180));
  for(i = 0;i<4;i++)
  {
    a[i] = (xa + ((a[i] - xa)*cos(theta) - (b[i] - ya)*sin(theta)));
    b[i] = (ya + ((a[i] - xa)*sin(theta) + (b[i] - ya)*cos(theta)));
  }
}

void transform()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glBegin(GL_LINES);
  // Find the original rectangle vertices
  da = p - x; db = q - y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;

  // Draw original rectangle
  display_rectangle();

  rotate();
  
  // Draw rotated rectangle
  display_rectangle();
  
  glEnd();

  glFlush();

}

void main(int argc, char **argv)
{
  printf("\n**********************************************\n");
  printf("Enter the upper left corner of the rectangle:\n");
  scanf("%d%d", &x, &y);
  printf("\n**********************************************\n");
  printf("Enter the lower right corner of the rectangle:\n");
  scanf("%d%d", &p, &q);

  printf("\n********************Rotation********************\n");
  printf("Enter the value of fixed point and angle of rotation:\n");
  scanf("%d%d%d", &xa, &ya, &ra);

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Rotation");
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}

Written by

Monday, December 22, 2014

2D Translation Without Using OpenGL Function For Translation

Program to draw a rectangle and translate it without using OpenGL function for translation

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

int x, y, p, q, a[10], b[10], da, db, i;
float dx, dy;

void line(int x, int y, int a, int b)
{
  glBegin(GL_LINES);
  glVertex2d(x, y);
  glVertex2d(a, b);
  glEnd();
}

void display_rectangle()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void translate()
{
  // Find the translated rectangle vertices
  for(i = 0; i < 4; i++)
  {
    a[i] = a[i] + dx; b[i] = b[i] + dy;
  }
}

void transform()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original rectangle vertices
  da = p-x; db = q-y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;
  // Draw original rectangle
  display_rectangle();

  // Perform translation
  translate();
  // Draw translated rectangle using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_rectangle();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  printf("\n************************************************\n");
  printf("Enter the upper left corner of the rectangle:\n");
  scanf("%d%d", &x, &y);
  printf("\n************************************************\n");
  printf("Enter the lower right corner of the rectangle:\n");
  scanf("%d%d", &p, &q);

  printf("\n******************Translation******************\n");
  printf("Enter the value of shift vector:\n");
  scanf("%f%f", &dx, &dy);

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Translation");
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}
Written by

Sunday, December 14, 2014

Smiley using Bresenham's Circle Drawing in OpenGL


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

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot two points on the lower quadrants 
// using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc-x, yc-y);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a smiley
void smiley(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw an arc
  bresenham_circle(radius);

  // Draw a vertical line
  glBegin(GL_LINES);
  glVertex2i(xc, yc);
  glVertex2i(xc, yc-60);
  glEnd();

  // Draw 2 points on either side 
  // of the line
  glBegin(GL_POINTS);
  // A big dot made of 4 small dots
  // to the left of the line
  glVertex2i(xc-20, yc-20);
  glVertex2i(xc-19, yc-20);
  glVertex2i(xc-20, yc-19);
  glVertex2i(xc-19, yc-19);

  // A big dot made of 4 small dots
  // to the right of the line
  glVertex2i(xc+20, yc-20);
  glVertex2i(xc+21, yc-20);
  glVertex2i(xc+21, yc-19);
  glVertex2i(xc+20, yc-19);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white*/
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("Smiley :-)");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(smiley);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}

Output
=====


Written by

Friday, December 12, 2014

Static Clock using Bresenham's Circle Drawing in OpenGL

/* To draw a static clock using Bresenham's circle drawing algorithm */
#include <stdio.h>
#include <GL/glut.h>

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a static clock
void static_clock(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw a circle
  bresenham_circle(radius);
  glBegin(GL_LINES);
  // Minute hand
  glVertex2i(xc, yc);
  glVertex2i(xc, yc+95);
  // Hour hand
  glVertex2i(xc, yc);
  glVertex2i(xc+30, yc+50);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white */
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("bresenham_circle");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(static_clock);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}
Output















Written by 

Wednesday, December 10, 2014

Bresenham's Circle Drawing Algorithm using OpenGL

This program is to draw two concentric circles using bresenham's circle drawing algorithm with center (320, 240) and radii of circles as 100 and 200.

#include <stdio.h>
#include <math.h>
#include <GL/glut.h>

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
  glFlush();
}

// Function to draw two concentric circles
void concentric_circles(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius1 = 100, radius2 = 200;
  bresenham_circle(radius1);
  bresenham_circle(radius2);
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,0);
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);
  /* glViewport(0 , 0 , 640 , 480); */
  /* glMatrixMode(GL_PROJECTION); */
  /* glLoadIdentity(); */
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("bresenham_circle");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(concentric_circles);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}

Written by

Generalized Bresenham's Line Drawing Algorithm using OpenGL

Bresenham's line algorithm is an algorithm that determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures[source]. For more visit this website.
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glut.h>

/* Function that returns -1,0,1 depending on whether x */
/* is <0, =0, >0 respectively */
#define sign(x) ((x>0)?1:((x<0)?-1:0))

/* Function to plot a point */
void setPixel(GLint x, GLint y) {
  glBegin(GL_POINTS);
  glVertex2i(x,y);
  glEnd();
}

/* Generalized Bresenham's Algorithm */
void bres_general(int x1, int y1, int x2, int y2)
{
  int dx, dy, x, y, d, s1, s2, swap=0, temp;

  dx = abs(x2 - x1);
  dy = abs(y2 - y1);
  s1 = sign(x2-x1);
  s2 = sign(y2-y1);

  /* Check if dx or dy has a greater range */
  /* if dy has a greater range than dx swap dx and dy */
  if(dy > dx){temp = dx; dx = dy; dy = temp; swap = 1;}

  /* Set the initial decision parameter and the initial point */
  d = 2 * dy - dx;
  x = x1;
  y = y1;

  int i;
  for(i = 1; i <= dx; i++)
  {
    setPixel(x,y);
    
    while(d >= 0) 
    {
      if(swap) x = x + s1;
      else 
      {
        y = y + s2;
        d = d - 2* dx;
      }
    }
    if(swap) y = y + s2;
    else x = x + s1;
    d = d + 2 * dy;
  }
  glFlush();
}

/* Function to draw a rhombus inscribed in a rectangle and roll */
/* number printed in it */
void draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
 
  /* Draw rectangle */
  bres_general(20,40,620,40);
  bres_general(620,40,620,440);
  bres_general(620,440,20,440);
  bres_general(20,440,20,40);

  /* Draw rhombus */
  bres_general(320,440,20,240);
  bres_general(20,240,320,40);
  bres_general(320,40,620,240);
  bres_general(620,240,320,440);

  /* 1 */
  bres_general(250,150,250,250);
  /* 0 */
  bres_general(300,150,300,250);
  bres_general(300,250,400,250);
  bres_general(400,250,400,150);
  bres_general(400,150,300,150);

  glFlush();
}

void init() {  
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Green Window");
  glClearColor(0.0,0.0,0.0,0);
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0,640,0,480);
  
}

int main(int argc, char **argv) 
{
  glutInit(&argc, argv);
  init();
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}


Written by