2023年5月25日 星期四

大吃十五斤Week15

 week15

從https://jsyeh.org/3dcg10/下載data和win32解壓縮後把data裡面的data複製進windows資料夾,然後開啟Projection.exe
把2023graphicsb資料夾從github下載下來

week15-1_gluLookAt

在main函式前加入motion函式

float eyeX = 0,eyeY = 0;
void motion(int x,int y)///加入motion函式
{
    eyeX = 3*(x-320)/320.0;///算出eyeX座標
    eyeY = 3*(240-y)/240.0;///算出eyeY座標
    glLoadIdentity();///還原最一開始開始的矩陣
    gluLookAt(eyeX,eyeY,3,         0,0,-6,            0,1,0);
                         ///eye位置      ///中間主角   ///up向量
    glutPostRedisplay();///重畫畫面
}

在main函式中加入glutMotionFunc(motion);

可以轉視角

切換glOrtho垂直投影  glFrustum梯形投影  gluPerspective透視投影  三種模式

長寬比

week15-2_gluPerspective

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.0*3, 1.0*3, 2.0, 100.0); ///垂直投影
    glMatrixMode(GL_MODELVIEW); /// 切換成modle view矩陣

    glLoadIdentity() ; ///設成單位矩陣
}

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.0*3, 1.0*3, 2.0, 100.0); ///垂直投影 ///註解掉

    gluPerspective(60, ar, 0.01, 1000); ///透視投影

    glMatrixMode(GL_MODELVIEW); /// 切換成modle view矩陣

    glLoadIdentity() ; ///設成單位矩陣
}

week15-3_gluPerspective_gluLookAt

#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();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

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

Final_Project

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * right_arm = NULL;
GLMmodel * left_arm = NULL;
int show[4] ={1,1,1,1};
int ID = 3;
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;
    if( key=='1' ) ID=1;
    if( key=='2' ) ID=2;
    if( key=='3' ) ID=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");
        right_arm = glmReadOBJ("model/right_arm.obj");
        left_arm = glmReadOBJ("model/right_hand.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        ///glTranslatef(teapotX,teapotY,0);
        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.360000,+0.360000,0);
            glRotatef(angle[2],0,0,1);
            glTranslatef(1.360000,-0.360000,0);

            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(right_arm, 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(left_arm, 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;
    teapotY -= (y-oldY)/150.0;
    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("week15");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}







沒有留言:

張貼留言