Social Icons

Friday, 2 May 2014

Solar System Using Opengl IN Graphics


#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

//float x=0.0;
float a[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//angle of Rotation
float speed[10]={0.0,0.06,0.05,0.04,0.03,0.02,0.01,0.008,0.007,0.006};//speed of Rotation
float radius[10]=   {5.0,1.5,1.2,1.0,2.0,1.3,1.0,1.0,1.0,1.0};//planet Size
float distancex[10]={0.0,7.0,10.0,13.0,17.0,22.0,26.0,29.0,32.0,35.0};//DIstance Between Planets

  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat skyblue[] = { 0.2, 0.2, 1.0, 1.0 };
  GLfloat mitty[] = { 0.8, 0.8, 1.0, 1.0 };

void display()
{
       int i;
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);

        for(i=0;i<10;i++)
        {
            glPushMatrix();
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,white);
             glutWireTorus(0.001,distancex[i],50,50);
            if(i==4)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,red);
            else if(i==3)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,blue);
            else if(i==0)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,yellow);
            else
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mitty);
            glRotatef(a[i],0.0,0.0,1.0);
            glTranslatef(distancex[i],0.0,0.0);
             if(i==6)
            {
                glRotatef(-45,1.0,1.0,0.0);
                glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,green);
                glutWireTorus(0.2,1.5,20,20);
            }

            glPushMatrix();
            glRotatef(a[i],0.0,1.0,0.0);
            glutSolidSphere(radius[i],30,10);
            glPopMatrix();

              glPopMatrix();
          }

      glutSwapBuffers();
}

void timer(int vaulue)
{
    int i;
    for(i=1;i<10;i++)
       {
           a[i] = a[i]+speed[i];
       }
       glutPostRedisplay();
       glutTimerFunc(1000/500,timer,1);
}

void reshape(GLint w, GLint h) {
   glViewport(0, 0, w, h);
 // glClearColor(1.0,1.0,1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
gluLookAt(5.0,-5.0,10.0,.0,0.0,0.0,0.0,0.0,1.0);
  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
    // width is smaller, so stretch out the height
    glOrtho(-40.0, 40.0, -40.0/aspect, 40.0/aspect, -40.0, 40.0);
  } else {
    // height is smaller, so stretch out the width
    glOrtho(-40.0*aspect, 40.0*aspect, -40.0, 40.0, -40.0, 40.0);
  }
}

void init() {
 GLfloat direction[] = { 5.0, 0.0, 0.0, 0.0 };
 GLfloat direction1[] = { -5.0, 0.0, 0.0, 0.0 };
 glMaterialf(GL_FRONT, GL_SHININESS, 50);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glLightfv(GL_LIGHT1, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT1, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}

// The usual application statup code.
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(600, 600);
  glutCreateWindow("Solar System Simulation");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
   glutTimerFunc(1000/10,timer,1);
  init();
  glutMainLoop();
}

Bouncing Ball In Opengl

#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1,u=0.0,d=0.0;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
 
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x+u,y+u);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
{

 if(d==0)
  {
  u=u+0.03;
  if(u>2.9)
  d=1;
  }
   if(d==1)
  {
  u=u-0.03;
  if(u<-2.9)
  d=0;
  }
 

 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
 
  glutMainLoop();
}

Moving Torus in opengl

#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;
float p=2.0;
void display()
{   p=p+0.03;

glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);
    glPushMatrix();
     
      glRotatef(p,1.0,0.0,0.0);
      glutWireTorus(3,5,30,40);
     
      glPopMatrix();
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);glVertex3f(0,0,0);glVertex3f(20,0,0);
glColor3f(1.0,1.0,0.0);glVertex3f(0,0,0);glVertex3f(0,20,0);
glColor3f(1.0,0.0,1.0);glVertex3f(0,0,0);glVertex3f(0,0,20);

glEnd();

   glFlush();
   glutPostRedisplay();
   //glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 glClearColor(0.0,0.0,0.0,1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60.0,4/3,1,40);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(7,7,20,0,0,0,0,1,0);



 /*glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);*/

}



/*
void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}


*/

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
 
   glutDisplayFunc(display);
  // glutReshapeFunc(reshape);
  // glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}

Moon in Opengl


#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;

void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);

glutSolidSphere(1.0,30,30);
GLfloat direction[]={1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0,GL_POSITION,direction);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
x=radius* cos(u);
y=0;
z=radius* sin(u);

gluLookAt(x,y,z,0,0,0,0,1,0);




   glFlush();
   glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 GLfloat yellow[]={1.0,1.0,0.0};  
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 //gluOrtho2D(0.0,640.0,0.0,480.0);
 glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);

}




void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}




int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Going for a SMILEY :-)");
 
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
   glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}
 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management