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

No comments:

Post a Comment