2014年4月24日 星期四

10


#include <GL/glut.h>
float teapotX=0, teapotY=0;
void keyboard(unsigned char key, int x,int y)
{
     if(key=='a') teapotX+=0.1;
     if(key=='b') teapotX-=0.1;
     if(key=='c') teapotY+=0.1;
     if(key=='d') teapotY-=0.1;
     glutPostRedisplay();
}
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();
         glTranslatef(teapotX, teapotY, 0);
         glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
   
}
int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
 
    glutMainLoop();
}




可以上下左右移動的茶壺



#include <GL/glut.h>
#include <stdio.h>//讓你的 printf可以運作
float teapotX=0 ,teapotY=0;
float angleX=0, startX=0;



/*void keyboard(unsigned char key, int x,int y)
{// new
     printf("%c %d %d\n", key ,x,y);
     if(key=='a') teapotX+=0.1;
     if(key=='b') teapotX-=0.1;
     if(key=='c') teapotY+=0.1;
     if(key=='d') teapotY-=0.1;
     glutPostRedisplay();// 小心 要重劃畫面
}*/
void mouse(int button, int state ,int x, int y)
{
     printf("%d %d %d %d\n",button,state,x,y);
     startX=x;
       
}
void motion(int x,int y)
{
     printf("%d%d\n",x,y);
     angleX+=(x-startX);
     glutPostRedisplay();
}


void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();
   
       
         glTranslatef(teapotX, teapotY ,0);
         glRotatef(angleX , 0,1,0);
         glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}  
int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
    glutDisplayFunc(display);//new
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


可以轉嚕~~~~~~


有深度~~~~

沒有留言:

張貼留言