2024年5月30日 星期四

電腦圖學 Week15


 # Week15

#week15-1_PlaySound

  1. 原來程式碼改
  2. ```cpp
    #include <windows.h>

    ```cpp
    int main(...)
    {
         PlaySound("C:/mykbeat.wav",NULL,SND_ASYNC);
    }```
  3. 改程式碼
    1. PlaySound("C:/Do.wav",NULL,SND_SYNC);
    2.     PlaySound("C:/Re.wav",NULL,SND_SYNC);
    3.     PlaySound("C:/Mi.wav",NULL,SND_SYNC);
    4.     PlaySound("C:/Fa.wav",NULL,SND_SYNC);
    5.     PlaySound("C:/Sol.wav",NULL,SND_SYNC);
    6.     PlaySound("C:/mykbeat.wav",NULL,SND_ASYNC);
  4. 程式碼:
      1. #include <windows.h>
      2. /*
      3.  * GLUT Shapes Demo
      4.  *
      5.  * Written by Nigel Stewart November 2003
      6.  *
      7.  * This program is test harness for the sphere, cone
      8.  * and torus shapes in GLUT.
      9.  *
      10.  * Spinning wireframe and smooth shaded shapes are
      11.  * displayed until the ESC or q key is pressed.  The
      12.  * number of geometry stacks and slices can be adjusted
      13.  * using the + and - keys.
      14.  */

      15. #ifdef __APPLE__
      16. #include <GLUT/glut.h>
      17. #else
      18. #include <GL/glut.h>
      19. #endif

      20. #include <stdlib.h>

      21. static int slices = 16;
      22. static int stacks = 16;

      23. /* GLUT callback Handlers */

      24. static void resize(int width, int height)
      25. {
      26.     const float ar = (float) width / (float) height;

      27.     glViewport(0, 0, width, height);
      28.     glMatrixMode(GL_PROJECTION);
      29.     glLoadIdentity();
      30.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

      31.     glMatrixMode(GL_MODELVIEW);
      32.     glLoadIdentity() ;
      33. }

      34. static void display(void)
      35. {
      36.     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
      37.     const double a = t*90.0;

      38.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      39.     glColor3d(1,0,0);

      40.     glPushMatrix();
      41.         glTranslated(-2.4,1.2,-6);
      42.         glRotated(60,1,0,0);
      43.         glRotated(a,0,0,1);
      44.         glutSolidSphere(1,slices,stacks);
      45.     glPopMatrix();

      46.     glPushMatrix();
      47.         glTranslated(0,1.2,-6);
      48.         glRotated(60,1,0,0);
      49.         glRotated(a,0,0,1);
      50.         glutSolidCone(1,1,slices,stacks);
      51.     glPopMatrix();

      52.     glPushMatrix();
      53.         glTranslated(2.4,1.2,-6);
      54.         glRotated(60,1,0,0);
      55.         glRotated(a,0,0,1);
      56.         glutSolidTorus(0.2,0.8,slices,stacks);
      57.     glPopMatrix();

      58.     glPushMatrix();
      59.         glTranslated(-2.4,-1.2,-6);
      60.         glRotated(60,1,0,0);
      61.         glRotated(a,0,0,1);
      62.         glutWireSphere(1,slices,stacks);
      63.     glPopMatrix();

      64.     glPushMatrix();
      65.         glTranslated(0,-1.2,-6);
      66.         glRotated(60,1,0,0);
      67.         glRotated(a,0,0,1);
      68.         glutWireCone(1,1,slices,stacks);
      69.     glPopMatrix();

      70.     glPushMatrix();
      71.         glTranslated(2.4,-1.2,-6);
      72.         glRotated(60,1,0,0);
      73.         glRotated(a,0,0,1);
      74.         glutWireTorus(0.2,0.8,slices,stacks);
      75.     glPopMatrix();

      76.     glutSwapBuffers();
      77. }


      78. static void key(unsigned char key, int x, int y)
      79. {
      80.     switch (key)
      81.     {
      82.         case '1':
      83.             PlaySound("Do.wav",NULL,SND_ASYNC);
      84.             break;

      85.         case '2':
      86.             PlaySound("Re.wav",NULL,SND_ASYNC);
      87.             break;
      88.         case '3':
      89.             PlaySound("Mi.wav",NULL,SND_ASYNC);
      90.             break;

      91.             case 27 :
      92.         case 'q':
      93.             exit(0);
      94.             break;

      95.         case '+':
      96.             slices++;
      97.             stacks++;
      98.             break;

      99.         case '-':
      100.             if (slices>3 && stacks>3)
      101.             {
      102.                 slices--;
      103.                 stacks--;
      104.             }
      105.             break;
      106.     }

      107.     glutPostRedisplay();
      108. }

      109. static void idle(void)
      110. {
      111.     glutPostRedisplay();
      112. }

      113. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
      114. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
      115. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
      116. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

      117. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
      118. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
      119. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
      120. const GLfloat high_shininess[] = { 100.0f };

      121. /* Program entry point */

      122. int main(int argc, char *argv[])
      123. {
      124.     /*PlaySound("C:/Do.wav",NULL,SND_SYNC);
      125.     PlaySound("C:/Re.wav",NULL,SND_SYNC);
      126.     PlaySound("C:/Mi.wav",NULL,SND_SYNC);
      127.     PlaySound("C:/Fa.wav",NULL,SND_SYNC);
      128.     PlaySound("C:/Sol.wav",NULL,SND_SYNC);
      129.     PlaySound("C:/mykbeat.wav",NULL,SND_ASYNC);*/
      130.     glutInit(&argc, argv);
      131.     glutInitWindowSize(640,480);
      132.     glutInitWindowPosition(10,10);
      133.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

      134.     glutCreateWindow("GLUT Shapes");

      135.     glutReshapeFunc(resize);
      136.     glutDisplayFunc(display);
      137.     glutKeyboardFunc(key);
      138.     glutIdleFunc(idle);

      139.     glClearColor(1,1,1,1);
      140.     glEnable(GL_CULL_FACE);
      141.     glCullFace(GL_BACK);

      142.     glEnable(GL_DEPTH_TEST);
      143.     glDepthFunc(GL_LESS);

      144.     glEnable(GL_LIGHT0);
      145.     glEnable(GL_NORMALIZE);
      146.     glEnable(GL_COLOR_MATERIAL);
      147.     glEnable(GL_LIGHTING);

      148.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
      149.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
      150.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
      151.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);

      152.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
      153.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
      154.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
      155.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

      156.     glutMainLoop();

      157.     return EXIT_SUCCESS;
      158. }


  5. #week15-2_PlaySound_empty
  6. 程式碼全部刪掉
  7. #include <windows.h> int main() { PlaySound("Do.wav",NULL,SND_SYNC); }
  8. 程式碼: #include <windows.h> #include <stdio.h> int main() { ///PlaySound("C:/Do.wav",NULL,SND_SYNC); PlaySound("mykbeat.wav",NULL,SND_SYNC); printf("請輸入一個整數: "); int n; scanf("%d",&n); }

  9. #week15-3_CM3_MCI
  10. ```cpp
    #include <CM3_MCI.h> //雙引號,很像之前"glm.h"
    CM3_MCI myMP3;//宣告一個物件變數

  11. 在main()裡
  12. ```cpp
    int main(...)
    {
        myMP3.Load("song.mp3");
        myMP3.Play();
        ...
    }
  13. 程式碼:
    1. #include <windows.h>
    2. #include <stdio.h>
    3. int main()
    4. {
    5.      ///PlaySound("C:/Do.wav",NULL,SND_SYNC);
    6.      PlaySound("mykbeat.wav",NULL,SND_SYNC);
    7.      printf("請輸入一個整數: ");
    8.      int n;
    9.      scanf("%d",&n);
    10. }



  1. week15-4_mouse_montion_translatef_rotate
  2. 貼上11行程式
  3. 目標:先能移動 旋轉 陣列
  4. 滑鼠移動物件顯示座標位址
  5. 程式碼:
    ///貼上11行程式 ,目標:先能移動 旋轉 陣列 #include <GL/glut.h> #include <stdio.h>///printf() float angleX[10]={},angleY[10]={};///陣列 float teapotX=0,teapotY=0; void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glutSolidSphere(0.02,30,30);///秀出中心點 glPushMatrix(); glTranslatef(teapotX,teapotY,0); glutSolidTeapot(0.3); glPopMatrix(); glutSwapBuffers(); } int oldX=0,oldY=0; void mouse(int button,int state,int x,int y){ oldX=x; oldY=y; } void motion(int x,int y){ teapotX += (x-oldX)/150.0; teapotY += (y-oldY)/150.0; printf("glTranslatef(%.3f,%.3f,0);\n",teapotX,teapotY); oldX=x; oldY=y; glutPostRedisplay(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week15-4"); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutMainLoop(); }

  6. 把程式碼改成設置圓點在茶壺端來讓它旋轉
  7. 程式碼:
    ///貼上11行程式 ,目標:先能移動 旋轉 陣列
    #include <GL/glut.h>
    #include <stdio.h>///printf()
    float angleX[10]={},angleY[10]={};///陣列
    float teapotX=0,teapotY=0;///第3-4周教的技巧
    void display(){
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glutSolidSphere(0.02,30,30);///秀出中心點
        glPushMatrix();
            ///glTranslatef(teapotX,teapotY,0);
            glRotatef(angleX[0],0,0,1);
            glTranslatef(-0.520,-0.133,0);
            glutSolidTeapot(0.3);
        glPopMatrix();
        glutSwapBuffers();
    }
    int oldX=0,oldY=0;
    void mouse(int button,int state,int x,int y){
        oldX=x;
        oldY=y;
    }
    void motion(int x,int y){
        ///teapotX += (x-oldX)/150.0;
        ///teapotY += (y-oldY)/150.0;
        ///printf("glTranslatef(%.3f,%.3f,0);\n",teapotX,teapotY);
        angleX[0]+=(x-oldX);
        oldX=x;
        oldY=y;
        glutPostRedisplay();
    }
    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow("week15-4");
        glutDisplayFunc(display);
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
        glutMainLoop();


    }



沒有留言:

張貼留言