2023年5月24日 星期三

Code Talker Week15 攝影機,投影

 STEP 01

1.到https://jsyeh.org/3dcg10/下載data/win32,並解壓縮

2.執行projection.exe,試了解gluLookAt,我們看到三個東西都各有三個參數,分別是eye,center,up

3.透過調整數字,我們大致明白eye是從哪裡看,center是看哪裡

STEP 02

1.up是關於從eye到center,他們之間會形成一個軸線,up會以該軸線作旋轉軸,以360度為限(一圈),跟攝影機直拍橫拍反著拍有關係,偶爾斜拍,up向量也會向著斜斜的方向

STEP 03

1.上面還有一行程式碼,gluPerspective,他有四個參數值,第一個是fovy,是視野的角度,或許是類似廣角鏡頭的效果,第二個是aspect是長寬比,預設是1,也就是1:1,如果調整成2,那就是2:1,第三個和第四個zNear,zFar,是指視線的最近範圍和最遠範圍

STEP 04

1.用git指令把程式倉庫複製到桌面

2.開啟一個新專案,我們直接建立在程式倉庫裡,並直接使用裡面的freeglut

3.在預設的程式碼加上程式碼,如圖:

4.我們把他改造成gluPerspective:
STEP 05

1.建立另一個新專案

2.打上程式碼:

#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){

    glViewport(0, 0, w, h);///視窗裡會看到的2D範圍

    float ar = w / (float) h;///長寬比
    glMatrixMode(GL_PROJECTION);///切換成投影矩陣
    glLoadIdentity();///矩陣清空,還原成單位矩陣
    gluPerspective(60, ar, 0.01, 1000);

    glMatrixMode(GL_MODELVIEW);///切換成MODEL VIEW矩陣
    glLoadIdentity();///還原成單位矩陣
    gluLookAt(0,0,1, 0,0,0, 0,1,0);

    glutPostRedisplay();
}
int main(int argc, char** argv)

{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week15");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();

}

我們從很近的位置看茶壺,茶壺看起來比較大:

STEP 06

1.我們增加motion函式,令其可以藉由滑鼠調整鏡頭觀看的角度,圖以右上看茶壺為例






STEP07

1.打開final_project

2.將角度改為陣列,程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {0, 1, 1, 1};
int ID=3;//0頭1身2上手臂3下手臂
void keyboard(unsigned char key, int x, int y){
    if(key=='0') ID=0;
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    glutPostRedisplay();
}

FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0, teapotY=0;
float angle[20]={};
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glScalef(0.2, 0.2, 0.2);
        if(body==NULL){
            body = glmReadOBJ("model/body.obj");
            head = glmReadOBJ("model/head.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");

            //glmUnitize(body);
        }
        if(ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[0]) glmDraw(head, GLM_MATERIAL);

        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body, GLM_MATERIAL);
        glPushMatrix();
            glTranslatef(-1.186666, +0.380000, 0);
            glRotatef(angle[2], 0, 0, 1);
            glTranslatef(1.186666, -0.380000, 0);
            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);

            glPushMatrix();
                //glTranslatef(teapotX, teapotY, 0);
                glTranslatef(-1.953333, +0.120001, 0);
                glRotatef(angle[3], 0, 0, 1);
                glTranslatef(1.953333, -0.120001, 0);
                if(ID==3) glColor3f(1,0,0);
                else glColor3f(1,1,1);
                if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
            glPopMatrix();

        glPopMatrix();

    glPopMatrix();
    glColor3f(0,1,0);
    glutSolidTeapot(0.02);
    glutSwapBuffers();

}
int oldX = 0, oldY = 0;
void motion(int x, int y){
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    angle[ID] += (x-oldX);
    oldX = x;
    oldY = y;
    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;
        oldY = y;
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        if(fout==NULL) fout = fopen("file4.txt","w");
        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
//void keyboard(unsigned char key,int x, int y)
//{
//    if(fin==NULL){
//        fclose(fout);
//        fin = fopen("file4.txt","r");
//    }
//    fscanf(fin, "%f %f", &teapotX, &teapotY);
//    display();
//}
int main(int argc, char *argv[])//main()主函式 進階版

{

    glutInit(&argc,argv);//把參數送給glutInit初始化

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能

    glutCreateWindow("week15");//開GLUT視窗



    glutDisplayFunc(display);//顯示用的函式

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);



    glutMainLoop();

}

就能夠轉動選擇的位置





3.想用開檔案的方式作簡易的動畫,於是將程式碼改成這樣:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {0, 1, 1, 1};
int ID=3;//0頭1身2上手臂3下手臂
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0, teapotY=0;
float angle[20]={};
void keyboard(unsigned char key, int x, int y){
    if(key=='0') ID=0;
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    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("motion.txt", "r");
        for(int i=0;i<20;i++){
            fscanf(fin, "%f", &angle[i] );
        }
        glutPostRedisplay();
    }
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    glutPostRedisplay();
}

void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glScalef(0.2, 0.2, 0.2);
        if(body==NULL){
            body = glmReadOBJ("model/body.obj");
            head = glmReadOBJ("model/head.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");

            //glmUnitize(body);
        }
        if(ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[0]) glmDraw(head, GLM_MATERIAL);

        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body, GLM_MATERIAL);
        glPushMatrix();
            glTranslatef(-1.186666, +0.380000, 0);
            glRotatef(angle[2], 0, 0, 1);
            glTranslatef(1.186666, -0.380000, 0);
            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);

            glPushMatrix();
                //glTranslatef(teapotX, teapotY, 0);
                glTranslatef(-1.953333, +0.120001, 0);
                glRotatef(angle[3], 0, 0, 1);
                glTranslatef(1.953333, -0.120001, 0);
                if(ID==3) glColor3f(1,0,0);
                else glColor3f(1,1,1);
                if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
            glPopMatrix();

        glPopMatrix();

    glPopMatrix();
    glColor3f(0,1,0);
    glutSolidTeapot(0.02);
    glutSwapBuffers();

}
int oldX = 0, oldY = 0;
void motion(int x, int y){
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    angle[ID] += (x-oldX);
    oldX = x;
    oldY = y;
    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;
        oldY = y;
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        //if(fout==NULL) fout = fopen("file4.txt","w");注:只能有一個開檔案,不然會出錯,這裡註解掉
        //fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
//void keyboard(unsigned char key,int x, int y)
//{
//    if(fin==NULL){
//        fclose(fout);
//        fin = fopen("file4.txt","r");
//    }
//    fscanf(fin, "%f %f", &teapotX, &teapotY);
//    display();
//}
int main(int argc, char *argv[])//main()主函式 進階版

{

    glutInit(&argc,argv);//把參數送給glutInit初始化

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能

    glutCreateWindow("week15");//開GLUT視窗



    glutDisplayFunc(display);//顯示用的函式

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);



    glutMainLoop();

}

按S能存檔案,R可以讀檔案

沒有留言:

張貼留言