2022年6月7日 星期二

獻祭肝臟的電腦圖學課堂筆記_week16

  Step01:

1.alpha內插法

alpha:00.0~1.0

angle=alpha*new+(1-alpha)*old

                                                                                                                                                                                                                                                                                                                                                         
2.讀入上週程式
 #include <GL/glut.h>
#include <stdio.h>
float ang    le[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");
}
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        fscanf(fin, "%f", &angle[i]);
    }
    glutPostRedisplay();//重畫畫面
}
void keyboard(unsigned char key,int x,int y){
    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,.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,.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);
    glutInitWindowSize(600,600);
    glutCreateWindow("week15_ANGLE_TRT_Again");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
3.加入
void myInterpolate(float alpha){///alpha內插法
    for(int i=0;i<20;i++){
        angle[i]=alpha*Newangle[i]+(1-alpha)+Oldangle[i];
    }
}
在keyboard()加

int t=0;

if(key=='p'){///按p逐步加入內插
        if(t%30==0)myRead();
        myInterpolate(t%30/30);
        glutPostRedisplay();
        t++;
    }
修改mtRead()
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]);///讀新的
    }
    glutPostRedisplay();//重畫畫面
}
4.加入timer()
void timer(int t){
    if(t%50==0)myRead();
    myInterpolate(t%50/50.0);
    glutTimerFunc(20,timer,t+1);
}
5.修改keboard()
if(key=='p'){
        myRead();
        glutTimerFunc(0,timer,0);
    }

Step02:
1.開啟projection.exe
類似從眼睛視角看出去
glutLookAt(0.00,0.00,2.00,0.00    眼睛
                    0.00,0.80,0.00    中心
                    0.00,1.00,0.00);   上方(向量)
aspect ratio 長寬比ex:16:9,4:3
3.寫入
#include <GL/glut.h>
void reshape(int w,int h){
    const float ar = (float) w / (float) h;

    glViewport(0, 0, w, h);///3D變2D
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    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();
}
int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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