2023年5月24日 星期三

SangMo 電腦圖學 Week15 攝影機、投影、運鏡

 

Step01-1 認識攝影機

到jsyeh.org/3dcg10/下載windows、data壓縮檔,把data丟進windows後開啟Projection檔案

eye:改變攝影機位置


center:讓攝影機看哪個位置
所以eye射向--->center

Step01-2

up:調整攝影機旋轉的角度(類似手機拍攝會歪一點,不一定正的)

Step01-3

介紹gluParspeotive()
fovy 看物體的視野角度(越高越遠反之越近)
aspect:壓縮攝影機的看物體的寬度(越高越壓縮反之越寬)

zNear:攝影機到物體的距離(越高越近)



zFar:攝影機拍物體的範圍(越低越近)

zNear、zFar:2者能圈出一個拍物體的範圍

Step02-1 開啟專案

打開codeblock 名稱為week15-1_gluPerspective,測試一遍看是不是下方的圖
是這樣就對了

Step02-2 認識gluPerspective


如果glOrtho數值照抄glFrustum 會發現變成下方圖片
所以要放大數值,放大三倍,得到一開始的圖
再改成gluPerspective(60,ar,0.01,100);
可以發現不用改太多數值就能得到一樣的圖
這三個就是轉換投影矩陣的程式(後面要補glLoadIdentity() ;還原單位矩陣)

Step02-3

開啟專案 week15-2_gluLookAt
並打下方程式
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void reshape(int w,int h)
{
    float ar = w/(float) h;
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
}
接者新增程式在reshape裡面 (完成會有胖茶壺)
void reshape(int w,int h)
{
    glViewport(0,0,w,h);///視窗裡.會看到的2D範圍
    float ar = w/(float) h;///aspect,長寬比
    glMatrixMode(GL_PROJECTION);///切換Project矩陣
    glLoadIdentity();///還原單位矩陣
    gluPerspective(60,ar,0.01,100);
    glMatrixMode(GL_MODELVIEW);///切換model view 矩陣
    glLoadIdentity();///還原單位矩陣
    gluLookAt(0,0,1,  0,0,0,  0,1,0);


    glutPostRedisplay();
}

Step02-4

新增motion程式在reshape下方
float eyeX=0,eyeY=0;
void motion(int x,int y)
{
    eyeX=(x-150.0)/150.0;
    eyeY=(150.0-y)/150.0;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(eyeX,eyeY,1,  0,0,0,  0,1,0);
    glutPostRedisplay();
}
底下還要給motion函式
修改完成


Step03-1

讓關節自己動自己的角度
先設定20個角度




存檔's'讀檔'r'
在keybroad函式加入
if(key=='s')
    {
        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("motiom.txt","r");
        for(int i=0;i<20;i++)
        {
            fscanf(fin,"%f",&angle[i]);
        }
    }
    glutPostRedisplay();
資料夾會冒出motion.txt 
裡面會記錄座標




沒有留言:

張貼留言