2023年5月24日 星期三

week15 Camera攝影機、期末作業

 Week15攝影機

https://jsyeh.org/3dcg10/

先下載data windows,然後解壓縮把data丟到windows打開模型

介紹:在glutlookat()最後三個參數是up的向量

前面的eye和center會決定攝影機的軸線,從eye射向center。

攝影機會照軸線再360轉動,這個是由up向量來決定的。


step2開啟專案

檔名:week15-1_gluperspective


改數值 glOrtho(-ar*3, ar*3, -1*3, 1*3, -100, +100);


程式碼:

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);

    glMatrixMode(GL_MODELVIEW);///切換model view矩陣

    glLoadIdentity() ;///還原成單位矩陣

}





建一個新專案


程式碼:
#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);
    float ar = w/(float) h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.01,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,1, 0,0,0, 0,1,0);
    ///在0,0,1看著茶壺0,0,0 up是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();
}

胖胖的茶壺

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

    float ar = w/(float) h;

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(60,ar,0.01,1000);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    gluLookAt(0,0,1, 0,0,0, 0,1,0);

    ///在0,0,1看著茶壺0,0,0 up是0,1,0

    glutPostRedisplay();

    }

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();

}

int main(int argc,char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week15");

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMainLoop();

}

滑鼠按著移動可以看到茶壺有立體感


打開final project(因安裝github當掉,所以git沒有Final Project)

導致無法做以下動作




沒有留言:

張貼留言