2015年5月6日 星期三

Week11 02160510黃品鈞

3D的茶壺可以旋轉
我的茶壺可以像彈弓一樣發射出去了喔

#include <GL/glut.h>
#include <math.h>

float angle =0,potx=0,poty=0,potvx=0,potvy=0;
int pots=0;

void timer(int t)
{
    glutTimerFunc(10,timer,0);
    angle+=1;
    if(pots==0)poty=0.5*sin(angle/180*3.14);
    if(pots==2){
        potx+=potvx;
        poty+=potvy;
    }
    glutPostRedisplay();
}

void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(potx,poty,0);
        glRotatef(angle,0,1,0);
        glutSolidTeapot(0.05);
    glPopMatrix();
    glFlush();
}

void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)pots=1;
    else if(state==GLUT_UP){
        pots=2;
        potvx=-(x-350)/3500.0;
        potvy=(y-350)/3500.0;
    }
}

void motion(int x,int y)
{
    if(pots==1){
        potx=(x-350)/350.0;
        poty=-(y-350)/350.0;
    }
}

int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("peter");
    glutReshapeWindow(700,700);
    glutDisplayFunc(display);
    glutTimerFunc(10,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

沒有留言:

張貼留言