2023年5月24日 星期三

sheeba - week15 - Camera 投影 運鏡

 Step01-1

https://jsyeh.org/3dcg10/下載windows.zip、data.zip

將下載好的windows檔案與data檔案解壓縮   把data丟到windows裡


開啟projection.exe調整數值觀察


Step02-1

建立GLUT專案   命名為week15-1_gluPerspective


#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

static int slices = 16;
static int stacks = 16;

/* GLUT callback Handlers */

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

Step02-2



#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

static int slices = 16;
static int stacks = 16;

/* GLUT callback Handlers */

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);///week15_step02-2 切換成投影矩陣(暫時
    glLoadIdentity();///week15_step02-2還原成單位矩陣(暫時
    ///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);
///week15_step02-2角度,比例,近的,遠的

    glMatrixMode(GL_MODELVIEW);///week15_step02-2 切換成 Model view 矩陣
    glLoadIdentity() ;///week15_step02-2 還原成單位矩陣
}

Step02-3

建立GLUT專案   命名為week15-2_gluLookAt



///week15-2_gluLookAt
///函式reshape(), display()
#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 ratio長寬比

    glMatrixMode(GL_PROJECTION);///先切換到projection矩陣
    glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)
    gluPerspective(60, ar, 0.01, 1000);

    glMatrixMode(GL_MODELVIEW);///做好後馬上切回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);///做好後馬上切回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();
}

沒有留言:

張貼留言