2016年5月31日 星期二

WEEK15陳心瑜

WEEK15

1.T-R-T小考
2.內插Interpolation
3.動作內插

TODO:T-R-T小考

程式碼:
glPushMatrix();
  glTranslatef(x,y,z);
  glRotatef(angle,x,y,z);
  glTranslatef(x,y,z);
  drawhand();
glPopMatrix();






















TODO:用Excel作內插

公式:new*alpha+old*(1-alpha)

選取A & C 製作圖表

















內插法


















TODO:畫圖Excel插入x*y圖


新增:C:\Users\USER\Desktop\freeglut\include
















新增:C:\Users\USER\Desktop\freeglut\lib

















新增咒語:winmm/freeglut/gui32/gdi32/opengl32

















1.motion()不要狂寫檔
2.keyboard()裡if(key=='s')
3.keyboard()的'r'讀一行,改裝't'先讀一行
4.加上timer,glutTimerFunc















程式碼:

#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0}, rotold[10]={0}, rotnew[10]={0};
int rotID=1, oldX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutWireCube(0.3);
        glPushMatrix();
            glTranslatef(0,0.2,0);
            glutWireCube(0.1);
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.15,0.1,0);
            glRotatef(rot[1],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot[2],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot[3],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);
        glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.15,0.1,0);
            glRotatef(-rot[4],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[5],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[6],0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        oldX=x;
    }
}void motion(int x, int y)
{
    rot[rotID] += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}

void timer(int t)
{
    float alpha=(t%10)/10.0;
    if(t%10==0)
    {
        if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++)
        {
            rotold[i]=rotnew[i];
            fscanf( fin, "%f ", &rotnew[i]);;
            printf( "%f ", rotnew[i]);;
        }
        fprintf(fin, "\n");
        printf("\n");
    }
    for(int i;i<10;i++)
    {
        rot[i]=rotnew[i]*alpha+rotold[i]*(1-alpha);
    }
    glutTimerFunc(100, timer, t+1);
    glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') rotID=1;
    if(key=='2') rotID=2;
    if(key=='3') rotID=3;
    if(key=='4') rotID=4;
    if(key=='5') rotID=5;
    if(key=='6') rotID=6;

    if(key=='r'){
        if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++)
        {
            fscanf( fin, "%f ", &rot[i]);;
            printf( "%f ", rot[i]);;
        }
        fprintf(fin, "\n");
        printf("\n");
    }
    if(key=='s')
    {
        if(fout==NULL) fout=fopen("motion.txt","w+");
        for(int i=0;i<10;i++)
        {
            fprintf( fout, "%.1f ", rot[i]);;
            printf( "%.1f ", rot[i]);;
        }
        fprintf(fout, "\n");
        printf("\n");
    }
    if(key=='t')
    {
        if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++)
        {
            fscanf( fin, "%f ", &rotnew[i]);;
            printf( "%f ", rotnew[i]);;
        }
        fprintf(fin, "\n");
        printf("\n");
        glutTimerFunc(100, timer, 0);
    }
    glutPostRedisplay();
}

int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();

}

't'->讀入位置
's'->存位置
'r'->播放

要先執行->然後拉位置->按S儲存->貼到Notepad++->再重新執行按T


沒有留言:

張貼留言