Thursday, 3 December 2015

3d tea pot

#include <GL\glut.h>

GLfloat xRotated, yRotated, zRotated;
GLdouble size=1;


void display(void)
{

    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();
    // traslate the draw by z = -4.0
    // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
    glTranslatef(0.0,0.0,-4.5);
    // Red color used to draw.
    glColor3f(0.8, 0.2, 0.1);
    // changing in transformation matrix.
    // rotation about X axis
    glRotatef(xRotated,1.0,0.0,0.0);
    // rotation about Y axis
    glRotatef(yRotated,0.0,1.0,0.0);
    // rotation about Z axis
    glRotatef(zRotated,0.0,0.0,1.0);
    // scaling transfomation
    glScalef(1.0,1.0,1.0);
    // built-in (glut library) function , draw you a Teapot.
    glutSolidTeapot(size);
    // Flush buffers to screen
   
    glFlush();      
    // sawp buffers called because we are using double buffering
   // glutSwapBuffers();
}

void reshapeFunc(int x, int y)
{
    if (y == 0 || x == 0) return;  //Nothing is visible then, so return
    //Set a new projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //Angle of view:40 degrees
    //Near clipping plane distance: 0.5
    //Far clipping plane distance: 20.0
   
    gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);

    glViewport(0,0,x,y);  //Use the whole window for rendering
}

void idleFunc(void)
{

     yRotated += 0.01;
   
    display();
}


int main (int argc, char **argv)
{
    //Initialize GLUT
    glutInit(&argc, argv);
    //double buffering used to avoid flickering problem in animation
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    // window size
    glutInitWindowSize(400,350);
    // create the window
    glutCreateWindow("Teapot Rotating Animation");
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    xRotated = yRotated = zRotated = 30.0;
     xRotated=33;
     yRotated=40;
    glClearColor(0.0,0.0,0.0,0.0);
    //Assign  the function used in events
    glutDisplayFunc(display);
   glutReshapeFunc(reshapeFunc);
    glutIdleFunc(idleFunc);
    //Let start glut loop
    glutMainLoop();
    return 0;
}

ellipse

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

void init()
{
glClearColor(1,1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-300,300,-200,200);

}


void ellipse()
{
        int xc=20, yc=20, rx=60, ry=20;
       
float rxSq = rx * rx;
        float rySq = ry * ry;
        float x = 0, y = ry, p;
        float px = 0, py = 2 * rxSq * y;
       
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5);
        glColor3f(0,0,1);
        glBegin(GL_POINTS);

       
          glVertex2f(xc+x,yc-y);
          glVertex2d(xc-x,yc-y);
          glVertex2d(xc-x,yc+y);
       
glEnd();

        p = rySq - (rxSq * ry) + (0.25 * rxSq);
        while (px < py)
        {
                        x++;
                        px = px + 2 * rySq;                  
  if (p < 0)
                        p = p + rySq + px;
                        else
                        {
                        y--;
                        py = py - 2 * rxSq;
                        p = p + rySq + px - py;
                        }
                         glBegin(GL_POINTS);

                          glVertex2d(xc+x,yc+y);
                          glVertex2d(xc+x,yc-y);
                          glVertex2d(xc-x,yc-y);
                          glVertex2d(xc-x,yc+y);
                         glEnd();

        }

        p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;

        while (y > 0)
        {
                y--;
                py = py - 2 * rxSq;
                if (p > 0)
                p = p + rxSq - py;
                else
                {
                x++;
                px = px + 2 * rySq;
                p = p + rxSq - py + px;
                }
               glBegin(GL_POINTS);

          glVertex2d(xc+x,yc+y);
          glVertex2d(xc+x,yc-y);
          glVertex2d(xc-x,yc-y);
          glVertex2d(xc-x,yc+y);
         glEnd();
         glFlush();
             
       
        }
}



int main(int argc, char **argv)

