2015年6月18日 星期四

02160083廖峻賢

資傳二乙 廖峻賢
我的想法是讓一個積木人活過來 並且雀躍的跳舞
影片呈現的看了就知道

https://youtu.be/4op9zmI3Ysc


#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
#include <math.h>
#include <mmsystem.h>

float a=0; float x=0;

FILE *fout=NULL; ///­n¦sÀɥΪº°Õ!
FILE * fin  =NULL;

GLMmodel *pmodel1=NULL;
GLMmodel *pmodel2=NULL;
GLMmodel *pmodel3=NULL;
GLMmodel *pmodel4=NULL;
GLMmodel *pmodel5=NULL;

float angle[10]={0};
    float angleOld[10]={0};
    float angleNew[10]={0};
float oldX=0, oldY=0;
int n;


void readNew()
{
    if(fin==NULL) fin=fopen("a.txt", "r");
    for(int i=0;i<10;i++)
    {   angleOld[i]=angleNew[i];
        fscanf(fin, "%f",&angleNew[i]);
        printf("%.1f ",angleNew[i]);
    }
    printf("\n");
}

void timerPlay(int t)
{
    glutTimerFunc(20, timerPlay, t+1);

    if(t%2==0) readNew();
    a=(t%2)/2.0;
    for(int i=0;i<10;i++)
    {
        angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
    }
    glutPostRedisplay();
}


void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')n=1;
    if(key=='2')n=2;
    if(key=='3')n=3;
    if(key=='4')n=4;
    if(key=='a')x-=0.1;
    if(key=='d')x+=0.1;
    if(key=='p')glutTimerFunc(10, timerPlay,0);
    if(key=='s')
    {
        if(fout==NULL) fout=fopen("a.txt", "w+");
    for(int i=0;i<10;i++)
    {
            printf(      "%.1f ",angle[i]);
            fprintf(fout, "%.1f",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}



    glutPostRedisplay();


}
void drawBody(){
    glPushMatrix();
    glmDraw(pmodel1,GLM_SMOOTH|GLM_MATERIAL);
    glPopMatrix();
}

void display()
{
    { ///Lighting
    //glColor3ub(166,132,100);
    glClearColor(0.3,0.5,0,0);
    GLfloat pos[]={1, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glRotatef(x,0,1,0);
        glScalef(0.5,0.5,0.5);
        //glTranslatef(cos(x/20)*0.5,y,0);
        drawBody();
//////////////B
        glPushMatrix();
            glTranslatef(-0.3,0,-0.02);
            glRotatef(angle[1],90,100,-180);
            glTranslatef(0,-0.1,-0.3);
            glScalef(0.4,0.4,0.4);


///////////////A
            glmDraw(pmodel2,GLM_SMOOTH|GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glTranslatef(0.4,0.3,-0.2);
            glRotatef(-angle[2],-90,0,-90);
            glTranslatef(-0.2,-0.4,0.05);
            glScalef(0.5,0.5,0.5);
//////////////AA
            glmDraw(pmodel3,GLM_SMOOTH|GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glTranslatef(0.2,-0.65,0.5);
            glRotatef(angle[3],-0.5,0,0.5);
            glTranslatef(0,-0.5,-0.5);
            glScalef(0.3,0.3,0.3);
/////////////////BB
            glmDraw(pmodel4,GLM_SMOOTH|GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.035,-0.85,0);
            glRotatef(-angle[4],0.5,-0.2,0.8);
            glTranslatef(-0.1,-0.3,-0.2);
            glScalef(0.3,0.3,0.3);

            glmDraw(pmodel5,GLM_SMOOTH|GLM_MATERIAL);
         glPopMatrix();
    glPopMatrix();


    glFlush();
    x++;
}
void motion(int x, int y)
{
    angle[n] -= y - oldY;
    if(angle[n]>60)angle[n]=60;
    if(angle[n]<0)angle[n]=0;
    oldY=y; oldX=x;
    if(fout==NULL)fout=fopen("a.txt","w+");
    for(int i=0;i<8;i++)
    {
        fprintf(fout,"%.1f\t",angle[i]);
    }
    fprintf(fout,"\n");
}

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





void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
  PlaySound("QQQQ.wav",NULL,SND_ASYNC);
    glutInit(&argc, argv);

    pmodel1=glmReadOBJ("body.obj");
    glmUnitize(pmodel1);
    glmFacetNormals(pmodel1);
    glmVertexNormals(pmodel1,90.0);

    pmodel2=glmReadOBJ("A.obj");
    glmUnitize(pmodel2);
    glmFacetNormals(pmodel2);
    glmVertexNormals(pmodel2,90.0);

    pmodel3=glmReadOBJ("B.obj");
    glmUnitize(pmodel3);
    glmFacetNormals(pmodel3);
    glmVertexNormals(pmodel3,90.0);

    pmodel4=glmReadOBJ("AA.obj");
    glmUnitize(pmodel4);
    glmFacetNormals(pmodel4);
    glmVertexNormals(pmodel4,90.0);

    pmodel5=glmReadOBJ("BB.obj");
    glmUnitize(pmodel5);
    glmFacetNormals(pmodel5);
    glmVertexNormals(pmodel5,90.0);

    glutCreateWindow("02160083");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

沒有留言:

張貼留言