2014年4月25日 星期五

01160422_彭胤峰




/*keyboard event + Command*/

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float anglex=0 , angley=0 , startx=0 , starty=0;
float m[16]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
void keyboard(unsigned char key , int x , int y)
{
     printf("%c %d %d\n",key,x,y);
     if(key=='a') anglex+=3;
     if(key=='b') anglex-=3;
     glutPostRedisplay();
}
void mouse(int button , int state /*按住0 放開1 */ , int x , int y)
{
     printf("%d %d %d %d\n",button,state,x,y);
     startx=x;
     starty=y;
}
void motion(int x , int y) //拖曳function
{
     printf("%d %d\n",x,y);
     anglex+=x-startx; startx=x; //拖曳之後 到下一個點
     angley+=y-starty; starty=y;
     glutPostRedisplay();
}
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //清除畫面的方法
     glPushMatrix();
        float dist =sqrt(anglex*anglex + angley*angley);
        glRotatef(dist,-angley, -anglex , 0);
        glMultMatrixf(m);
        glGetFloatv(GL_MODELVIEW_MATRIX,m);
        glRotatef(anglex,0,-1,0); /*移動x 中心軸y*/
        glRotatef(angley,-1,0,0); /*移動y 中心軸x*/
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}
void light()
{
     glEnable(GL_DEPTH_TEST); //深度測試
     const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f }; //打光的方法
     glEnable(GL_LIGHT0);
     glEnable(GL_NORMALIZE);
     glEnable(GL_COLOR_MATERIAL);
     glEnable(GL_LIGHTING);
     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
   
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion); //拖曳
   
    light();
   
    glutMainLoop();
}

沒有留言:

張貼留言