2015年5月27日 星期三

week14_02160615_成庭萱

(1)茶壺打光
 
 
#include <GL/glut.h>
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);
    glutSolidTeapot(0.3);
    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutMainLoop();
}
 
(2)T-R-T 茶壺旋轉

 
#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();
        glTranslatef(0.6, 0.2, 0); ///最後一步,把樓下的 "會對壺柄做旋轉的東西" 再移到右上 掛上去 ver4
        glRotatef(angle,0,0,1); ///把下面整團都對中間做旋轉 ver3
        glTranslatef(0.3, 0, 0); ///把它往右移,壺柄在中間 ver2
        glutSolidTeapot(0.3); ///這是個茶壺 ver1
    glPopMatrix();
    glutSolidTeapot(0.3); ///body
    glFlush();
}
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);
    glutMainLoop();
}
 
(3)T-R-T 關節旋轉

 
#include <GL/glut.h>
float angle=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); ///最後一步,把樓下的 "會對壺柄做旋轉的東西" 再移到右上 掛上去 ver4
        glRotatef(angle,0,0,1); ///把下面整團都對中間做旋轉 ver3
        glTranslatef(0.3, 0, 0); ///把它往右移,壺柄在中間 ver2
        drawArm(); ///這是個茶壺 ver1
    glPopMatrix();
    drawBody(); ///body
    glFlush();
}
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);
    glutMainLoop();
}
 
 
(4)滑鼠控制關節
 
 

 
 
 #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); ///最後一步,把樓下的 "會對壺柄做旋轉的東西" 再移到右上 掛上去 ver4
        glRotatef(angle,0,0,1); ///把下面整團都對中間做旋轉 ver3
        glTranslatef(0.25, 0, 0); ///把它往右移,壺柄在中間 ver2
        drawArm(); ///這是個茶壺 ver1
        glPushMatrix();
        glTranslatef(0.25, 0, 0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25, 0, 0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    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(); ///body
    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);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutMainLoop();
}
 
 

沒有留言:

張貼留言