Computer Graphics Project "MINION" Source Code with Glut OpenGL

Hello !!! This time I post about computer graphics again. And yes, I will share a project which mainly used rotation and a little bit morphing. You can download this project here ( Download here to download glut and follow the instruction) .The character used in this project is well known. Well here’s the depiction of this project.




Minion !!! The eyes will rotate. And it’s smile will move up and down which give you a creepy smile hahaha.  So here’s the source code for this project :

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

#ifndef M_PI
#define M_PI 3.141592653589793
#endif

typedef struct {
  float x;
  float y;
} point2D_t;

typedef struct {
  float r;
  float g;
  float b;
} color_t;

////////////// OpenGL drawShape Functions ver 1 /////////////////
void setColor(float red,float green,float blue)
{
  glColor3f(red, green, blue);
}

void setColor(color_t col)
{
  glColor3f(col.r, col.g, col.b);
}

void drawDot(float x,float y)
{
  glBegin(GL_POINTS);
    glVertex2f(x, y);
  glEnd();
}

void drawLine(float x1, float y1, float x2, float y2)
{
  glBegin(GL_LINES);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
  glEnd();
}

void drawLine(point2D_t p1,point2D_t p2)
{
  drawLine(p1.x,p1.y,p2.x,p2.y);
}

//n: number of points
void drawPolyline(point2D_t pnt[],int n)
{
  int i;
  glBegin(GL_LINE_STRIP);
    for (i=0;i<n;i++) {
      glVertex2f(pnt[i].x, pnt[i].y);
    }
  glEnd();
}

//n: number of vertices
void drawPolygon(point2D_t pnt[],int n)
{
  int i;
  glBegin(GL_LINE_LOOP);
    for (i=0;i<n;i++) {
      glVertex2f(pnt[i].x, pnt[i].y);
    }
  glEnd();
}

// The function fillPolygon can fills only convex polygons
//n: number of vertices
void fillPolygon(point2D_t pnt[],int n,color_t color)
{
  int i;
  setColor(color);
  glBegin(GL_POLYGON);
    for (i=0;i<n;i++) {
      glVertex2f(pnt[i].x, pnt[i].y);
    }
  glEnd();
}

// The function gradatePolygon can fills only convex polygons
// The vertices will be painted with corresponding given colors.
// The points inside the polygon will be painted with the mixed color.
//n: number of vertices
void gradatePolygon(point2D_t pnt[],color_t col[],int num)
{
  int i;
  glBegin(GL_POLYGON);
    for (i=0;i<num;i++) {
      setColor(col[i]);
      glVertex2f(pnt[i].x, pnt[i].y);
    }
  glEnd();
}

//////////// End of OpenGL drawShape Functions ver 1 ////////////

void userdraw(void);

void display(void)
{
  glClear( GL_COLOR_BUFFER_BIT);
  userdraw();
  glutSwapBuffers();
}

//////////////////////////////////////////////////////////////////
void drawcharX(float x,float y)
{
  drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y);
}

void drawcharY(float x,float y)
{
  drawLine(x+5,y,x+5,y+7);drawLine(x,y+12,x+5,y+7);drawLine(x+10,y+12,x+5,y+7);
}

void drawAxes(void)
{
  drawLine(-310,0,310,0);drawLine(310,0,300,5);drawLine(310,0,300,-5);
  drawcharX(300,-20);
  drawLine(0,-230,0,230);drawLine(0,230,5,220);drawLine(0,230,-5,220);
  drawcharY(-20,220);
}

//////////////////////////////////////////////////////////////////
point2D_t interpolate(point2D_t a, point2D_t b, float r)
{
  // 0.0<=r<=1.0  AC:CB=r:(1-r)
  if (r<0.0) r=0.0;
  if (1.0<r) r=1.0;
  point2D_t c;
  float r1=1.0-r;
  c.x=r1*a.x+r*b.x;
  c.y=r1*a.y+r*b.y;
  return c;
}

void makeSquare(point2D_t p[],float r)
{
  int i,j;
  for (i=0;i<5;i++) {
    p[5*i].x=r*cos((0.5*i+0.25)*M_PI);
    p[5*i].y=r*sin((0.5*i+0.25)*M_PI);
  }
  for (i=0;i<4;i++) {
    for (j=1;j<5;j++) {
      p[5*i+j]=interpolate(p[5*i],p[5*i+5],0.2*j);
    }
  }
}

void makePentagram(point2D_t p[],float r)
{
  int i,j;
  for (i=0;i<6;i++) {
    p[4*i].x=r*cos((0.8*i+0.5)*M_PI);
    p[4*i].y=r*sin((0.8*i+0.5)*M_PI);
  }
  for (i=0;i<5;i++) {
    for (j=1;j<4;j++) {
      p[4*i+j]=interpolate(p[4*i],p[4*i+4],0.25*j);
    }
  }
}

void make20gram(point2D_t p[],float r)
{
  int i;
  for (i=0;i<20;i++) {
    p[i].x=r*cos(i*M_PI*18/19.);
    p[i].y=r*sin(i*M_PI*18/19.);
  }
}

void userdraw(void)
{

  static int tick=0;
  int disp=(tick/100)%6;
  float rtick=(tick%100)/80.;
  point2D_t square[40];
  point2D_t pentagram[40];
  point2D_t ngram20[40];
  point2D_t shape[40];
  int i;
  makeSquare(square,150);
  makePentagram(pentagram,150);
  make20gram(ngram20,150);
  setColor(1,1,1);
  drawAxes();
  setColor(0,1,1);
  switch(disp) {
  case 0:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(square[i],pentagram[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  case 1:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(pentagram[i],ngram20[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  case 2:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(ngram20[i],square[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  case 3:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(square[i],ngram20[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  case 4:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(ngram20[i],pentagram[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  case 5:
    for(i=0;i<20;i++) {
      shape[i]=interpolate(pentagram[i],square[i],rtick);
    }
    drawPolygon(shape,20);
    break;
  default:
    break;
  }
  tick++;

}

int main(int argc, char **argv)
{
  glutInit(&argc,argv);
  glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
  glutInitWindowPosition(100,100);
  glutInitWindowSize(640,480);
  glutCreateWindow ("figures");
  glClearColor(0.0, 0.0, 0.0, 0.0);
  gluOrtho2D(-320., 320., -240.0, 240.0);
    // Define the dimensions of the Orthographic Viewing Volume
  glutIdleFunc(display); // idle event call back
  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}

Comments

  1. Hey. This code draws figures and not minions. It would be great if you could upload the code for minions :)

    ReplyDelete
    Replies
    1. i cant download the project from GDrive
      error -> We're sorry. You can't access this item because it is in violation of our Terms of Service.

      Delete

Post a Comment

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#