2024年4月11日 星期四

電腦圖學 Week08

 Week08

  1. https://jsyeh.org/3dcg10/點開下載window.zip和data,zip還有source

  2. 兩個解壓縮後把data資料夾放進window夾裡面
    分析一下 data.zip裡面的檔案 解壓縮
    -AI.mtl看到裡面 Ka Kd對應打光的material
    -AI.obj裡面很多內容
      -#代表註解
      -可以再引用另一個mtl檔
      -v 代表頂點 後面有座標x y z
      -f 代表面 face 或 facet 後面有頂點的編號
      -g 代表 gruop
  3. 桌面安裝freeglut把lib的libfreeglut.a複製改名成libglut32.a
    File-NewProject,GLUT專案week08_sample

  4. File-NewProject,GLUT專案week08-1_glm_gundam
    要把source.zip裡面的glm.h glm.c都copy到你的專案資料夾,把glm.c改檔名成glm.cpp
    在專案裡,按右鍵Add Files 把glm.cpp加入專案
    把 main.cpp裡,改成11行 GLUT的簡單程式碼
    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. }

  5. 再把gundam模型(Gundam.mtl Gumdam.obj)加進去你的專案目錄裡的data資料夾
  6. 要把專案的共作執行目錄,換成小數點
  7. 要把 桌面 複製到freeglut-MinGW-3.0.0-1.mp\freeglut\bin\freeglut.dll專案目錄裡
  8. main.cpp再加入讀模型的程式碼
  9. 程式碼:
    1. #include <GL/glut.h>
    2. #include "glm.h"
    3. GLMmodel * pmodel = NULL;
    4. void drawmodel(void)
    5. {
    6.     if (!pmodel) {
    7. pmodel = glmReadOBJ("data/Gundam.obj");
    8. if (!pmodel) exit(0);
    9. glmUnitize(pmodel);
    10. glmFacetNormals(pmodel);
    11. glmVertexNormals(pmodel, 90.0);
    12.     }

    13.     glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    14. }
    15. void display()
    16. {
    17.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    18.     drawmodel();
    19.     glutSwapBuffers();
    20. }

    21. int main(int argc,char*argv[])
    22. {
    23.     glutInit(&argc,argv);
    24.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    25.     glutCreateWindow("week08-1_again");
    26.     glutDisplayFunc(display);

    27.     glutMainLoop();
    28. }

  10. week08-2_glm_gundam_opencv_texture
  11. 安裝opencv2.1記得要 add path ,不要改目錄
  12. 關閉重開codeblocks(才會把path設定設好。要記得存檔)
  13. file-new-project, glut專案week08-2_glm_gundam_opencv_texture
  14. 重做剛剛的 glm.h glm.cpp放到目錄裡把Gundam.obj Gundam.mtl及新的Diffuse.jpg都放到專案的data目錄裡

  15. 要把專案的共作執行目錄,換成小數點


  16. 要把 桌面 複製到freeglut\bin\freeglut.dll專案

  17. 貼上貼圖18行程式碼
  18. 另外,在glutMainLoop();
    ```cpp
    myTexture("data/Diffuse.jpg")
    glutMainLoop();
  19. Setting-Compiler要加入OpenCV相關的 3個設定
  20. -compiler的include要加入 C:\OpenCV2.1\include 
    -linker的lib要加入C:\OpenCV\lib
    -linker設定 ,要加入cv210 cxcore210 highgui210



  21. 改一下glmDraw(pmodel,GLM_SMOOTH|GLM_MATERIAL|GLM_TEXTURE);
  22. 程式碼:
    1. #include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
    2. #include <opencv/cv.h>
    3. #include <GL/glut.h>
    4. int myTexture(char * filename)
    5. {
    6.     IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    7.     cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    8.     glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    9.     GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    10.     glGenTextures(1, &id); /// 產生Generate 貼圖ID
    11.     glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    12.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    13.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    14.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    15.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    16.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    17.     return id;
    18. }
    19. #include <GL/glut.h>
    20. #include "glm.h"
    21. GLMmodel * pmodel = NULL;
    22. void drawmodel(void)
    23. {
    24.     if (!pmodel) {
    25. pmodel = glmReadOBJ("data/Gundam.obj");
    26. if (!pmodel) exit(0);
    27. glmUnitize(pmodel);
    28. glmFacetNormals(pmodel);
    29. glmVertexNormals(pmodel, 90.0);
    30.     }

    31.     glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL|GLM_TEXTURE);
    32. }
    33. void display()
    34. {
    35.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    36.     drawmodel();
    37.     glutSwapBuffers();
    38. }

    39. int main(int argc,char*argv[])
    40. {
    41.     glutInit(&argc,argv);
    42.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    43.     glutCreateWindow("week08-1_again");
    44.     glutDisplayFunc(display);
    45.     myTexture("data/Diffuse.jpg");
    46.     glutMainLoop();
    47. }

  23. week08-3_glm_gundam_opencv_texture_lighting
  24. File-NewProject,GLUT專案
  25. 在專案目錄裡,加入data目錄 ,裡面有Gundam.obj Gunfam.mtl Diffuse.jpg三個檔案
  26. 在專案目錄裡,加入glm.h glm.cpp freeglut.dll三個檔案
  27. 專案設定 左邊 右鍵-最下面Properties跳出來的第二頁,工作執行目錄executable working directory 設成小數點.

  28. 最後,在main.cpp裡,貼入week08-2_glm_gundam_opencv_texture的程式碼
  29. 再加入week08-0_sample裡的打光lighting程式碼8行陣列+12+2行的函示呼叫
  30. 程式碼:
    1. ///先把 week08-1_glm_gundam 的程式貼上來
    2. ///等一下, 要再加貼圖的 18行程式
    3. #include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
    4. #include <opencv/cv.h>
    5. #include <GL/glut.h>
    6. int myTexture(char * filename)
    7. {
    8.     IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    9.     cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    10.     glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    11.     GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    12.     glGenTextures(1, &id); /// 產生Generate 貼圖ID
    13.     glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    14.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    15.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    16.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    17.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    18.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    19.     return id;
    20. }
    21. #include <GL/glut.h>
    22. #include "glm.h"
    23. GLMmodel * pmodel = NULL;
    24. void drawmodel(void)
    25. {
    26.     if (!pmodel) {
    27. pmodel = glmReadOBJ("data/Gundam.obj");
    28. if (!pmodel) exit(0);
    29. glmUnitize(pmodel);
    30. glmFacetNormals(pmodel);
    31. glmVertexNormals(pmodel, 90.0);
    32.     }

    33.     glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE);
    34. }
    35. float angle = 0; ///加這行, 讓它轉動
    36. void display()
    37. {
    38.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    39.     glPushMatrix(); ///加這行, 讓它轉動
    40.         glRotatef(angle++, 0, 1, 0); ///加這行, 讓它轉動
    41.         drawmodel(); //glutSolidTeapot( 0.3 );
    42.     glPopMatrix(); ///加這行, 讓它轉動
    43.     glutSwapBuffers();
    44. }

    45. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
    46. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
    47. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    48. const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };///加這行, 讓它轉動

    49. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
    50. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
    51. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
    52. const GLfloat high_shininess[] = { 100.0f };

    53. int main(int argc, char*argv[])
    54. {
    55.     glutInit(&argc, argv);
    56.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    57.     glutCreateWindow("week08 Gundam");
    58.     glutDisplayFunc(display);
    59.     glutIdleFunc(display); ///加這行, 讓它轉動

    60.     myTexture("data/Diffuse.jpg");

    61.     glEnable(GL_DEPTH_TEST);
    62.     glDepthFunc(GL_LESS);

    63.     glEnable(GL_LIGHT0);
    64.     glEnable(GL_NORMALIZE);
    65.     glEnable(GL_COLOR_MATERIAL);
    66.     glEnable(GL_LIGHTING);

    67.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    68.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    69.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    70.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    71.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    72.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    73.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    74.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    75.     glutMainLoop();
    76. }

沒有留言:

張貼留言