2022年6月7日 星期二

古利夏醫生的艾爾迪亞日記week16

每天睡醒肚子都好餓

這邊來個alpha公式

alpha公式: alpha: 0.0~1.0

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

alpha: 0 => 舊

alpha: 0.5 => 新/舊

alpha: 1 => 新

開個新專案 還是拿上周的TRT_again程式改

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

}

step2 運鏡

打開Projection.exe

eye鏡頭位置
center鏡頭看的位置
up畫面旋轉角度
程式碼:
#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();
}

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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