{

         glutInit(&argc,argv);

         glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
         glutInitWindowSize(640,480);
         glutInitWindowPosition(0,0);
         glutCreateWindow("Ellipse");
init();
         glutDisplayFunc(ellipse);
         glutMainLoop();

         return 0;

rotating cube

#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

// ----------------------------------------------------------
// Function Prototypes
// ----------------------------------------------------------
void display();
void specialKeys();

// ----------------------------------------------------------
// Global Variables
// ----------------------------------------------------------
double rotate_y=0;
double rotate_x=0;

// ----------------------------------------------------------
// display() Callback function
// ----------------------------------------------------------
void display(){

  //  Clear screen and Z-buffer
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  // Reset transformations
  glLoadIdentity();

  // Other Transformations
  // glTranslatef( 0.1, 0.0, 0.0 );      // Not included
  // glRotatef( 180, 0.0, 1.0, 0.0 );    // Not included

  // Rotate when user changes rotate_x and rotate_y
  glRotatef( rotate_x, 1.0, 0.0, 0.0 );
  glRotatef( rotate_y, 0.0, 1.0, 0.0 );

  // Other Transformations
  // glScalef( 2.0, 2.0, 0.0 );          // Not included

  //Multi-colored side - FRONT
  glBegin(GL_POLYGON);

  glColor3f( 1.0, 0.0, 0.0 );     glVertex3f(  0.5, -0.5, -0.5 );      // P1 is red
  glColor3f( 0.0, 1.0, 0.0 );     glVertex3f(  0.5,  0.5, -0.5 );      // P2 is green
  glColor3f( 0.0, 0.0, 1.0 );     glVertex3f( -0.5,  0.5, -0.5 );      // P3 is blue
  glColor3f( 1.0, 0.0, 1.0 );     glVertex3f( -0.5, -0.5, -0.5 );      // P4 is purple

  glEnd();

  // White side - BACK
  glBegin(GL_POLYGON);
  glColor3f(   1.0,  1.0, 1.0 );
  glVertex3f(  0.5, -0.5, 0.5 );
  glVertex3f(  0.5,  0.5, 0.5 );
  glVertex3f( -0.5,  0.5, 0.5 );
  glVertex3f( -0.5, -0.5, 0.5 );
  glEnd();

  // Purple side - RIGHT
  glBegin(GL_POLYGON);
  glColor3f(  1.0,  0.0,  1.0 );
  glVertex3f( 0.5, -0.5, -0.5 );
  glVertex3f( 0.5,  0.5, -0.5 );
  glVertex3f( 0.5,  0.5,  0.5 );
  glVertex3f( 0.5, -0.5,  0.5 );
  glEnd();

  // Green side - LEFT
  glBegin(GL_POLYGON);
  glColor3f(   0.0,  1.0,  0.0 );
  glVertex3f( -0.5, -0.5,  0.5 );
  glVertex3f( -0.5,  0.5,  0.5 );
  glVertex3f( -0.5,  0.5, -0.5 );
  glVertex3f( -0.5, -0.5, -0.5 );
  glEnd();

  // Blue side - TOP
  glBegin(GL_POLYGON);
  glColor3f(   0.0,  0.0,  1.0 );
  glVertex3f(  0.5,  0.5,  0.5 );
  glVertex3f(  0.5,  0.5, -0.5 );
  glVertex3f( -0.5,  0.5, -0.5 );
  glVertex3f( -0.5,  0.5,  0.5 );
  glEnd();

  // Red side - BOTTOM
  glBegin(GL_POLYGON);
  glColor3f(   1.0,  0.0,  0.0 );
  glVertex3f(  0.5, -0.5, -0.5 );
  glVertex3f(  0.5, -0.5,  0.5 );
  glVertex3f( -0.5, -0.5,  0.5 );
  glVertex3f( -0.5, -0.5, -0.5 );
  glEnd();

  glFlush();
  glutSwapBuffers();

}

// ----------------------------------------------------------
// specialKeys() Callback Function
// ----------------------------------------------------------
void specialKeys( int key, int x, int y ) {

  //  Right arrow - increase rotation by 5 degree
  if (key == GLUT_KEY_RIGHT)
    rotate_y += 5;

  //  Left arrow - decrease rotation by 5 degree
  else if (key == GLUT_KEY_LEFT)
    rotate_y -= 5;

  else if (key == GLUT_KEY_UP)
    rotate_x += 5;

  else if (key == GLUT_KEY_DOWN)
    rotate_x -= 5;

  //  Request display update
  glutPostRedisplay();

}

// ----------------------------------------------------------
// main() function
// ----------------------------------------------------------
int main(int argc, char* argv[]){

  //  Initialize GLUT and process user parameters
  glutInit(&argc,argv);

  //  Request double buffered true color window with Z-buffer
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  // Create window
  glutCreateWindow("Awesome Cube");

  //  Enable Z-buffer depth test
  glEnable(GL_DEPTH_TEST);

  // Callback functions
  glutDisplayFunc(display);
  glutSpecialFunc(specialKeys);

  //  Pass control to GLUT for events
  glutMainLoop();

  //  Return to OS
  return 0;

}

road side

#include<GL/glut.h>

void road()
{

//SKY

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glPointSize(5);
glColor3f(0,0,1);

glVertex2f(-1,1);
glVertex2f(1,1);

glColor3f(1,1,1);
glVertex2f(1,0);
glVertex2f(-1,0);
glEnd();

//Buildings

glBegin(GL_QUADS);
glPointSize(5);
glColor3f(1,1,0);

glVertex2f(-1,0.5);
glVertex2f(-0.167,0.5);
glVertex2f(-0.167,0);
glVertex2f(-1,0);
glEnd();



//Road

glBegin(GL_QUADS);
glColor3f(1,0,0);

glVertex2f(-1,0);
glVertex2f(1,0);
    //glColor3f(1,1,1);
glVertex2f(1,-0.5);
glVertex2f(-1,-0.5);


glEnd();

//CAR

glBegin(GL_QUADS);
glColor3f(0,0,0);

glVertex2f(-1,0);
glVertex2f(1,0);
glColor3f(1,1,1);
glVertex2f(1,-0.5);
glVertex2f(-1,-0.5);


glEnd();


//strips
glBegin(GL_LINES);
glColor3f(1,1,1);

glVertex2f(-1,-0.25);
glVertex2f(-0.88,-0.25);

glVertex2f(-0.66,-0.25);
glVertex2f(-0.44,-0.25);


glVertex2f(-0.22,-0.25);
glVertex2f(0,-0.25);

glVertex2f(0,-0.25);
glVertex2f(0.22,-0.25);

glVertex2f(0.44,-0.25);
glVertex2f(0.66,-0.25);


glVertex2f(0.88,-0.25);
glVertex2f(1,-0.25);



glEnd();



//red light

//pole
glBegin(GL_QUADS);
glColor3f(0,0,0);

glVertex2f(0.75,0);
glVertex2f(0.75,0.300);
glVertex2f(0.80,0.300);
glVertex2f(0.80,0);

glEnd();

//display

glBegin(GL_QUADS);
glColor3f(0,0,0);

glVertex2f(0.60,0.300);
glVertex2f(0.95,0.300);
glVertex2f(0.95,0.700);
glVertex2f(0.60,0.700);

glEnd();

//lights

glBegin(GL_POINTS);
glPointSize(100);

    glColor3f(1,0,0);
glVertex2f(0.70,0.280);

glColor3f(1,1,0);
glVertex2f(0.70,0.250);

glColor3f(0,1,0);
glVertex2f(0.70,0.0125);
glEnd();


//greenry

glBegin(GL_QUADS);
glColor3f(0,1,0);
glVertex2f(-1,-0.5);
glVertex2f(1,-0.5);
glVertex2f(1,-1);
glVertex2f(-1,-1);


glEnd();
glFlush();
}

void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(10,10);
glutCreateWindow("Road_Side_View");
glutDisplayFunc(road);
glutMainLoop();
}

