2017年6月5日 星期一

Week 16 鐘珈翎的上課筆記



(1)內插公式
(2)存檔、讀檔
(3)利用Timer自動內插
(4)鍵盤+滑鼠




用Excel來做內插公式



利用Timer做自動內插


#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel_head = NULL;
GLMmodel* pmodel_hand1 = NULL;
GLMmodel* pmodel_hand2 = NULL;
GLMmodel* pmodel_face = NULL;

float angle[9]={},dir=1;
float alpha=0.0;
float oldAngle[9]={0,0,0,0,0,0,0,0,0};
float newAngle[9]={40,40,-40,-40,0,0,0,0,0};
void interpolation()
{
    for(int i=0;i<9;i++)
    {
        angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
    }
    alpha+=0.1;
}

//float angle[20]={0};
int now=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

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

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

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


//////////////////////////////////////////////////////////////////////
    glPushMatrix();
       // glRotatef(angle, 0,1,0);


         glPushMatrix();
            glTranslatef(0,0,0);
            glScalef(1,1,1);
            glmDraw(pmodel_head, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0,0.8,-0.08);
             glRotatef(0, 0,1,0);
            glScalef(1.8,1.8,1.8);
            glmDraw(pmodel_face, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();


//////////////////////
        glPushMatrix();
            glTranslatef(0.3,0.4,0);  //掛到身體
            glRotatef(-angle[0], 0,0,1);  //開始旋轉
            glTranslatef(0.1,-0.6,0);   //找中心點
            glScalef(1,1,1);
            glmDraw(pmodel_hand2, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();



        glPushMatrix();
            glTranslatef(-0.3, 0.4, 0);  //掛到身體
            glRotatef(angle[1], 0,0,1);  //開始旋轉
            glTranslatef(-0.1,-0.6,0);   //找中心點
            glScalef(1,1,1);
            glmDraw(pmodel_hand1, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();




    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[] = { 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 };
/*
#include <stdio.h>
void timer(int t)///計時器叫的時候, 要做什麼呢?
{
    glutTimerFunc(10, timer, t+1);///先撥下一個鬧鐘
    angle++;///做了要做的事

    glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦

}///再度沈睡
*/
#include <mmsystem.h>
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0')now=0;
    if(key=='1')now=1;
    if(key=='2')now=2;
}

int oldX=0,oldY=0;
void mouse (int button,int state,int x, int y)
{
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
void motion(int x, int y)
{
    angle[now]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}

void timer (int t)
{
    glutTimerFunc(100,timer,t+1);
    interpolation();
    glutPostRedisplay();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutCreateWindow("04160063");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutDisplayFunc(display);
 //   glutIdleFunc(display);
  //  glutTimerFunc(0, timer, 0);///(1) 第一次呼叫timer計時器
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glClearColor(1,1,1,1);
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);

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



ToDo:讀檔、寫檔
#include <stdio.h>
FILE*fout==NULLfout=fopen("file.txt","w+");
if(key=='s'||key=='S')
{
    fprintf(fout,"%.3f %.3f %.3f %.3f %.3f\n",angle[0]);
}




沒有留言:

張貼留言