2023年5月25日 星期四

91 week15

 到https://jsyeh.org/3dcg10/

下載win32、data檔案解壓縮



開啟Projection.exe

調整攝影機角度

look at:眼睛

中心點

向量位置


week15-1:
安裝git 打開git bash
cd desktop
git clone https://github.com/SeanC91/2023graphicsb
cd 2023graphicsb
start  .
開啟新專案freeglut連結
利用+-號控制角個數
添加程式碼到int main()上面:
float eyeX=0,eyeY=0;
void motion(int x,int y){
    eyeX=3*(x-320)/320.0;
    eyeY=3*(240-y)/240.0;
    glLoadIdentity();
    gluLookAt(eyeX,eyeY,-3,   0,0,-6,   0,1,0);
    glutPostRedisplay();
}
增加:glutMotionFunc(motion);到glut shape下面
gluPerspective(fovy,aspect,zNear,zFar);

fov:field of view  (y方向)
aspect:aspect ratio 長寬比
zNear

zFar

week15-2:
開新專案glut
y軸為準,犧牲x軸:
長寬比:const float ar = (float) width / (float) height;
兩行程式碼做切換:
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);///斜的(受邊界影響)
    glOrtho(-ar*3, ar*3, -1.0*3, 1.0*3, 2.0, 100.0);///正的(不受邊界影響)
切換第三種程式碼:
gluPerspective(60,ar,0.01,1000);
push上雲端:

week15-3:
滑鼠旋轉茶壺
#include <GL/glut.h>
void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void motion(int x,int y){
    glLoadIdentity();
    float eyeX=(x-150)/150.0,eyeY=(150-y)/150.0;
    gluLookAt(eyeX,eyeY,1,   0,0,0,   0,1,0);
    glutPostRedisplay();
}
void reshape(int w,int h){
    float ar=w/(float)h;
    glViewport(0,0,w,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);
}
int main(int argc,char**argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");

    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}
week15-4:
修改角度變陣列:
float angle[20]={};

glRotatef(angle[2],0,0,1);

glRotatef(angle[3],0,0,1);

angle[ID]+=x-oldX;

程式碼完整版:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel*head=NULL;
GLMmodel*body=NULL;
GLMmodel*uphandr=NULL;
GLMmodel*downhandr=NULL;
int show[4]={1,1,1,1};///week14_step03_1
int ID =2;///week14_step03_1
float teapotX=0,teapotY=0;
float angle[20]={};
FILE*fout=NULL;
FILE*fin=NULL;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID=0;///show[0]=!show[0];
    if(key=='1') ID=1;///show[1]=!show[1];
    if(key=='2') ID=2;///show[2]=!show[2];
    if(key=='3') ID=3;///show[3]=!show[3];
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head=glmReadOBJ("model/head.obj");
        body=glmReadOBJ("model/body.obj");
        uphandr=glmReadOBJ("model/uphandr.obj");
        downhandr=glmReadOBJ("model/downhandr.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glPushMatrix();
            ///glTranslatef(teapotX,teapotY,0);
            if(ID==0)glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[0]) glmDraw(head,GLM_MATERIAL);
    glPopMatrix();
     if(ID==1)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body,GLM_MATERIAL);
        glPushMatrix();
        glTranslatef(-1.360000,+0.500000,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(1.360000,-0.500000,0);
        if(ID==2)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[2]) glmDraw(uphandr,GLM_MATERIAL);
        glPushMatrix();
        glTranslatef(-1.959999,+0.080000,0);
        glRotatef(angle[3],0,0,1);
        glTranslatef(1.959999,-0.080000,0);
        if(ID==3)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[3]) glmDraw(downhandr,GLM_MATERIAL);
        glPopMatrix();
        glPopMatrix();
        glPopMatrix();
        glColor3f(0,1,0);
        glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX=x;
        oldY=y;
    }
}
void motion(int x,int y){
    teapotX+=(x-oldX)/150.0*3;
    teapotY-=(y-oldY)/150.0*3;
    printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY);
    angle[ID]+=x-oldX;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Final_Project");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}
git push上雲端







沒有留言:

張貼留言