顯示具有 02160510_黃品鈞 標籤的文章。 顯示所有文章
顯示具有 02160510_黃品鈞 標籤的文章。 顯示所有文章

2015年6月10日 星期三

Week16 02160510黃品鈞

寫檔讀檔

#include <GL/glut.h>
#include <stdio.h>
float angle[10], oldX=0, oldY=0;
int angleID=1;
int now=0;
int i;
FILE* fout=NULL;
FILE* fin=NULL;
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()
{
    { ///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.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle[1], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle[2], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle[3], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle[4], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{   angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
    if(fout==NULL)fout=fopen("02160040.txt","w+");
    for(i=0;i<10;i++)
        {
            printf(   "%f ", angle[i]);
            fprintf(fout,"%f ",angle[i]);

        }
        printf("\n");
        fprintf(fout, "\n");
        }
void timerplay(int t)
{
    glutTimerFunc(500,timerplay,t+1);
    if(fin==NULL)fin=fopen("02160421.txt","r");
        for(i=0;i<4;i++)
        {
            fscanf(fin, "%f",&angle[i]);
            printf("%f ",angle[i]);
        }
    glutPostRedisplay();
}
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=='p')glutTimerFunc(10,timerplay,0);

}
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();
}

Week15 02160510黃品鈞

#include <GL/glut.h>
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;

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;
    glutPostOverlayRedisplay();
}

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()
{
    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();
        //glutSolidTeapot(0.3);
    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[now] -= y - oldY;
    oldY=y; oldX=x;
}

void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    ///angle++; 要自己動,要刪掉
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("TRT test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

2015年5月27日 星期三

Week14 02160510黃品鈞


今天學到將身體轉動

#include <GL/glut.h>
float angle=0 ,oldX=0, oldY=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()
{
    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,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();

    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();

}
void motion(int x,int y)
{
    angle -= 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);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();

}

Week13 02160510黃品鈞


#include <GL/glut.h>
float eyeX=-100,eyeY=80,eye2=-200;

void display()
{
    GLfloat pos[]={0,0,200,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eye2,0,0,0,0,1,0);
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x,int y)
{
    eyeX=x-400;eyeY=300-y;
    glutPostRedisplay();
}

void reshape(int w,int h)
{
     glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.001,10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
void timer(int t )
{
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300)eyeY+=4;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);



    glutMainLoop();
}