顯示具有 week16 標籤的文章。 顯示所有文章
顯示具有 week16 標籤的文章。 顯示所有文章

2022年6月25日 星期六

電腦圖學week16課堂筆記

 課堂內容 : 讓動畫自動動作、運鏡
1.第一個程式碼 : (複製之前的程式碼)
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
float newangle[20],oldangle[20];
int angleid=0;
FILE * fout = NULL , *fin =NULL;
void write()
{
    if(fout == NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++)
    {
        printf("%.1f",angle[i]);
        fprintf(fout,"%.1f",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
void read()
{
    if(fout != NULL) fclose(fout); fout=NULL;
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        oldangle[i]=newangle[i];
        fscanf(fin,"%f", &newangle[i]);
    }
    glutPostRedisplay();
}
void interpolate(float alpha)
{
    for(int i=0;i<20;i++)
    {
        angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];
    }
}
void timer(int t)
{
    if(t%50==0) read();
    interpolate( (t%50)/50.0 );
    glutPostRedisplay();
    glutTimerFunc(5,timer,t+1);
}

void key(unsigned char key,int x,int y)
{
    if(key=='p')
    {
        read();
        glutTimerFunc(0,timer,0);
    }
    if(key=='s') write();
    if(key=='r') read();
    if(key=='0') angleid=0;
    if(key=='1') angleid=1;
    if(key=='2') angleid=2;
    if(key=='3') angleid=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleid]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);
    glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);
        glPushMatrix();
            glTranslatef(-0.7,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week16");
    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
按 P 後動畫會自己動作

2.運鏡
程式碼 : 
#include <GL/glut.h>
void reshape(int w,int h)
{
    float ar =(float) w / (float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,3,
              0,0,0,
              0,1,0);
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
void motion (int x,int y)
{
     glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150)/150.0,(y-150)/150.0,3,
               0,0,0,
               0,1,0);
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week16");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}


2022年6月22日 星期三

Ru的電腦圖學筆記>

 

Step01

    1.利用Excel來把alpha內插公式實作出來, 看到新的、舊的資料可以做內插

        -alpha 內插公式:alpha: 0.0~1.0

        -angle = alpha*新 + (1-alpha)*舊

    2.拿 week15_angles_TRT_again 來改

        -建立新的 GLUT 檔案,檔名:week16_interpolation

Step02

    1.講解上一週作業

    2.利用課本的範例,講解gluLookAt(眼睛座標、看哪裡、up向量)

        -開啟之前在老師網頁下載的 windows/data/Projection.exe



Step03

    1.建立新的 GLUT 檔案,檔名:week16_camera_projection_GlutLookat




2022年6月7日 星期二

END之書 week16

 1.alpha公式: alpha: 0.0~1.0


angle=angle*新+(1-angle)*舊


alpha: 0 => 舊

alpha: 0.5 => 新/舊

alpha: 1 => 新


1.1 動畫補間?

 #include <GL/glut.h>

#include <stdio.h>

float angle[20],oldX=0;


float newangle[20],oldangle[20];


int angleid=0;///0右手 1右手關節 2左手 3左手關節

FILE * fout = NULL , *fin =NULL;

void write()

{

    if(fout == NULL) fout = fopen("file.txt","w+");


    for(int i=0;i<20;i++)

    {

        printf("%.1f",angle[i]);///印出黑視窗

        fprintf(fout,"%.1f",angle[i]);///印出檔案

    }///印出20個數字

    printf("\n");///每呼叫一次,黑視窗跳行

    fprintf(fout,"\n");///每呼叫一次,檔案跳行

}


void read()

{

    if(fout != NULL) { fclose(fout); fout=NULL; }

    if(fin==NULL) fin=fopen("file.txt","r");

    for(int i=0;i<20;i++)

    {

        oldangle[i]=newangle[i];///原來新的變舊的

        fscanf(fin,"%f", &newangle[i]);讀新角度

    }

    glutPostRedisplay();///重畫畫面

}


void interpolate(float alpha)

{

    for(int i=0;i<20;i++)

    {

        angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];

    }

}


int t=0;


void key(unsigned char key,int x,int y)

{

    if(key=='p')  ///p要一直按著

    {

        if(t%30==0) read();

        interpolate( (t%30)/30.0 );///介於0.0到1.0

        glutPostRedisplay();

        t++;

    }


    if(key=='s') write();///調好動作後存檔

    if(key=='r') read();

    if(key=='0') angleid=0;

    if(key=='1') angleid=1;

    if(key=='2') angleid=2;

    if(key=='3') angleid=3;

}

void mouse(int button,int state,int x,int y)

{

    oldX=x;

}

void motion(int x,int y)