square move

#include<GL/glut.h>

int a=0;
int start=0;

init()
{
glClearColor(1,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-300,300,-200,200);

}

void disp()
{


glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,1,1);
//Square


glPushMatrix();

glTranslatef(a,0,0);

glBegin(GL_QUADS);

glVertex2f(0,0);
glVertex2f(50,0);
glVertex2f(50,50);
glVertex2f(0,50);
glEnd();
glPopMatrix();

glFlush();

}


void time(int v)
{

if(start==1)
{

a=a+20;

if (a>360)
{
a=0;
}


glutPostRedisplay();
}

glutTimerFunc(100,time,0);

}

//Mouse Func

void mouse(int button,int state,int x, int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
start=0;
}

  if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
   {
start=1;
    }
}

void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(600,400);
glutInitWindowPosition(30,10);
glutCreateWindow("Mouse");
init();
glutMouseFunc(mouse);
glutTimerFunc(100,time,0);
glutDisplayFunc(disp);
glutMainLoop();

}

bitmap


 #include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<stdlib.h>
 GLubyte rasters[]={
         0xc0,0xf0,
         0xc0,0xf0,
         0xc1,0xb0,
         0xc1,0xb0,
         0xc3,0x30,
         0xc3,0x30,
         0xc6,0x30,
         0xc6,0x30,
         0xd8,0x30,
         0xd8,0x30,
         0xf0,0x30,
         0xf0,0x30
       };
 GLubyte rasters1[]={
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xff,0xf0,
         0xff,0xf0,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xff,0xf0,
         0xff,0xf0
       };
GLubyte rasters2[]={
         0x06,0x00,
         0x06,0x00,
         0x1f,0x80,
         0x19,0x80,
         0x39,0xc0,
         0x39,0xc0,
         0x70,0xe0,
         0x701,0e00,
         0x60,0x60,
         0xe0,0x70,
         0xe0,0x70,
         0xc0,0x30
       };
GLubyte rasters3[]={
         0xff,0xe0,
         0xff,0xf0,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xff,0xf0,
         0xff,0xe0
       };
