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