{

    angle[angleid]+=(x-oldX);

    ///write();      不再複讀

    oldX=x;

    glutPostRedisplay();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.3,0.5,-0.3,-0.5);

    glPushMatrix();

        glTranslatef(0.3,0.4,0);///手臂掛回身體

        glRotatef(angle[0],0,0,1);///對z軸旋轉

        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心

        glColor3f(1,0,0);

        glRectf(0.3,0.5,0.7,0.3);


        glPushMatrix();

            glTranslatef(0.7,0.4,0);///手臂掛回身體

            glRotatef(angle[1],0,0,1);///對z軸旋轉

            glTranslatef(-0.7,-0.4,0);///手臂旋轉軸放中心

            glColor3f(0,1,0);

            glRectf(0.7,0.5,1.0,0.3);///下手肘

        glPopMatrix();


    glPopMatrix();


    glPushMatrix();

        glTranslatef(-0.3,0.4,0);///手臂掛回身體

        glRotatef(angle[2],0,0,1);///對z軸旋轉

        glTranslatef(0.3,-0.4,0);///手臂旋轉軸放中心

        glColor3f(1,0,0);

        glRectf(-0.3,0.5,-0.7,0.3);


        glPushMatrix();

            glTranslatef(-0.7,0.4,0);///手臂掛回身體

            glRotatef(angle[3],0,0,1);///對z軸旋轉

            glTranslatef(0.7,-0.4,0);///手臂旋轉軸放中心

            glColor3f(0,1,0);

            glRectf(-0.7,0.5,-1.0,0.3);///下手肘

        glPopMatrix();


    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15 trt 2");


    glutMouseFunc(mouse);

    glutKeyboardFunc(key);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}


#先存數個座標

1-2 按一次P自動補間

 #include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;

float newangle[20],oldangle[20];

int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL , *fin =NULL;
void write()
{
    if(fout == NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++)
    {
        printf("%.1f",angle[i]);///印出黑視窗
        fprintf(fout,"%.1f",angle[i]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}

void read()
{
    if(fout != NULL) { fclose(fout); fout=NULL; }
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        oldangle[i]=newangle[i];
        fscanf(fin,"%f", &newangle[i]);
    }
    glutPostRedisplay();///重畫畫面
}

void interpolate(float alpha)
{
    for(int i=0;i<20;i++)
    {
        angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];
    }
}

///int t=0;

void timer(int t)
{
    if(t%50==0) read();
    interpolate( (t%50)/50.0 );///介於0.0到1.0
    glutPostRedisplay();
    glutTimerFunc(5,timer,t+1);

}

void key(unsigned char key,int x,int y)
{
    if(key=='p')
    {
        read();
        glutTimerFunc(0,timer,0);
        ///if(t%30==0) read();
        ///interpolate( (t%30)/30.0 );///介於0.0到1.0
        ///glutPostRedisplay();
        ///t++;
    }

    if(key=='s') write();///調好動作後存檔
    if(key=='r') read();
    if(key=='0') angleid=0;
    if(key=='1') angleid=1;
    if(key=='2') angleid=2;
    if(key=='3') angleid=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleid]+=(x-oldX);
    ///write();      不再複讀
    oldX=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);
    glPushMatrix();
        glTranslatef(0.3,0.4,0);///手臂掛回身體
        glRotatef(angle[0],0,0,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.4,0);///手臂掛回身體
            glRotatef(angle[1],0,0,1);///對z軸旋轉
            glTranslatef(-0.7,-0.4,0);///手臂旋轉軸放中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);///下手肘
        glPopMatrix();

    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛回身體
        glRotatef(angle[2],0,0,1);///對z軸旋轉
        glTranslatef(0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);

        glPushMatrix();
            glTranslatef(-0.7,0.4,0);///手臂掛回身體
            glRotatef(angle[3],0,0,1);///對z軸旋轉
            glTranslatef(0.7,-0.4,0);///手臂旋轉軸放中心
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1.0,0.3);///下手肘
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 trt 2");

    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-1 運鏡

window => Projection.exe

<= eye => 鏡頭位置
<= center => 鏡頭看的位置
<= up => 畫面旋轉角度

2-2 resize

#include <GL/glut.h>

void reshape(int w,int h)   ///不能整數除法
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D=>2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(1,0,3,///eye
                       0,1,0,///center看哪裡
                       0,0,1);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camera");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}


2-3 用滑鼠改變鏡頭位置

#include <GL/glut.h>

void reshape(int w,int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D=>2D
    glLoadIdentity();
    gluPerspective(60,ar,1,10);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(1,0,3,///eye
                       0,1,0,///center看哪裡
                       0,0,2);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}

void motion(int x,int y)
{
    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt( (x-150)/150.0 , (y-150)/150.0 , 3 ,///eye
              0,1,0,///center看哪裡
              0,0,2);///up向量
    glutPostRedisplay();
}

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

    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}


