2024年3月14日 星期四

電腦圖學 Week04

  1.  桌面安裝freeglut把lib的libfreeglut.a複製改名成libglut32.a
  2. File-NewProject,GLUT專案week04_mouse__glScalef
  3. 貼上10行程式碼
    1. #include <GL/glut.h>
    2. void display()
    3. {
    4.     glutSolidTeapot(0.3);
    5.     glutSwapBuffers();
    6. }

    7. int main(int argc,char*argv[])
    8. {
    9.     glutInit(&argc,argv);
    10.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    11.     glutCreateWindow("week04 mouse glScalef");
    12.     glutDisplayFunc(display);

    13.     glutMainLoop();
    14. }

  4. 補上glClearColor(1.0,1.0,0.9,1.0);
            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    背景顏色變

  5. 旋轉 再貼上上周的程式碼motion的
    1. #include <GL/glut.h>
    2. float angle=0;
    3. void display()
    4. {
    5.     glClearColor(1.0,0.0,0.9,1.0);
    6.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    7.     glPushMatrix();
    8.         glRotatef(angle,0,0,1);
    9.         glutSolidTeapot(0.3);
    10.     glPopMatrix();
    11.     glutSwapBuffers();
    12. }
    13. void motion(int x,int y){
    14.     angle=x;
    15.     display();
    16. }
    17. int main(int argc,char*argv[])
    18. {
    19.     glutInit(&argc,argv);
    20.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    21.     glutCreateWindow("week04 mouse glScalef");
    22.     glutDisplayFunc(display);
    23.     glutMotionFunc(motion);
    24.     glutMainLoop();
    25. }

  6. 縮放
    1. #include <GL/glut.h>
    2. ///float angle=0;
    3. float s=1;
    4. void display()
    5. {
    6.     glClearColor(1.0,0.0,0.9,1.0);
    7.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    8.     glPushMatrix();
    9.         ///glRotatef(angle,0,0,1);
    10.         glScalef(s,s,s);
    11.         glutSolidTeapot(0.3);
    12.     glPopMatrix();
    13.     glutSwapBuffers();
    14. }
    15. void motion(int x,int y){
    16.     ///angle=x;
    17.     s=1+(x-150)/150.0;
    18.     display();
    19. }
    20. int main(int argc,char*argv[])
    21. {
    22.     glutInit(&argc,argv);
    23.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    24.     glutCreateWindow("week04 mouse glScalef");
    25.     glutDisplayFunc(display);
    26.     glutMotionFunc(motion);
    27.     glutMainLoop();
    28. }
  7. week04_keyboard_mouse_motion
    1. #include <GL/glut.h>
    2. #include <stdio.h.>
    3. void display()
    4. {
    5.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    6.     glutSolidTeapot(0.3);
    7.     glutSwapBuffers();
    8. }
    9. void keyboard(unsigned char key,int x,int y){
    10.     printf("key: %c x: %d y: %d\n",key,x,y);
    11. }
    12. int main(int argc,char*argv[])
    13. {
    14.     glutInit(&argc,argv);
    15.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    16.     glutCreateWindow("week04 keyboard mouse motion");
    17.     glutDisplayFunc(display);
    18.     glutKeyboardFunc(keyboard);
    19.     glutMainLoop();
    20. }
  8. 改一下
    1. #include <GL/glut.h>
    2. #include <stdio.h.>
    3. float angle=0,oldX=0;
    4. void display()
    5. {
    6.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    7.     glPushMatrix();
    8.         glRotatef(angle,0,0,1);
    9.         glutSolidTeapot(0.3);
    10.     glPushMatrix();
    11.     glutSwapBuffers();
    12. }
    13. void mouse(int button,int state,int x,int y)
    14. {
    15.     oldX=x;
    16. }
    17. void motion(int x,int y)
    18. {
    19.     angle+=(x-oldX);
    20.     oldX=x;
    21.     display();
    22. }
    23. void keyboard(unsigned char key,int x,int y)
    24. {

    25.     printf("key: %c x: %d y: %d\n",key,x,y);
    26. }
    27. int main(int argc,char*argv[])
    28. {
    29.     glutInit(&argc,argv);
    30.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    31.     glutCreateWindow("week04 keyboard mouse motion");
    32.     glutDisplayFunc(display);
    33.     glutKeyboardFunc(keyboard);
    34.     glutMouseFunc(mouse);
    35.     glutMotionFunc(motion);
    36.     glutMainLoop();
    37. }

  9. https://jsyeh.org/3dcg10/點開下載window.zip和data,zip

    兩個解壓縮後把data資料夾放進window夾裡面

  10. week04 translate rotate scale貼上前一個程式碼
    稍微改一下茶壺會自己旋轉
    1. #include <GL/glut.h>
    2. #include <stdio.h.>
    3. float angle=0,oldX=0;
    4. void display()
    5. {
    6.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    7.     glPushMatrix();
    8.         glRotatef(angle++,0,0,1);
    9.         glutSolidTeapot(0.3);
    10.     glPopMatrix();
    11.     glutSwapBuffers();
    12. }
    13. int main(int argc,char*argv[])
    14. {
    15.     glutInit(&argc,argv);
    16.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    17.     glutCreateWindow("week04 keyboard mouse motion");
    18.     glutDisplayFunc(display);
    19.     glutIdleFunc(display);

    20.     glutMainLoop();
    21. }

  11. 旋轉縮小變色茶壺
    1. #include <GL/glut.h>
    2. #include <stdio.h.>
    3. float angle=0,oldX=0;
    4. void display()
    5. {
    6.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    7.     glPushMatrix();
    8.         glTranslatef(0.8,0,0);
    9.         glRotatef(angle++,0,0,1);
    10.         glScalef(0.3,0.3,0.3);
    11.         glColor3f(0.5,1,1);
    12.         glutSolidTeapot(0.3);
    13.     glPopMatrix();
    14.     glutSwapBuffers();
    15. }
    16. int main(int argc,char*argv[])
    17. {
    18.     glutInit(&argc,argv);
    19.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    20.     glutCreateWindow("week04 translate rotate scale");
    21.     glutDisplayFunc(display);
    22.     glutIdleFunc(display);

    23.     glutMainLoop();
    24. }

  12. 老師畫面

  13. week04_rotate_translate_scale再創一個貼上前一個的程式碼
    然後這兩個交換glRotatef(angle++,0,0,1);    glTranslatef(0.8,0,0);
    #include <GL/glut.h>
    #include <stdio.h.>
    float angle=0,oldX=0;
    void display()
    {
        glClearColor(1.0,1.0,0.9,1.0);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glPushMatrix();
            glRotatef(angle++,0,0,1);
            glTranslatef(0.8,0,0);
            glScalef(0.3,0.3,0.3);
            glColor3f(0.5,1,1);
            glutSolidTeapot(0.3);
        glPopMatrix();
        glutSwapBuffers();
    }
    int main(int argc,char*argv[])
    {
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
        glutCreateWindow("week04 translate rotate scale");
        glutDisplayFunc(display);
        glutIdleFunc(display);

        glutMainLoop();
    }

沒有留言:

張貼留言