2023年5月31日 星期三

呱! - Week16 - 總複習🦆

Week16


安裝Git GitBash

-cd desktop
-git clone https://github.com/Owoyayou/2023graphicsa
-cd 2023graphicsa
-start .
  (打開2023graphicsa資料夾,把今日程式碼檔案放進來)
-git add .
-git status
-git config --global user.email ______________
-git config --global user.name Owoyayou
-git commit -m "week15"
-git push


Notepad++新增檔案


檔案類型.cpp


step01



-1


step02



-2


step03





-3


step04_移動



-4


step05_轉動



-5





新增model資料夾放入.mtl檔 .obj檔
Properties > Build targets > working dir工作執行目錄 > Debug 和 Release 都改成.

安裝OpenCV-2.1.0-win32-vs2008.exe,有三個選項的地方注意,選第二個,並且重開CodeBlocks
Settings > Compiler > Linker settings 要加目錄C:/...??以及210什麼鬼的




執行程式 > 找不到檔案




Final_Project > freeglut > bin > freeglut.dll


貼到 > week16_all資料目錄裡


再執行一次檔案就會正常了





step06




複製week10的 glm.h 以及 glm.cpp 至week16_all中


並且Add files > glm.cpp 


再執行一次看看,會出現東西是正常的



加上2行程式碼gundam = glmReadOBJ("model/gundam.obj");

把茶壺刪掉
加入glmDraw(gundam, GLM_MATERIAL | GLM_TEXTURE);




step06-1


太大了,要改變大小,加入glScalef(0.03, 0.03, 0.03);






step06-2

加上顏色glColor3f(1,0,0);

紅色鋼彈配上綠色點點






step06-3

加入week7的main.cpp程式碼以及 myTexture("model/Diffuse.jpg");



把鋼彈改成白色glColor3f(1,1,1);




增加3D感glEnable(GL_DEPTH_TEST);




42行增加旋轉glRotatef(angle, 0, 1, 0);



step07







-7


step07-1


6、7行
GLMmodel * head = NULL;
GLMmodel * body = NULL;

44行
glmDraw(body, GLM_MATERIAL | GLM_TEXTURE);

62、63行
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");




身體出來了,但是太小,進行大小更動

42、43行增加
glTranslatef(0,-0.7,0);
glScalef(2, 2, 2);




以身體做基準,把頭畫在身體下面

47行增加的頭程式碼在身體程式碼下
glPushMatrix();///頭
            glmDraw(head, GLM_MATERIAL | GLM_TEXTURE);
glPopMatrix();



接著把頭放到正圓心(綠色點點)
31、32


45行的旋轉先註解掉 //glRotatef(angle, 0, 1, 0); 比較好移動






複製最後一行


46行被註解掉的程式碼複製貼上到50行
49行負的變正的,正的變負的




執行結果

頭的橫向轉動



----------------------------------------------------------------------

*執行有誤*

因為 glTranslatef(0,-0.7,0); 這行的問題,導致頭會亂轉、無法正常轉動



24行新增angle2=0
34行新增angle2 += y - oldY; ///有不同
52行新增glRotatef(angle2, 1, 0, 0);

step08

#include <stdio.h>
#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL; ///GLMmodel * gundam = NULL;
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}

float teapotX=0, teapotY=0, angle=0, angle2=0, 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)/10.0; ///有不同
    teapotY += (oldY - y)/10.0; ///有不同
    angle += x - oldX;
    angle2 += y - oldY; ///有不同
    oldX = x;
    oldY = y; ///有不同
    printf("glTranslatef(%.3f , %.3f , 0 );\n", teapotX, teapotY);
    glutPostRedisplay();
}
void display() {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1,1,1);///glColor3f(1,0,0);
        glScalef(0.03, 0.03, 0.03);
        ///glRotatef(angle, 0, 1, 0);
        glmDraw(body, GLM_MATERIAL | GLM_TEXTURE); ///glmDraw(gundam, GLM_MATERIAL | GLM_TEXTURE);
        glPushMatrix();
            ///glTranslatef(+0.000 , +33.200 , 0 );
            ///glRotatef(angle, 0, 1, 0);
            ///glRotatef(angle2, 1, 0, 0);
            ///glTranslatef(-0.000 , -33.200 , 0 );
            glTranslatef(teapotX, teapotY, 0);
            glmDraw(head, GLM_MATERIAL | GLM_TEXTURE);
        glPopMatrix();
    glPopMatrix();

    glColor3f(0,1,0);
    glutSolidTeapot( 0.01 );

    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    head = glmReadOBJ("model/head.obj");
    body = glmReadOBJ("model/body.obj"); ///gundam = glmReadOBJ("model/gundam.obj");
    myTexture("model/Diffuse.jpg");
    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}


*執行有誤*
---------------------



step08執行結果發現頭的旋轉有問題,經過修正,正確程式碼應為

step09






執行結果如下



-9



step10_身體長手臂


第8行宣告手臂變數
GLMmodel * arm1 = NULL, * arm2 = NULL;

75、76新增讀取.obj檔案
arm1 = glmReadOBJ("model/arm1.obj");
arm2 = glmReadOBJ("model/arm2.obj");

63、64行變更中心點位置


