2015年6月10日 星期三

小連的課堂作業

Week16圖學進度

  1. 移動、旋轉、縮放
  2. 寫檔、讀檔
  3. 內插 (Interpolate) 動作
  4. 期末作業

使用內插讓機器人能擺出連續pose

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle[8]={},angleOld[8]={},angleNew[8]={}, oldX=0, oldY=0,a=0;
int angleID=1;
void drawBody()
{
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    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.5, 0, 0);
        glRotatef(angle[1], 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle[3], 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle[4], 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void readNew()
{
    if(fin==NULL)fin=fopen("02160013.txt","r");
    for(int i=0;i<8;i++)
    {
        angleOld[i]=angleNew[i];
        fscanf(fin,"%f",&angleNew[i]);
        printf("%f ",angleNew[i]);
    }
    printf("\n");
}

void motion(int x, int y)
{
    angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timerPlay(int t)
{
    glutTimerFunc(20, timerPlay, t+1);
    if(t%50==0) readNew();
    a=(t%50)/50.0;
    for(int i=0;i<8;i++)
    {
        angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
    }
    glutPostRedisplay();
}

void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
    if(key=='p')glutTimerFunc(10,timerPlay,0);
   
if(key=='s')
    {
        if(fout==NULL)fout=fopen("02160013.txt","w+");
        for(int i=0;i<8;i++)
        {
            printf("%.1f ",angle[i]);
            fprintf(fout,"%.1f ",angle[i]);
        }
        fprintf(fout,"\n");
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02160013");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}



讓機器人能自己做動作

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle[10]={}, oldX=0, oldY=0;
int angleID=1;
void drawBody()
{
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    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.5, 0, 0);
        glRotatef(angle[1], 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle[3], 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle[4], 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{
    angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
    if(fout==NULL)fout=fopen("02160013.txt","w+");
    for(int i=0;i<10;i++)
    {
        printf("%f ",angle[i]);
        fprintf(fout,"%f ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timerPlay(int t)
{
    glutTimerFunc(500, timerPlay, t+1);
    if(fin==NULL)fin=fopen("02160013.txt","r");
    for(int i=0;i<10;i++)
    {
        fscanf(fin,"%f",&angle[i]);
        printf("%f ",angle[i]);
    }
    glutPostRedisplay();
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
    if(key=='p')glutTimerFunc(10,timerPlay,0);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02160013");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}



複習上次的T-R-T(四個關節)



#include <GL/glut.h>float angle[10]={}, oldX=0, oldY=0;
int angleID=0;
void drawBody()
{
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    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.5, 0, 0);
        glRotatef(angle[1], 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle[3], 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle[4], 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{   angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
}
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);
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02160013");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}





複習上禮拜的T-R-T



#include <Gl/glut.h>float angle1=0;void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(0,0,1);
        glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(0.5,0.15,0);
            glRotatef(angle1,0,0,1);
            glTranslatef(0.3,0,0);
            glScalef(1,0.3,0.3);
            glColor3f(1,0,0);
            glutSolidSphere(0.3,30,30);
        glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x,int y)
{
    angle1=x;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160013");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}



沒有留言:

張貼留言