GLubyte rasters4[]={
         0xff,0xf0,
         0xff,0xf0,
         0xc0,0x00,
         0xc0,0x00,
         0xc0,0x00,
         0xff,0xc0,
         0xff,0xc0,
         0xc0,0x00,
         0xc0,0x00,
         0xc0,0x00,
         0xff,0xf0,
         0xff,0xf0
       };
GLubyte rasters5[]={
         0xc0,0x00,
         0xc0,0x00,
         0xc0,0x00,
         0xc0,0x00,
         0xc0,0x00,
         0xff,0xf0,
         0xff,0xf0,
         0xc0,0x30,
         0xc0,0x30,
         0xc0,0x30,
         0xff,0xf0,
         0xff,0xf0
       };
     
void init(void)
{
     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
      glClearColor(0.0,1.0,1.0,0.0);
}
void reshape(int w,int h)
{
     glViewport(0,0,(GLsizei) w,(GLsizei) h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(0,w,0,h,-1.0,1.0);
     glMatrixMode(GL_MODELVIEW);
     //glLoadIdentity();
}
void display(void)
{
      // glClearColor(0.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(0.0,0.0,1.0);
     glRasterPos2i(100,100);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters1);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters2);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters3);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters4);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters4);
     glBitmap(12,12,0.0,0.0,15.0,0.0,rasters5);
     glFlush();
}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(100,100);
    glutCreateWindow("bitmapping");
    glutReshapeFunc(reshape);
    init();
    glutDisplayFunc(display);
    glutMainLoop();
}  

midpoint

#include<stdio.h>
#include<conio.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<stdlib.h>
int r,x,y;
void disp()
{
       glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
     glPointSize(2.0); // to set size to pixels
     glBegin(GL_POINTS);
int p;
x=0;y=r;
p=1-r;
while(x<=y)
{
        if(p<0)
        {
               x=x+1;
               y=y;
               p=p+2*(x)+1;
               glVertex2f(x,y);
               glVertex2f(x,-y);
               glVertex2f(-x,-y);
               glVertex2f(-x,y);
               glVertex2f(y,x);
               glVertex2f(y,-x);
               glVertex2f(-y,-x);
               glVertex2f(-y,x);
               printf("x=%d,y=%d \n",x,y);
        }
        else
        {
            x=x+1;
            y=y-1;
            p=p+2*(x-y);
            glVertex2f(x,y);
            glVertex2f(x,-y);
            glVertex2f(-x,-y);
            glVertex2f(-x,y);
            glVertex2f(y,x);
            glVertex2f(y,-x);
            glVertex2f(-y,-x);
            glVertex2f(-y,x);
            printf("x=%d,y=%d \n",x,y);
        } 
}

glEnd();
glFlush();

}
void abc()
{
     glClearColor(0.0,0.0,0.0,1.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(-100.0,100.0,-100.0,100.0,-1.0,1.0);
     }              
int  main()
{
       printf("Enter radius");
      scanf("%d",&r);
      glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
      glutInitWindowSize(500,500);
      glutCreateWindow("Bresenham's");
      glutInitWindowPosition(50,50);
          abc();
      glutDisplayFunc(disp);
      glutMainLoop();
      return 0;
      }


bresenham

# include <stdio.h>
  # include <conio.h>
 #include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
int x1, y1, x2, y2,p,x,y;
float m;

void disp ()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
     glPointSize(5.0); // to set size to pixels
     glBegin(GL_POINTS);
     int dx= x2-x1;
     int dy=y2-y1;
     printf("%d\n",dx);
     printf("%d\n",dy);
m=dy/dx;
printf("%d\n",m);

   x = x1;
    y = y1;
    if(m<1)
    {
         
    p = 2 * (dy) - (dx);
printf("%d\n",p);
    while(x <= x2)
    {
      if(p < 0)
      {
        x=x+1;
        y=y;
        p = p + 2 * (dy);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dy - dx);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
     }
}
}
  else
  {
      p=2*dx-dy;
while(x <= x2)
    {
      if(p < 0)
      {
        x=x;
        y=y+1;
        p = p + 2 * (dx);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dx- dy);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
     }
}
}
     glEnd();
     glFlush();
     }
      void abc()
{
     glClearColor(0.0,0.0,0.0,1.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(-100.0,100.0,-100.0,100.0,-1.0,1.0);
     }              
int  main()
{
       printf("Enter the starting points");
      scanf("%d%d",&x1,&y1);
      printf("Enter the ending points points");
      scanf("%d%d",&x2,&y2);
      glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
      glutInitWindowSize(500,500);
      glutCreateWindow("LINE");
      glutInitWindowPosition(50,50);
          abc();
      glutDisplayFunc(disp);
      glutMainLoop();
      return 0;
      }