2015年5月28日 星期四

02160792_蔡靖嵐_WEEK14

WEEK14

1)期末作品
2)會跳舞的機械人
3)T-R-T對關節轉
4)小考:T-R-T觀念
5)作業問題




先載好freeglut,所以開codeblocks新增freeglut專案.
打三個旋轉的版本
ver1:打光茶壺
ver2:()往右移
ver3:( )整盤旋轉

茶壺就自己在轉囉.




可以再加上mouse()就可以用滑鼠控制茶壺在轉~~
#include <GL/glut.h>
float angle=0,oldX=0,oldY=0;
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();
        glRotatef(angle,0,0,1);
        glTranslatef(0.5,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();

}
void timer(int t)
{
    glutTimerFunc(20,timer,0);
    angle++;
    glutPostRedisplay();
}


void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){ oldX=x; oldY=y; }
}

void motion(int x, int y)
{
    angle+=(x-oldX);
    oldX=x;
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("TRT 3D");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

來複習一下:
第一步:先弄一個茶壺



第三步:
把第一個扣在第二個茶壺上面.



#include <GL/glut.h>
float angle=0,oldX=0,oldY=0;
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.41,0.15,0);  //第三
        glRotatef(angle,0,0,1); //第二 
        glTranslatef(0.5,0,0); // 第一
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSolidTeapot(0.3);
    glFlush();

}
void timer(int t)
{
    glutTimerFunc(20,timer,0);
    //angle++;
    glutPostRedisplay();
}


void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){ oldX=x; oldY=y; }
}

void motion(int x, int y)
{
    angle+=(x-oldX);
    oldX=x;
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("TRT 3D");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


知道觀念之後就可以做一些圖案出來試試看.



#include <GL/glut.h>
float angle=0,oldX=0,oldY=0;
void drawArm()
{
    glPushMatrix();
        glScalef(0.5,0.25,0.25);
        glutSolidCube(1);
    glPopMatrix();
}
void drawBody()
{
    glPushMatrix();
        glTranslatef(0,-0.5,0);
        glRotatef(-90,1,0,0);
        glutSolidCone(0.3,0.8,30,30);
    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.15,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        //glutSolidTeapot(0.3);
        drawArm();
    glPushMatrix();
    glTranslatef(0.25,0,0);
    glRotatef(angle,0,0,1);
    glTranslatef(0.25,0,0);
    drawArm();
    glPopMatrix();
    glPopMatrix();
//以上是右手,以下是左手(程式碼是一樣,只有正負號不一樣)這樣就可以做出兩隻手啦.

glPushMatrix();
        glTranslatef(-0.15,0,0);
        glRotatef(angle,0,0,-1);
        glTranslatef(-0.25,0,0);
        //glutSolidTeapot(0.3);
        drawArm();
    glPushMatrix();
    glTranslatef(-0.25,0,0);
    glRotatef(angle,0,0,-1);
    glTranslatef(-0.25,0,0);
    drawArm();
    glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();

}
void timer(int t)
{
    glutTimerFunc(20,timer,0);
    //angle++;
    glutPostRedisplay();
}


void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){ oldX=x; oldY=y; }
}

void motion(int x, int y)
{
    angle+=(x-oldX);
    oldX=x;
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("TRT 3D");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}







沒有留言:

張貼留言