54行被註解掉的茶壺,複製到59行glTranslatef(teapotX, teapotY, 0);

多加一層
glPushMatrix();
glPopMatrix();
在最外層固定頭、身體,用來設定手臂


程式碼

-10




step11_設定手臂中心點






先把除了手臂的Rotate都註解,需要時才打開,否則影響視覺觀感,如上圖所示。


成功



程式碼

-11




step12_手臂長手肘




宣告手臂變數
GLMmodel * hand1 = NULL, * hand2 = NULL;

新增讀取.obj檔案
hand1 = glmReadOBJ("model/hand1.obj");
hand2 = glmReadOBJ("model/hand2.obj");

手臂程式碼往下長手肘程式碼
glPushMatrix();
    glmDraw(hand2, GLM_MATERIAL | GLM_TEXTURE);
glPopMatrix();

複製茶壺到手肘程式碼內
glPushMatrix();
    glTranslatef(teapotX, teapotY, 0);
    glmDraw(hand2, GLM_MATERIAL | GLM_TEXTURE);
glPopMatrix();

註解先前的旋轉程式碼

執行

拉動手肘至小綠點位置,並複製最後一行位置程式碼
glTranslatef(4.400 , -18.600 , 0 );


註解茶壺程式碼,複製貼上旋轉程式碼
glPushMatrix();
    glTranslatef(-4.400 , 18.600 , 0 );
    glRotatef(angle, 0, 1, 0);
    glRotatef(angle2, 1, 0, 0);
    glTranslatef(4.400 , -18.600 , 0 );
    //glTranslatef(teapotX, teapotY, 0);
    glmDraw(hand1, GLM_MATERIAL | GLM_TEXTURE);
glPopMatrix();


執行


上半身的左半邊出來了!!!


上半身右半部

複製左半部程式碼,直接貼在下面(不要把Rotate打開),因為是右半部,所以與左半部對稱,故程式碼只須改手臂、手肘名稱以及右半邊的全部X軸負的變正的,正的變負的
glPushMatrix();
                glTranslatef(4.000 , 22.000 , 0 );
                //glRotatef(angle, 0, 1, 0);
                //glRotatef(angle2, 1, 0, 0);
                glTranslatef(-4.000 , -22.000 , 0 );
                //glTranslatef(teapotX, teapotY, 0);
                glmDraw(arm2, GLM_MATERIAL | GLM_TEXTURE);
                glPushMatrix();
                    glTranslatef(4.400 , 18.600 , 0 );
                    glRotatef(angle, 0, 1, 0);
                    glRotatef(angle2, 1, 0, 0);
                    glTranslatef(-4.400 , -18.600 , 0 );
                    //glTranslatef(teapotX, teapotY, 0);
                    glmDraw(hand2, GLM_MATERIAL | GLM_TEXTURE);
                glPopMatrix();
            glPopMatrix();






程式碼

-12




step13

先把旋轉註解,把尚未讀近來的模型全部做宣告
GLMmodel * bot = NULL;
GLMmodel * leg1 = NULL, * leg2 = NULL;
GLMmodel * knee1 = NULL, * knee2 = NULL;
GLMmodel * foot1 = NULL, * foot2 = NULL;

全部讀入
 bot = glmReadOBJ("model/bot.obj");
 leg1 = glmReadOBJ("model/leg1.obj");
 leg2 = glmReadOBJ("model/leg2.obj");
 knee1 = glmReadOBJ("model/knee1.obj");
 knee2 = glmReadOBJ("model/knee2.obj");
 foot1 = glmReadOBJ("model/foot1.obj");
 foot2 = glmReadOBJ("model/foot2.obj");

屁股跟身體同層







屁股接大腿,記得把茶壺註解或刪掉
glPushMatrix();///左大腿
    glTranslatef(-2.100 , 14.900 , 0 );
    glRotatef(angle, 0, 1, 0);
    glRotatef(angle2, 1, 0, 0);
    glTranslatef(2.100 , -14.900 , 0 );
    glTranslatef(teapotX, teapotY, 0);
    glmDraw(knee1, GLM_MATERIAL | GLM_TEXTURE);
glPopMatrix();



左膝蓋在大腿程式碼裡
glPushMatrix();///左膝
                    glTranslatef(-2.000 , +9.900 , 0 );
                    //glRotatef(angle, 0, 1, 0);
                    //glRotatef(angle2, 1, 0, 0);
                    glTranslatef(2.000 , -9.900 , 0 );
                    glmDraw(knee1, GLM_MATERIAL | GLM_TEXTURE);




左腳掌
glPushMatrix();///左腳掌
                        glTranslatef(-2.000 , +3.300 , 0 );
                        glRotatef(angle, 0, 1, 0);
                        glRotatef(angle2, 1, 0, 0);
                        glTranslatef(2.000 , -3.300 , 0 );
                        glmDraw(foot1, GLM_MATERIAL | GLM_TEXTURE);
                    glPopMatrix();





程式碼

-13




step14_下半身右半部







程式碼

-14



step15_關節

命名關節,用數字命名


程式碼

-15




step16

程式碼

-16




step17


程式碼

-17




step18


程式碼

-18

沒有留言:

張貼留言