Social Icons

Showing posts with label OPENGL GRAPHICS. Show all posts
Showing posts with label OPENGL GRAPHICS. Show all posts

Friday, 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


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

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

Sunday, 23 March 2014

MOVING CIRCLE IN OPENGL


#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1;
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_TRIANGLE_STRIP);
  glVertex2f(1.0,0.0);
  glVertex2f(2.0,0.0);
  glVertex2f(2,0.5);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(-1.0,0.0);
  glVertex2f(-2.0,0.0);
  glVertex2f(-2,-0.5);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(0.0,-1.0);
  glVertex2f(0.0,-2.0);
  glVertex2f(0.5,-2.0);
  glEnd();
   glBegin(GL_TRIANGLE_STRIP);
  glVertex2f(0.0,1.0);
  glVertex2f(0.0,2.0);
  glVertex2f(-0.5,2.0);
  glEnd();
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x,y);
   }

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

void timer(int v)
 {
  if (spinning)
   {
 
 
    currentAngleOfRotation += 2.0;
      if(currentAngleOfRotation==360.0)
      {
   
  currentAngleOfRotation = -360.0;
     
 }
     glutPostRedisplay();
  }
  glutTimerFunc(1000/FPS, timer, v);
}

void mouse(int button, int state, int x, int y) {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    spinning = true;
  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
    spinning = false;
  }
}

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);
  glutMouseFunc(mouse);
  glutMainLoop();
}

CIRCLE USING OPENGL

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>
void display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
    // glColor3f(1.0,0.0,0.0);
   
     glPointSize(6.0);
     //glPointSize(6.0);
 
   
     int i;
     double angle,x,y;
     float PI=3.14;
     glBegin(GL_LINES);  
                            for(i=0;i<=300;i++)
                            {
                              angle=2*PI*i/300;
                              x=0.5*cos(angle);
                              y=0.5*sin(angle);
                              glVertex2d(x,y);
                            }
     glEnd();
                         
     glFlush();
}


int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
    glutDisplayFunc(display);
 
    glutMainLoop();
}

Saturday, 22 February 2014

spining cube in opengl

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;

static GLfloat currentAngleOfRotation = 0.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, -1.0, 1.0);
  } else {
 
    glOrtho(-2.0*aspect, 2.0*aspect, -2.0, 2.0, -1.0, 1.0);
  }
}
void display() {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 10.0, 1.0);
 // glRectf(-25.0, -25.0, 25.0, 25.0);
//  printf("f1=%d f2=%d",f1,f2);
 // glScalef(5.0,3.0,5.0);
 glTranslatef(0.3,0.2,0.0);
  glScalef(currentAngleOfRotation/120,currentAngleOfRotation/120,2);

glBegin(GL_QUADS);
    glColor3f(1.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();
// glColor3f(0.0, 1.0, 1.0);
glBegin(GL_QUADS);
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();

// glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
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();

// glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.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();
// glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.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();

//glColor3f(1.0, 0.0, 1.0);

glBegin(GL_QUADS);
glColor3f(1.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();
  glFlush();
  glutSwapBuffers();
}

void timer(int v) {
  if (spinning) {
    currentAngleOfRotation += 2.0;
    if (currentAngleOfRotation > 360.0) {
      currentAngleOfRotation -= 360.0;
    }
    glutPostRedisplay();
  }
  glutTimerFunc(1000/FPS, timer, v);
}
void mouse(int button, int state, int x, int y) {
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    spinning = true;
  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
    spinning = false;
  }
}
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
     glEnable(GL_DEPTH_TEST);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
  glutMouseFunc(mouse);
  glutMainLoop();
}




Thursday, 13 February 2014

QUAD IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_QUADS);
                       
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(200,150);
                        glColor3f(1.0,0.0,1.0);
                        glVertex2f(140,150);
                     
   //glVertex2f(140,200);
                        //glVertex2f(180,200);
                     
                     
        glColor3f(1.0,1.0,0.0);
                       
    //glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

QUAD STRIP IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_QUAD_STRIP);
                       
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                     
                        glColor3f(1.0,0.0,1.0);
                        glVertex2f(140,150);
                         glVertex2f(200,150);
   glVertex2f(140,200);
                        glVertex2f(180,200);
                     
                     
        glColor3f(1.0,1.0,0.0);
                       
    //glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

TRIANGLE FAN IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLE_FAN);
                         glVertex2f(140,150);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glColor3f(1.0,0.0,1.0);
                       
                      glVertex2f(200,120);
                       glColor3f(1.0,1.0,0.0);
                        glVertex2f(250,140);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

TRIANGLE STRIP IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLE_STRIP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       glVertex2f(200,150);
                        glVertex2f(150,200);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

Drawing figure using Line Strip

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
 
   
     glPointSize(15);
     glBegin(GL_LINE_STRIP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
 
    glutMainLoop();
}

DRAWING TRIANGLE USING LINE LOOP

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_LINE_LOOP);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                       // glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

DRAWING POINTS IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
 
   
     glPointSize(15);
     glBegin(GL_POINTS);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                        glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{

   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
 
    glutMainLoop();
}

CREATING A TRIANGLE IN OPENGL

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

void display()
{
glClearColor(1.0,1.0,1.0,0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
   
   
     glPointSize(15);
     glBegin(GL_TRIANGLES);
                        glVertex2f(90,100);
                        glVertex2f(180,100);
                        glVertex2f(140,150);
                        glVertex2f(200,150);
     glEnd();
     glFlush();
}

void init(void)
{


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.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 :-)");
    init();
glutDisplayFunc(display);
   
    glutMainLoop();
}

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management