2022年6月6日 星期一

week16

 1-1 利用excel學習內插法

    內插公式 alpha*新的+(1-alpha)*舊的

#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
///float alpha=0;
float angle[20],oldX=0;
float NewAngle[20],OldAngle[20];
GLMmodel * body = NULL;
GLMmodel * leftArm = NULL;
GLMmodel * lefthand = NULL;
GLMmodel * rightArm = NULL;
GLMmodel * righthand = NULL;
int angleID=0;///0:第0個關節,1:第1個關節,2:第2個關節
FILE * fout = NULL,* fin = NULL;
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void myWrite()
{
    if(fin!=NULL)
    {
        fclose(fin); fin=NULL;
    }
    if(fout==NULL)fout = fopen("file.txt","w+");
    for(int i=0;i<20;i++)
    {
         printf(     "%.2f ",angle[i]);
        fprintf(fout,"%.2f ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");///少了fclose,因為不想要才印一行就結束,想寫多行一點
}
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();///重畫畫面
}
void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++){
        angle[i]=alpha*NewAngle[i]+(1-alpha)*OldAngle[i];
        printf("%.2f",angle[i]);
    }
    printf("\n");
    glutPostRedisplay();
}
void timer(int t)
{
    float alpha =(t%30)/30.0;
    if(t%30==0) myRead();
    myInterpolate(alpha);
    glutTimerFunc(33,timer,t+1);
}
void keyboarad(unsigned char key,int x,int y)
{
    if(key=='p')
        myRead();
        glutTimerFunc(0,timer,0);
        //myInterpolate(alpha);
        //alpha=(alpha+0.01);
        //if(alpha>1) alpha=alpha-1;
    if(key=='r') myRead();
    if(key=='s') myWrite();///save
    if(key=='1') angleID=1;///預設這是第一個
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
}///用keyboarad的按鍵,來決定等一下motion()裡要改的
void mouse(int button,int state,int x,int y)
{///mouse按下去
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    ///myWrite();///沒有必要一直寫檔
    oldX=x;
    glutPostRedisplay();///重畫畫面Re display
}
GLMmodel * myReadOne(char*filename)
{
    GLMmodel * one = NULL;
    if( one == NULL ){
        one= glmReadOBJ(filename);
        glmUnitize(one);
        glmFacetNormals(one);
        glmVertexNormals(one,90);
    }
    return one;
}
void display()///準備新的display(),把每一塊都讀進來
{
    //glClearColor(0.2,0.2,0.4,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(body==NULL) body=myReadOne("data/body.obj");
    if(leftArm==NULL) leftArm=myReadOne("data/leftArm.obj");
    if(lefthand==NULL) lefthand=myReadOne("data/lefthand.obj");
    if(rightArm==NULL) rightArm=myReadOne("data/rightArm.obj");
    if(righthand==NULL) righthand=myReadOne("data/righthand.obj");
    glPushMatrix();
        glScalef(0.5,0.5,0.5);
        glRotatef(180,0,1,0);
        glTranslatef(0,0.5,0);
        glColor3f(1,0.5,0.5);
        glmDraw(body, GLM_SMOOTH);
    glPopMatrix();
    glPushMatrix();///右半邊
        glScalef(0.5,0.5,0.5);
        glTranslatef(-0.65,0.8,0);
        glRotatef(angle[1],0,0,1);
        glTranslatef(-0.25,-0.9,0);
        glColor3f(0,1,1);
        glmDraw(rightArm, GLM_SMOOTH);///上手臂
        glPushMatrix();
            glColor3f(1,1,1);
            glScalef(2,2,2);
            glTranslatef(-0.2,-0.5,0);
            glRotatef(angle[2],0,0,1);
            glTranslatef(0.05,-0.1,0);
            glmDraw(righthand, GLM_SMOOTH);///再畫右下手肘
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();///左半邊
        glScalef(0.5,0.5,0.5);
        glTranslatef(0.65,0.8,0);
        glRotatef(angle[3],0,0,1);
        glTranslatef(0.25,-0.9,0);
        glColor3f(0,1,1);
        glmDraw(leftArm, GLM_SMOOTH);///上手臂
        glPushMatrix();
            glColor3f(1,1,1);
            glScalef(2,2,2);
            glTranslatef(0.2,-0.5,0);
            glRotatef(angle[4],0,0,1);
            glTranslatef(-0.05,-0.1,0);
            glmDraw(lefthand, GLM_SMOOTH);///再畫右下手肘
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week15");
    glutKeyboardFunc(keyboarad);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);///不放Idle
    glEnable(GL_DEPTH_TEST);///3D的depth深度測試
    glDepthFunc(GL_LESS);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutMainLoop();
}


沒有留言:

張貼留言