2018年6月11日 星期一

Week 16 X+7 的筆記(學期成果)

///雲端共用影片網址::
https://drive.google.com/open?id=1wJ8zlOVsBG6_7VATab9eiaqfSw_0Zkbi




















操作後無顏色_失敗maya模型::

























































程式碼::
#include <Gl/glut.h> 
#include <mmsystem.h>
#include "CMP3_MCI.h"
#include <stdio.h>
#include "glm.h"
GLMmodel * pmodel=NULL;
GLMmodel * pmodell=NULL;
GLMmodel * pmodelll=NULL;     ///老師我不知道為什麼卡到中間就停下來了??
GLMmodel * pmodellll=NULL;
GLMmodel * pmodelllll=NULL;      ///數值有一直跑,但畫面不會動

FILE * fout=NULL;
FILE * fin=NULL;
CMP3_MCI myMP3;

float angle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};///Now4: 用陣列
float oldAngle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};
float newAngle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};
int oldX=0, now=0;
float alpha=0;

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(1,1,3, 0,0,0 , 0,1,0);
}
void timer(int t)
{
    printf("%d\n",t);
    glutTimerFunc(30, timer,t+1);

    if(t%100==0)
    {
        if(fin==NULL)fin=fopen("output.txt","r");
        for(int i=0;i<20;i++)
        {
            oldAngle[i] = newAngle[i];
            fscanf(fin,"%f", &newAngle[i]);
        }
    }
    float alpha = (t%100)/100.0;
    for(int i=0;i<20;i++)
    {
        angle[i] = newAngle[i]*alpha +  oldAngle[i]*(1-alpha);
    }
    glutPostRedisplay();
}

void keyboard(unsigned char key ,int x,int y)
{///Now3: 用keyboard來讀入動畫資料
    if(key=='1') now=1;///按1就會改變1號關節
    if(key=='2') now=2;///按2就會改變3號關節      //不會執行keyboard
    if(key=='3') now=3;
    if(key=='4') now=4;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='9') now=9;

     if(key=='w' || key=='W' || key=='s' || key=='S'){///Now4: 按's'才會寫關節的角度,一次20個
        if(fout==NULL) fout = fopen("output.txt", "w+");///第一節教的開檔

        for(int i=0;i<9;i++) {
        printf("%.3f\n",angle[i]);
        fprintf(fout, "%.3f\n", angle[i]);///TODO:把角度印到檔案裡
    }
}
        if(key=='r' || key=='R'){///Now4: 按'r'才會讀入關節的角度,一次讀20個
            if(fin==NULL)fin=fopen("output.txt", "r");///Now4: r表示讀入read
            for(int i=0;i<9;i++) {
            fscanf(fin, "%f", &angle[i]);///Now4: 用陣列從檔案fin讀進來
            printf("%.3f\n",angle[i]);
            }
            glutPostRedisplay();
        }
      if(key=='p'){
        glutTimerFunc(0,timer,0);
        if(fin==NULL)fin = fopen("output.txt", "r");
        for(int i=0;i<20;i++){
        fscanf(fin, "%f", &newAngle[i]);
        }
    }
}



void mouse(int button,int state,int x,int y)
{
    oldX=x;
}

void motion(int x,int y)
{
    angle[now] += x-oldX; ///角度稍微增減, 以前教過把大象放到冰箱去的程式碼
    oldX=x;
    glutPostRedisplay();
}

void  display()
{
    glClearColor(0.9,0.50,0.75,0.8);///粉紅色背景
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        if (!pmodel) {
        pmodel = glmReadOBJ("data/KBkody.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        glScalef(0.5,0.5,0.5);
        }
          glPushMatrix();
            glTranslatef(0,-0.1,-0.2);
            glRotatef(angle[0],0,1,0);
            glScalef(2,1.5,2);

            glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///身體
        glPopMatrix();


        if (!pmodell) {
        pmodell = glmReadOBJ("data/KB.obj");
        if (!pmodell) exit(0);
        glmUnitize(pmodell);
        glmFacetNormals(pmodell);
        glmVertexNormals(pmodell, 90.0);
        }

        glPushMatrix();
            glTranslatef(0,1.5,0.1);
            glRotatef(angle[1],1,0,0);
            glRotatef(0,0,0,0);
            glScalef(5,5,5);

            glmDraw(pmodell, GLM_SMOOTH | GLM_MATERIAL);///頭
        glPopMatrix();



        if (!pmodelll) {
        pmodelll = glmReadOBJ("data/Rhand2.obj");
        if (!pmodelll) exit(0);
        glmUnitize(pmodelll);
        glmFacetNormals(pmodelll);
        glmVertexNormals(pmodelll, 90.0);
        }

        glPushMatrix();
            glTranslatef(1.3,0.2,0);
            glRotatef(angle[3],1,0,0);
            glRotatef(0,1,0,0);
            glScalef(1,1,1);

            glmDraw(pmodelll, GLM_SMOOTH | GLM_MATERIAL);///左手臂
        glPopMatrix();

        glPushMatrix();
            glTranslatef(-1.3,0.2,0);
            glRotatef(angle[2],1,0,0);
            glRotatef(180,0,1,0);
            glScalef(1,1,1);
            glTranslatef(0,0,0);

            glmDraw(pmodelll, GLM_SMOOTH | GLM_MATERIAL);///右手臂
        glPopMatrix();


        if (!pmodellll) {
        pmodellll = glmReadOBJ("data/leg.obj");
        if (!pmodellll) exit(0);
        glmUnitize(pmodellll);
        glmFacetNormals(pmodellll);
        glmVertexNormals(pmodellll, 90.0);
        }

        glPushMatrix();
            glTranslatef(0.25,-1.2,0);
            glRotatef(angle[5],1,0,0);
            ///glRotatef(270,1,0,0);

            glScalef(0.7,0.7,0.7);
            glmDraw(pmodellll, GLM_SMOOTH | GLM_MATERIAL);///左腳
        glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.3,-1.2,0);
            glRotatef(angle[4],1,1,0);
            ///glRotatef(270,1,0,0);

            glScalef(0.7,0.7,0.7);
            glmDraw(pmodellll, GLM_SMOOTH | GLM_MATERIAL);///右腳
        glPopMatrix();


        if (!pmodelllll) {
        pmodelllll = glmReadOBJ("data/mic.obj");
        if (!pmodelllll) exit(0);
        glmUnitize(pmodelllll);
        glmFacetNormals(pmodelllll);
        glmVertexNormals(pmodelllll, 90.0);
        }

        glPushMatrix();
            glTranslatef(0,-0.5,1);
            glRotatef(angle[6],1,0,0);
            ///glRotatef(270,1,0,0);
            glScalef(1.2,1.2,1.2);
            glmDraw(pmodelllll, GLM_SMOOTH | GLM_MATERIAL);///mic
        glPopMatrix();

    glutSwapBuffers();
}


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[] = { 0.0f, 0.0f, 10.0f, 1.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 };



int main(int argc, char *argv[])
{
    myMP3.Load("KaneBrownHeaven.mp3"); ///自己剪歌
    myMP3.Play();
    ///PlaySound("BigDog.mp3", NULL, SND_ASYNC);

    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    /*glutTimerFunc(0,timer,0);*/


    glClearColor(1,1,1,1);

    glEnable(GL_DEPTH_TEST);
    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();

    return EXIT_SUCCESS;

}







沒有留言:

張貼留言