2015年4月9日 星期四

02160074_張凱晴 week07

本周重點:Keyboard、Mouse

1.Keyboard

2.增加音效:先下載一個音檔,放到 freeglut ->bin 資料夾裡

3.增加程式,當按下 a 的時候,發出聲音

4.找音階的單音檔,就可以做出
音階

5.Mouse 
glutSpecialFunc(special);//glut的SpecialFunc 可以用F1~F12 & 上下左右 &Ctrl.....等等


程式碼如下:
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
float teaY=0,rotX=0,rotY=0,oldX=0,oldY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(0,teaY,0);
    glRotatef(rotX,1,0,0);
    glRotatef(rotX,0,1,0);
    glColor3f(0,0.5,0.8);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();
}
void keyboard(unsigned char  key,int x,int y)
{
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
    if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7') PlaySound("Si.wav",NULL,SND_ASYNC);
}
void special(int key,int x,int y)
{
    if(key==GLUT_KEY_DOWN){ 
        teaY-=0.1; printf("down\n"); 
    }
    else if(key==GLUT_KEY_UP){
        teaY+=0.1; printf("up\n");
    }
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    oldX=x; oldY=y;
    if(GLUT_LEFT_BUTTON==button && GLUT_DOWN==state) printf("left down\n");
    if(GLUT_RIGHT_BUTTON==button && GLUT_DOWN==state) printf("right down\n");
}
void motion(int x,int y)
{
    rotX+=y-oldY;
    rotX+=x-oldX;
    oldX=x; oldY=y;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160074");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(special);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

沒有留言:

張貼留言