2020年6月16日 星期二

<插 大家都好會插 插>

#include <GL/glut.h>
///int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angle[20]={};///全部先放0 

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glTranslatef( angle[11]/100.0, angle[12]/100.0, angle[13]/100.0 );
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[2], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[3], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int angleID=0;
int oldX=0, oldY=0
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
void readAll()
{
    if(fin==NULL) fin=fopen("motion.txt", "r" );
    for(int i=0; i<20; i++){
        ///scanf(  "%d", &a[i]);
        fscanf(fin,"%d", &angle[i]);
    }
}
int oldAngle[20];
int newAngle[20];
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    if(t==0){
        if(fin==NULL) fin=fopen("motion.txt", "r");
        for(int i=0; i<20; i++){
            fscanf(fin, "%d", &newAngle[i]);
        }
    }
    float alpha = (t%30)/30.0; 
    if( t%30==0 ){
        for(int i=0; i<20; i++){
            oldAngle[i] = newAngle[i];
            fscanf(fin, "%d", &newAngle[i]);
        }
    }
    for(int i=0; i<20; i++){
        angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];
        ///printf(" %d", angle[i]);
    }
    ///readAll();
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='a') angleID=11;
    if(key=='r'){
        readAll();///TODO5:
        glutPostRedisplay();
    }else if(key=='p'){
        glutTimerFunc(20, timer, 0);
    }else if(key=='s'){///Week161
        void saveAll();
       saveAll();
    }
}

void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
}
void saveAll()
{
    if(fout==NULL) fout=fopen("motion.txt", "w+");
    for(int i=0; i<20; i++){ ///Week1603
        printf("%d ", angle[i]);
        fprintf(fout, "%d ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    if(angleID==11){ ///Week1603
        angle[angleID+1] += (oldY-y); ///Week1603
    }
    oldX=x; oldY=y; ///Week1603
    ///Week161 ///saveAll();
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);

glutMainLoop();
}







今天教內插

圖片裡選的是重點

沒有留言:

張貼留言