2015年6月10日 星期三

00370915 蔡昀佑 Computer Graphics Week16 - fout & fin & Interpolate

#include<GL/glut.h>
#include<stdio.h>
FILE * fout = NULL;
FILE * fin = NULL;
float oldX=0, oldY=0;
float angle[48]={0};
int now = 0;
void timerPlay(int t)
{
    glutTimerFunc(10,timerPlay,0);
    if(fin==NULL) fin=fopen("a.txt","r");
    {
        for(int i=0; i<48 ;i++)
        {
            fscanf(fin,"%f",&angle[i]);
           // printf("%f ",angle[i]);
        }
    }
}

void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') now=1;
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='9') now=9;
    if(key=='p') glutTimerFunc(10,timerPlay,0);
    glutPostRedisplay();
}
void drawBody(){
    glPushMatrix();
    glScalef(0.35, 0.35, 0.35);
    glutSolidCube(1);
    glPopMatrix();
}
void drawUpArm(){
    glPushMatrix();
    glScalef(0.35, 0.14, 0.14);
    glutSolidCube(1);
    glPopMatrix();
}
void drawDownArm(){
    glPushMatrix();
    glScalef(0.35, 0.12, 0.12);
    glutSolidCube(1);
    glPopMatrix();
}
void drawHand(){
    glPushMatrix();
    glScalef(0.11, 0.11, 0.11);
    glutSolidCube(1);
    glPopMatrix();
}
void drawFinger(){
    glPushMatrix();
    glScalef(0.1, 0.02, 0.02);
    glutSolidCube(1);
    glPopMatrix();
}
void drawDownBody(){
    glPushMatrix();
    glScalef(0.3, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    ///Lighting
    glColor3ub(166,132,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
   // glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(0, 0, 0);
    glRotatef(angle[8], 1,0,0);
    glTranslatef(0, 0.2,0);
    drawBody();
        glPushMatrix();
            glTranslatef(0.1, 0.1, 0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.2, 0,0);
            drawUpArm();
            glPushMatrix();
                glTranslatef(0.12, 0, 0);
                glRotatef(angle[2], 0,0,1);
                glTranslatef(0.2, 0,0);
                drawDownArm();
                glPushMatrix();
                    glTranslatef(0.2, 0, 0);
                    glRotatef(angle[5], 0,1,0);
                    glTranslatef(0, 0,0);
                    drawHand();
                    glPushMatrix();
                        glTranslatef(0.01, 0.04, 0);
                        glRotatef(angle[7], 0,1,0);
                        glTranslatef(0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(0.01, 0.013, 0);
                        glRotatef(angle[7], 0,1,0);
                        glTranslatef(0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(0.01, -0.014, 0);
                        glRotatef(angle[7], 0,1,0);
                        glTranslatef(0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(0.01, -0.04, 0);
                        glRotatef(angle[7], 0,1,0);
                        glTranslatef(0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.1, 0.1, 0);
            glRotatef(-angle[3], 0,0,1);
            glTranslatef(-0.2, 0,0);
            drawUpArm();
            glPushMatrix();
                glTranslatef(-0.12, 0, 0);
                glRotatef(-angle[4], 0,0,1);
                glTranslatef(-0.2, 0,0);
                drawDownArm();

                glPushMatrix();
                    glTranslatef(-0.2, 0, 0);
                    glRotatef(-angle[6], 0,1,0);
                    glTranslatef(0, 0,0);
                    drawHand();
                    glPushMatrix();
                        glTranslatef(-0.01, 0.04, 0);
                        glRotatef(-angle[7], 0,1,0);
                        glTranslatef(-0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(-0.01, 0.013, 0);
                        glRotatef(-angle[7], 0,1,0);
                        glTranslatef(-0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(-0.01, -0.014, 0);
                        glRotatef(-angle[7], 0,1,0);
                        glTranslatef(-0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                    glPushMatrix();
                        glTranslatef(-0.01, -0.04, 0);
                        glRotatef(-angle[7], 0,1,0);
                        glTranslatef(-0.05, 0,0);
                        drawFinger();
                    glPopMatrix();
                glPopMatrix();

            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    drawDownBody();
    glFlush();
}
void motion(int x, int y)
{   angle[now] -= y - oldY;
    oldY=y; oldX=x;
    if(fout==NULL) fout=fopen("a.txt","w+");
    {
        for(int i=0; i<48 ;i++)
        {
            fprintf(fout,"%.1f\t",angle[i]);
        }
        fprintf(fout,"\n");
    }
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMainLoop();
}


沒有留言:

張貼留言