2024年4月25日 星期四

電腦圖學 Week10

 #Week10

  1. week10-1_glutWireCube_mybody_myArm
  2. 安裝freeglut,把lib\libfreeglut.a複製成libglut32.a
  3. File-New-Project, GLUT專案 week10-1_glutWireCube_mybody_myArm
  4. 貼上上周的week09-3_glutWireTeapot_glutWireCube_glutWireSphere的程式碼
  5. 要把void display()改寫一下,呼叫myBody() myArm()
  6. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. void display()
    18. {
    19. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    20.     myBody();
    21.     myArm();

    22. glutSwapBuffers();

    23. }
    24. int main(int argc, char*argv[])
    25. {
    26. glutInit(&argc, argv);
    27. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    28. glutCreateWindow("week09 Microsoft Visual Studio");
    29. glutDisplayFunc(display);
    30. glutMainLoop();
    31. }

  7. week10-2_rotate_myArm
  8. File-New-Project, GLUT專案week10-2_rotate_myArm
  9. 貼上week10-1_glutWireCube_mybody_myArm的程式
  10. 外面加上float angle=0;
  11. 在main()加上glutIdleFunc(display);
  12. 在display()裡,myArm()的前後,加上glPushMatrix();  glPopMatrix();  glRotatef(angle++,0,0,1);
  13. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();
    22.     glPushMatrix();
    23.         glRotatef(angle++,0,0,1);
    24.         myArm();
    25.     glPopMatrix();

    26. glutSwapBuffers();

    27. }
    28. int main(int argc, char*argv[])
    29. {
    30. glutInit(&argc, argv);
    31. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    32. glutCreateWindow("week09 Microsoft Visual Studio");
    33. glutDisplayFunc(display);
    34. glutIdleFunc(display);
    35. glutMainLoop();
    36. }

  14. week10-3_rotate_translate_myArm
  15. File-New-Project, GLUT專案week10-3_rotate_translate_myArm
  16. 貼上week10-2_rotate_myArm的程式
  17. 加上glTranslatef(0.15,0,0);
  18. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();
    22.     glPushMatrix();
    23.         glRotatef(angle++,0,0,1);
    24.         glTranslatef(0.15,0,0);
    25.         myArm();
    26.     glPopMatrix();

    27. glutSwapBuffers();

    28. }
    29. int main(int argc, char*argv[])
    30. {
    31. glutInit(&argc, argv);
    32. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    33. glutCreateWindow("week09 Microsoft Visual Studio");
    34. glutDisplayFunc(display);
    35. glutIdleFunc(display);
    36. glutMainLoop();
    37. }

  19. week10-4_translate_rotate_translate_myArm
  20. File-New-Project, GLUT專案week10-4_translate_rotate_translate_myArm
  21. 貼上week10-3_rotate_translate_myArm的程式
  22. 加上最後一行glTranslatef(0.3,0.3,0);放最上面 掛上去
  23. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();
    22.     glPushMatrix();
    23.         glTranslatef(0.3,0.3,0);
    24.         glRotatef(angle++,0,0,1);
    25.         glTranslatef(0.15,0,0);
    26.         myArm();
    27.     glPopMatrix();

    28. glutSwapBuffers();

    29. }
    30. int main(int argc, char*argv[])
    31. {
    32. glutInit(&argc, argv);
    33. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    34. glutCreateWindow("week10-4_translate_rotate_translate_myArm");
    35. glutDisplayFunc(display);
    36. glutIdleFunc(display);
    37. glutMainLoop();
    38. }

  24. week10-5_TRT_TRT_myArm
  25. File-New-Project, GLUT專案week10-5_TRT_TRT_myArm
  26. 貼上week10-4_translate_rotate_translate_myArm的程式
  27. 把整套glPushMatrix();  glTranslatef();  glRotatef();  glTranslatef();  myArm();  glPopMatrix();
    要插在中間
  28. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();
    22.     glPushMatrix();
    23.         glTranslatef(0.3,0.3,0);
    24.         glRotatef(angle++,0,0,1);
    25.         glTranslatef(0.15,0,0);
    26.         myArm();
    27.         glPushMatrix();
    28.             glTranslatef(0.15,0.3,0);
    29.             glRotatef(angle++,0,0,1);
    30.             glTranslatef(0.15,0,0);
    31.             myArm();
    32.         glPopMatrix();
    33.     glPopMatrix();

    34. glutSwapBuffers();

    35. }
    36. int main(int argc, char*argv[])
    37. {
    38. glutInit(&argc, argv);
    39. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    40. glutCreateWindow("week10-4_translate_rotate_translate_myArm");
    41. glutDisplayFunc(display);
    42. glutIdleFunc(display);
    43. glutMainLoop();
    44. }

  29. week10-6_left_right_myArm
  30. File-New-Project, GLUT專案week10-6_left_right_myArm
  31. 貼上week10-5_TRT_TRT_myArm的程式
  32. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();

    22.     glPushMatrix();
    23.         glTranslatef(0.3,0.3,0);
    24.         glRotatef(angle++,0,0,1);
    25.         glTranslatef(0.15,0,0);
    26.         myArm();
    27.         glPushMatrix();
    28.             glTranslatef(0.15,0,0);
    29.             glRotatef(angle++,0,0,1);
    30.             glTranslatef(0.15,0,0);
    31.             myArm();
    32.         glPopMatrix();
    33.     glPopMatrix();

    34.     glPushMatrix();
    35.         glTranslatef(-0.3,0.3,0);
    36.         glRotatef(angle++,0,0,1);
    37.         glTranslatef(-0.15,0,0);
    38.         myArm();
    39.         glPushMatrix();
    40.             glTranslatef(-0.15,0,0);
    41.             glRotatef(angle++,0,0,1);
    42.             glTranslatef(-0.15,0,0);
    43.             myArm();
    44.         glPopMatrix();
    45.     glPopMatrix();

    46. glutSwapBuffers();

    47. }
    48. int main(int argc, char*argv[])
    49. {
    50. glutInit(&argc, argv);
    51. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    52. glutCreateWindow("week10-4_translate_rotate_translate_myArm");
    53. glutDisplayFunc(display);
    54. glutIdleFunc(display);
    55. glutMainLoop();
    56. }

  33. week10-7_mouse_motion_angle_myArm
  34. File-New-Project, GLUT專案week10-7_mouse_motion_angle_myArm
  35. 貼上week10-6_left_right_myArm的程式
  36. 想要用mouse motion來操控,所以angle++ 都改成angle(不要++)
  37. 在main()加glutMotionFunc(motion);
  38. 加一個void motion(int x,int y)函式
  39. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();

    22.     glPushMatrix();
    23.         glTranslatef(0.3,0.3,0);
    24.         glRotatef(angle,0,0,1);
    25.         glTranslatef(0.15,0,0);
    26.         myArm();
    27.         glPushMatrix();
    28.             glTranslatef(0.15,0,0);
    29.             glRotatef(angle,0,0,1);
    30.             glTranslatef(0.15,0,0);
    31.             myArm();
    32.         glPopMatrix();
    33.     glPopMatrix();

    34.     glPushMatrix();
    35.         glTranslatef(-0.3,0.3,0);
    36.         glRotatef(angle,0,0,1);
    37.         glTranslatef(-0.15,0,0);
    38.         myArm();
    39.         glPushMatrix();
    40.             glTranslatef(-0.15,0,0);
    41.             glRotatef(angle,0,0,1);
    42.             glTranslatef(-0.15,0,0);
    43.             myArm();
    44.         glPopMatrix();
    45.     glPopMatrix();

    46. glutSwapBuffers();
    47. }
    48. void motion(int x,int y){
    49.     angle=x;
    50.     glutPostRedisplay();
    51. }
    52. int main(int argc, char*argv[])
    53. {
    54. glutInit(&argc, argv);
    55. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    56. glutCreateWindow("week10");

    57. glutDisplayFunc(display);
    58. glutMotionFunc(motion);
    59. glutIdleFunc(display);

    60. glutMainLoop();
    61. }
  40. week10-8_mouse_motion_angle_2_myArm
  41. File-New-Project, GLUT專案week10-8_mouse_motion_angle_2_myArm
  42. 貼上week10-7_mouse_motion_angle_myArm的程式
  43. 把display()裡面的 左邊的angle改成 -angle(讓左右的角度,向負相反)
  44. 程式碼:
    1. #include <GL/glut.h>
    2. void myBody()
    3. {
    4.     glPushMatrix();
    5.         glColor3f(1,0,0);
    6.         glutWireCube(0.6);///myBody()
    7. glPopMatrix();
    8. }
    9. void myArm()
    10. {
    11.     glPushMatrix();
    12.         glColor3f(0,1,0);
    13.         glScalef(1,0.4,0.4);
    14.         glutWireCube(0.3);///myBody()
    15. glPopMatrix();
    16. }
    17. float angle=0;
    18. void display()
    19. {
    20. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    21.     myBody();

    22.     glPushMatrix();
    23.         glTranslatef(0.3,0.3,0);
    24.         glRotatef(angle,0,0,1);
    25.         glTranslatef(0.15,0,0);
    26.         myArm();
    27.         glPushMatrix();
    28.             glTranslatef(0.15,0,0);
    29.             glRotatef(angle,0,0,1);
    30.             glTranslatef(0.15,0,0);
    31.             myArm();
    32.         glPopMatrix();
    33.     glPopMatrix();

    34.     glPushMatrix();
    35.         glTranslatef(-0.3,0.3,0);
    36.         glRotatef(-angle,0,0,1);
    37.         glTranslatef(-0.15,0,0);
    38.         myArm();
    39.         glPushMatrix();
    40.             glTranslatef(-0.15,0,0);
    41.             glRotatef(-angle,0,0,1);
    42.             glTranslatef(-0.15,0,0);
    43.             myArm();
    44.         glPopMatrix();
    45.     glPopMatrix();

    46. glutSwapBuffers();
    47. }
    48. void motion(int x,int y){
    49.     angle=x;
    50.     glutPostRedisplay();
    51. }
    52. int main(int argc, char*argv[])
    53. {
    54. glutInit(&argc, argv);
    55. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    56. glutCreateWindow("week10");

    57. glutDisplayFunc(display);
    58. glutMotionFunc(motion);
    59. glutIdleFunc(display);

    60. glutMainLoop();
    61. }


  45. week10-9_PlaySound
  46. File-New-Project, GLUT專案week10-9_PlaySound
  47. 貼上week10-8_mouse_motion_angle_2_myArm的程式
  48. 在#include 的地方,加上 #include <mmsystem.h>
  49. 在main()的第1行,加入PlaySound("你的wav檔.wav",NULL,SND_ASYNC)
  50. 把老師的horse.wav放在 C:跟目錄裡
  51. 程式碼:
    1. #include <GL/glut.h>
    2. #include <mmsystem.h>
    3. void myBody()
    4. {
    5.     glPushMatrix();
    6.         glColor3f(1,0,0);
    7.         glutWireCube(0.6);///myBody()
    8. glPopMatrix();
    9. }
    10. void myArm()
    11. {
    12.     glPushMatrix();
    13.         glColor3f(0,1,0);
    14.         glScalef(1,0.4,0.4);
    15.         glutWireCube(0.3);///myBody()
    16. glPopMatrix();
    17. }
    18. float angle=0;
    19. void display()
    20. {
    21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    22.     myBody();

    23.     glPushMatrix();
    24.         glTranslatef(0.3,0.3,0);
    25.         glRotatef(angle,0,0,1);
    26.         glTranslatef(0.15,0,0);
    27.         myArm();
    28.         glPushMatrix();
    29.             glTranslatef(0.15,0,0);
    30.             glRotatef(angle,0,0,1);
    31.             glTranslatef(0.15,0,0);
    32.             myArm();
    33.         glPopMatrix();
    34.     glPopMatrix();

    35.     glPushMatrix();
    36.         glTranslatef(-0.3,0.3,0);
    37.         glRotatef(-angle,0,0,1);
    38.         glTranslatef(-0.15,0,0);
    39.         myArm();
    40.         glPushMatrix();
    41.             glTranslatef(-0.15,0,0);
    42.             glRotatef(-angle,0,0,1);
    43.             glTranslatef(-0.15,0,0);
    44.             myArm();
    45.         glPopMatrix();
    46.     glPopMatrix();

    47. glutSwapBuffers();
    48. }
    49. void motion(int x,int y){
    50.     angle=x;
    51.     glutPostRedisplay();
    52. }
    53. int main(int argc, char*argv[])
    54. {
    55.     PlaySound("C://horse.wav",NULL,SND_ASYNC);
    56. glutInit(&argc, argv);
    57. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    58. glutCreateWindow("week10");

    59. glutDisplayFunc(display);
    60. glutMotionFunc(motion);
    61. glutIdleFunc(display);

    62. glutMainLoop();
    63. }

沒有留言:

張貼留言