VERY BEAUTIFUL,VERY POWERFUL


一

1-1.alpha內插公式 : alpha : `0.0~1.0`

       `angle = alpha * 新 + ( 1 - alpha ) * 舊`
      ex.
      - alpha : 0 => 舊
      - alpha : 0.5 => 半新半舊
      - alpha : 1 => 新

1-2.開glut新專案,用上週TRT_again程式改
///
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
FILE * fout =NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf(" %.1f ",angle[i]);
        fprintf(fout," %.1f ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
float NewAngle[20],OldAngle[20];
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL)fin=fopen("file.txt,","r");
    for(int i=0;i<20;i++){
        OldAngle[i]=NewAngle[i];
        fscanf(fin,"%f",&NewAngle[i]);
        ///fscanf(fin," %f ",&angle[i]);
    }
    glutPostRedisplay();
}

void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++){
        angle[i]=alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
int t=0;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        if(t%30==0) myRead();
        myInterpolate( (t%30)/30.0 );
        glutPostRedisplay();
        t++;
    }
    if(key=='s') myWrite();
    if(key=='r') myRead();
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    ///myWrite();
    oldX=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5, -0.3,-0.5);
    glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5, 0.7,0.3);
        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5, 1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5, -0.7,0.3);
        glPushMatrix();
            glTranslatef(-0.7,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.7,0.5, -1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 angle TRT again");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
///
長按P會有動畫

///
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
FILE * fout =NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf(" %.1f ",angle[i]);
        fprintf(fout," %.1f ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
float NewAngle[20],OldAngle[20];
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL)fin=fopen("file.txt,","r");
    for(int i=0;i<20;i++){
        OldAngle[i]=NewAngle[i];
        fscanf(fin,"%f",&NewAngle[i]);
        ///fscanf(fin," %f ",&angle[i]);
    }
    glutPostRedisplay();
}

void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++){
        angle[i]=alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
///int t=0;
void timer(int t)
{
    myInterpolate((t%50)/50.0);
    glutPostRedisplay();
    glutTimerFunc(20,timer,t+1);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        myRead();
    glutTimerFunc(0,timer,0);
        ///if(t%30==0) myRead();
        ///myInterpolate( (t%30)/30.0 );
        ///glutPostRedisplay();
        ///t++;
    }
    if(key=='s') myWrite();
    if(key=='r') myRead();
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    ///myWrite();
    oldX=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5, -0.3,-0.5);
    glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5, 0.7,0.3);
        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5, 1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5, -0.7,0.3);
        glPushMatrix();
            glTranslatef(-0.7,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.7,0.5, -1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 angle TRT again");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
///
二
開啟windows/projection
查看如何運鏡
三
///
#include <GL/glut.h>

void reshape(int w/int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(0,0,3,///eye
                       0,0,0,///center看哪裡
                       0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camara");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}
///


再改成以下
///
#include <GL/glut.h>

void reshape(int w,int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(0,0,3,///eye
                      0,0,0,///center看哪裡
                      0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
void motion (int x,int y)
{
    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt((x-150)/150.0,(y-150)/150.0,3,///eye
              0,0,0,///center看哪裡
              0,1,0);///up向量
    glutPostRedisplay();///請重畫畫面
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camara");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}
///

筆記~圖學 Week16

程式Projection

1.eye:眼睛位置,center:眼睛往哪看,up:正看斜看

程式interpolation

0.alpha內插公式:angle = alpha*新 + (1-alpha)*舊

(1) alpha=0,angle=舊;alpha=1,angle=新。

(2) 可用Excel來套公式自動計算出所需角度值。

1.alpha內插

2.找物件中心點技巧

(1) 放個圓球在視窗中間。

(2) 從上層物件開始調。

(3) 調下層物件時,隱藏上層物件,留上層位置。

3.自動alpha內插

4.改視角,aspect ratio:長寬比(寬:長)

圖學筆記Week16

 本週課程內容:內插公式、動作內插、運鏡

內插公式:

alpha內插公式(alpha值0~1): angle = alpha*新+(1-alpha)*舊

alpha:0=>舊、alpha:0.5=>半新半舊、alpha:1=>新


動作內插

步驟:開啟新專案檔-->使用15週程式-->新增功能-->按下"p"能做動作-->myinterpolate函式功能:內插公式應用(如下圖)-->myRead函式功能:陣列值座新舊交換-->只按一次"p"動作連續(如下圖)



運鏡
eye=>相機移動
center=>物體移動
up=>相機、物體不動,畫面旋轉

步驟:開啟新專案檔-->aspect ratio長寬比(1920x1080)16:9、4:3-->reshape函式控制長寬比(如下圖)-->motion改變lookat參數(如下圖)



VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...