2015年5月28日 星期四

胡佩君_week 14

今天主要是要學怎麼讓關節轉動

一開始先知道xyz的概念
先做出一個會繞著茶壺把柄轉的茶壺 (加上打光)





#include <GL/glut.h>
float angle=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();
}
int main (int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("TRT 3D");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}



接下來不要利用timer來控制旋轉
改用滑鼠來控制旋轉


#include <GL/glut.h>
float angle=0;
int 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();
}


ps.有一個軟體很有趣,按Ctrl+t可以讓視窗一直都放在最上層
     叫做AlwaysOnTopMaker



再來是在中間加入一個茶壺
另一個茶壺掛在中間的茶壺嘴巴上
就像關節一樣


#include <GL/glut.h>
float angle=0;
int oldX,oldY;
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.45,0.14,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.45,-0.1,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 , oldY;;
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();
    //glutSolidTeapot(0.3);

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



沒有留言:

張貼留言