Social Icons

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




No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management