2014年6月6日 星期五

運鏡喔


#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float angleX =0,angle2=0,angle3=0,angleY=0;
float eyeX=0.2,eyeY=0.2,eyeZ=0.7, objX=0, objY=0, objZ=0, upX=1, upY=0, upZ=0;
float theSize=0.5;
void timerRotateShow(int t)
{
//if(t%10==0) glClearColor((rand()%255/255.0), (rand()%255/255.0), (rand()%255/255.0), (rand()%255/255.0));
glutTimerFunc(30,timerRotateShow, t+1);
eyeX=cos(t/100.0*3.1415);
eyeZ=sin(t/100.0*3.1415);
//eyeY-=0.005;
theSize+=0.005;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle2+=5;
if(key=='4') angle2-=5;
if(key=='5') angle3+=3;
if(key=='6') angle3-=3;
if(key=='0') angleY+=5;
if(key==' ') 
{
eyeX-=0.01;
eyeY-=0.01;
}
if(key=='o')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
if(key=='r')
{
glutTimerFunc(0, timerRotateShow, 0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
   gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);  
glScalef(theSize, theSize, theSize);     
   glTranslatef(0,0.08,0);
   glColor3f(1,0.76,0.76);
glRotatef(angleX, 0,1,0);
glRotatef(angleY,0,0,1);
/*body*/
glPushMatrix();
glScalef(1.2,0.8,1);
glutSolidSphere(0.25,30,30);
glPopMatrix();
/*head*/
        glPushMatrix();
        glTranslatef(0,0.5,0);
            glutSolidSphere(0.35,30,30);
        glPopMatrix();
/*leftshoudler*/
glPushMatrix();
        glTranslatef(-0.3,0.1,0);
            glutSolidSphere(0.15,30,30);
//leftarm
glPushMatrix();
   glTranslatef(-0.05,-0.04,0);
glRotatef(-angle2, 1,0,0);
glRotatef(-angle3, 0,0,1);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(0.2,3,0.3);
glutSolidCube(0.2);
glPopMatrix();
//lefthand
glPushMatrix();
glTranslatef(-0.07,-0.3,0);
glScalef(1.3,0.5,0.5);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
        glPopMatrix();
        /*rightshoudler*/
glPushMatrix();
        glTranslatef(0.3,0.1,0);
            glutSolidSphere(0.15,30,30);
            //rightarm
            glPushMatrix();
                glTranslatef(0.05,-0.04,0);
glRotatef(angle2, 1,0,0);
glRotatef(angle3, 0,0,1);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(0.2,3,0.3);
glutSolidCube(0.2);
glPopMatrix();
//righthand
glPushMatrix();
glTranslatef(0.07,-0.3,0);
glScalef(1.3,0.5,0.5);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
        glPopMatrix();
        /*peepee*/
        glPushMatrix();
        glTranslatef(0,-0.15,0);
        glScalef(0.6,1,1);
            glutSolidCube(0.2);
        glPopMatrix();
/*downpee*/
glPushMatrix();
            glTranslatef(0,-0.28,0);
            glScalef(1.2,0.5,1);
glutSolidSphere(0.15,30,30);
glPopMatrix();
/**/
glPushMatrix();
   glTranslatef(-0.15,-0.28,0);
glutSolidSphere(0.08,30,30);
glPopMatrix();
/**/
glPushMatrix();
   glTranslatef(0.15,-0.28,0);
glutSolidSphere(0.08,30,30);
glPopMatrix();
/*leftthigh*/
glPushMatrix();
            glTranslatef(-0.15,-0.25,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(0.5,0.8,0.5);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//KNEE
   glTranslatef(0,-0.1,0);
glutSolidSphere(0.05,30,30);
glPushMatrix();//LEFTLEG
   glTranslatef(0,0,0);
   glPushMatrix();
   glRotatef(-angle2+5, 0.5,0,0);
   glTranslatef(0,-0.1,0);
glScalef(0.5,1.5,0.5);
glutSolidCube(0.2);
glPushMatrix();//FOOT
glTranslatef(0,-0.1,-0.15);
      glScalef(1.58,0.5,2.8);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
/*rightthigh*/
glPushMatrix();
            glTranslatef(0.15,-0.25,0);
glRotatef(-angle2, 1,0,0);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(0.5,0.8,0.5);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//KNEE 
   glTranslatef(0,-0.1,0);
glutSolidSphere(0.05,30,30);
glPushMatrix();//RIGHTLEG
glRotatef(angle2+5, 1,0,0);
   glTranslatef(0,-0.1,0);
   glScalef(0.5,1.5,0.5);
glutSolidCube(0.2);
glPushMatrix();//FOOT
glTranslatef(0,-0.1,-0.15);
      glScalef(1.58,0.5,2.8);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
/*righteye*/
glPushMatrix();
glTranslatef(0.15,0.5,-0.2);
glColor3f(0.25,0.41,1);
glutSolidSphere(0.15,30,30);
  glPopMatrix();
  /*righteye*/
  glPushMatrix();
glTranslatef(-0.15,0.5,-0.2);
glColor3f(0.25,0.41,1);
glutSolidSphere(0.15,30,30);
  glPopMatrix();
 
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("homework robot");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myLight();
glutMainLoop();
}


沒有留言:

張貼留言