2023年5月24日 星期三

week15電腦圖學 Camera

 week15

先到https://jsyeh.org/3dcg10/下載

[win32][data]

把data資料夾放到win32資料夾中


接著執行projection.exe

center->畫面中心
eye->從眼睛看
up->手機拍照食指方向,選轉後食指指的方向



fovy->field of view(y方向)視野的角度

aspect->aspect ratio長寬比

zNear->最近從拿裡開始
zFar->最遠到哪



week15-1
開一個專案
放到final project 的glut裡面
調整 角度 比例 遠 近



#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;///aspect ratuio長寬比
    glMatrixMode(GL_PROJECTION);///先切換到Project矩陣
    glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)
    gluPerspective(60,ar,0.01,1000);///一片空白,在茶壺裡

    glMatrixMode(GL_MODELVIEW);
    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);///老師把名字變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);///視窗會看到2D範圍
    float ar=w / (float) h;///aspect ratuio長寬比
    glMatrixMode(GL_PROJECTION);///先切換到Project矩陣
    glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)
    gluPerspective(60,ar,0.01,1000);///一片空白,在茶壺裡

    glMatrixMode(GL_MODELVIEW);///做好後切回modle view矩陣
    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);///老師把名字變reshape

    glutMainLoop();
}



開啟final project專案

#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] = {1, 1, 1, 1};
int ID = 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();
    }

    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2, 0.2, 0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");

        }
        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.200000, +0.453333, 0);
            glRotatef(angle[2], 0, 0, 1);

            glTranslatef(1.200000, -0.453333, 0);

            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);
            glPushMatrix();
                glTranslatef(-1.959999, +0.113333, 0);
                glRotatef(angle[3], 0, 0, 1);
                glTranslatef(1.959999, -0.113333, 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;

    }
    display();
}

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

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

    glutMainLoop();
}






沒有留言:

張貼留言