2023年5月24日 星期三

ww-week15-攝影機 Camera_投影、運鏡

1.認識攝影機

在https://jsyeh.org/3dcg10/中下載[data][win32]三個檔案 / 在資料夾中開啟後分別解壓縮 / 將data資料夾放到window裡 / 開啟Projection.exe

gluLookAt()的九個參數:
    前三個是眼睛要放的位置
    中間三個調整頭的旋轉(左右旋轉、上下、深淺)
    最後三個up決定攝影機的自轉像拍一樣食指舉起來往左往右代表已z軸轉

gluPerspective()的四個參數:
    fovy(filed of view)代表y方向視野的角度(雨傘要開多大的角度)
    aspect(aspect ratio)長寬比,梯形上底切面的長寬比,對應視窗長寬
    zNear近的切面往z軸的深淺度越大越遠
    zFar梯形遠的切面的深度(如圖)
glOrtho()方塊攝影機,呈現front view的樣子,6個參數分別為方塊的六個面
glFrustum()較進階的梯形傘狀攝影機,六個面一樣可以調整

2.實作

git clone 2023graphicsa下來/開啟專案:week15-1_gluPerspective/將傳直接存在2023graphisa裡/freeglut選擇Final_project裡的來用就行了/

加入glOrtho( -ar*3, ar*3, -1*3, 1*3, -100, +100); 

比較glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

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

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    ///glOrtho( -ar*3, ar*3, -1*3, 1*3, -100, +100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}
...
加入gluPerspective( 60, ar, 0.01, 1000););///(張角60度,比例,近的,遠的)做比較

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

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);///切換成投影矩陣
    glLoadIdentity();///還原成單位矩陣//臨時切過來
    //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    //glOrtho( -ar*3, ar*3, -1*3, 1*3, -100, +100);
    gluPerspective( 60, ar, 0.01, 1000);///(張角60度,比例,近的,遠的)


    glMatrixMode(GL_MODELVIEW);///切換成Model View 矩陣
    glLoadIdentity() ;///還原成單位矩陣//馬上還回去
}
...
3.
開啟新檔:week15-1_gluLookAt/reshape()、display()畫茶壺

motion()用滑鼠gluLookAt()

save everything/備份到github 

4. 期末作業

開啟專案:開啟2023Grahhicsa 的Final_Project / file / open 開啟.cbp檔/加一個ID/備份
week15-3用陣列來切換不同關節float angle[20]/motion()時用[ID]來改 


加入存檔讀檔 :將宣告都拉到程式碼前面/S存檔動一步按一次  Final_project內會有motion.txt檔 R讀檔按一下走一步


#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"///week13-1
GLMmodel * head =NULL;///week13-1
GLMmodel * uparmR =NULL;///week13-1
GLMmodel * lowarmR =NULL;///week13-1
GLMmodel * body =NULL;///week13-1
int show[4]={1,1,1,1};///week13-3///week14 3-2
int ID = 3;///week14-3-1 //0頭 1身體 2上手臂 3下手臂///week14 3-2
FILE * fout=NULL;///step2-2
FILE * fin =NULL;///2
float teapotX=0,teapotY=0;
float angle[20]={};///week15-3改成用陣列
//float angle=0,angle2=0,angle3=0;///week15-3改成用陣列
void keyboard(unsigned char key,int x, int y){
    if(key=='0') ID = 0;///week14 3-1
    if(key=='1') ID = 1;///week14 3-1
    if(key=='2') ID = 2;///week14 3-1
    if(key=='3') ID = 3;///week14 3-1
    if(key=='s'){///week15-4存檔,讀檔
        if(fout==NULL)fout = fopen("motion.txt","w");
        for(int i=0;i<20;i++){
            fprintf(fout,"%.2f ",angle[i]);
        }
        fprintf(fout,"\n");
    }else if (key=='r'){
        if(fin==NULL)fin = fopen("motion.txt","r");
        for(int i=0;i<20;i++){
            fscanf(fin,"%f",&angle[i]);
        }
        glutPostRedisplay();
    }

    ///if(key=='0') show[0] = !show[0];///week13-3
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    glutPostRedisplay();
}///原來的keyboard先註解

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);///week13-2
        if(body==NULL){///week13-1
            head = glmReadOBJ("model/head.obj");///week13-2
            body = glmReadOBJ("model/body.obj");///week13-1
            uparmR = glmReadOBJ("model/uparmR.obj");///week13-3
            lowarmR = glmReadOBJ("model/lowarmR.obj");///week13-3
            ///glmUnitize(body);///week13-1
        }
        if(ID==0)glColor3f(1,0,0);///week14-3-1
        else glColor3f(1,1,1);///week14-3-1
        if(show[0]) glmDraw(head, GLM_MATERIAL);///week13-1

        if(ID==1)glColor3f(1,0,0);///week14-3-1
        else glColor3f(1,1,1);///week14-3-1
        if(show[1]) glmDraw(body, GLM_MATERIAL);///week13-2

        glPushMatrix();///week13-4
            glTranslatef(-1.200356,+0.466667,0);///week14-3-2
            //glTranslatef(teapotX,teapotY,0);
            glRotatef(angle[2],0,0,1);///week14-3-2 TRT建出來///week15-3改成用陣列
            glTranslatef(1.200356,-0.466667,0);///week14-3-2

            if(ID==2)glColor3f(1,0,0);///week14-3-1
            else glColor3f(1,1,1);///week14-3-1
            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);///week13-3

            glPushMatrix();
                glTranslatef(-1.946666,0.126667,0);///幫忙找出TRT移動的值///week14-3-3
                glRotatef(angle[3],0,0,1);///week14-3-2 TRT建出來///week15-3改成用陣列
                glTranslatef(1.946666,-0.126667,0);///week14-3-2

                if(ID==3)glColor3f(1,0,0);///week14-3-1
                else glColor3f(1,1,1);///week14-3-1
                if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);///week13-3
            glPopMatrix();

        glPopMatrix();///week13-4


    glPopMatrix();

    glColor3f(0,1,0);///week14-3-2放個小茶壺在中心當參考點
    glutSolidTeapot(0.02);///week14-3-2
    glutSwapBuffers();
}
int oldX=0,oldY=0;///week13-4
void motion (int x,int y){///week13-4
    teapotX += (x-oldX)/150.0;
    teapotY-= (y-oldY)/150.0;
    angle[ID] += (x-oldX);///week15-3改成用陣列//移到上面
    oldX = x;
    oldY = y;
    //angle= x;///week14-3-2///week15-3改成用陣列
    printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN){
        oldX = x;///week13-4teapotX = (x-150)/150.0;
        oldY = y;///week13-4teapotY = (150-y)/150.0;
        //angle =x;///week13-4///week15-3改成用陣列
        ///if(fout==NULL)fout =fopen("file4.txt","w");week12
        ///fprintf(fout,"%f %f\n",teapotX,teapotY);
    }
    display();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);///week13-4
glutKeyboardFunc(keyboard);///2keboard要做事了

    glutMainLoop();
}




git指令

cd desktop
git clone https://github.com/你的帳號/2023graphicsa
cd 2023graphicsa
加入檔案
git add . 
git status

git config --global user.email "jsyeh@mail.mcu.edu.tw"

git config --global user.name "jsyeh"

git commit -m "week10"

git push




沒有留言:

張貼留言