(1)移動、旋轉、縮放
(2)T-R-T及關節結構
(3)存檔、讀檔、撥放
(4)內插 動作Interpolate
(5)期末作品
今日複習
有好幾個關節
按P可重播上次動作
今日教學
S=存動作
P=動作
只要存動作就可以做一連串的動作
程式碼:
#include <GL/glut.h>
#include <stdio.h>
//#include "glm.h";
#include <math.h>;
FILE * fout=NULL;
FILE * fin=NULL;
//float angle[10]={0}, oldX=0, oldY=0;
float oldX=0,oldY=0,x=0,y=0;
float angle[8] = {0},angleOld[8],angleNew[8];
//int now;
/*GLMmodel*pmodel1=NULL;
GLMmodel*pmodel2=NULL;
GLMmodel*pmodel3=NULL;
GLMmodel*pmodel4=NULL;
GLMmodel*pmodel5=NULL;
GLMmodel*pmodel6=NULL;
GLMmodel*pmodel7=NULL;///MAYA需要
GLMmodel*pmodel8=NULL;*/
int angleID = 1;
void readNew()
{
if(fin==NULL) fin=fopen("a.txt","r");
for(int i=0; i<8; i++)
{
angleOld[i]=angleNew[i];///讀檔案,讀進一筆新的資料
fscanf(fin,"%f",&angleNew[i]);
printf("%f",angleNew[i]);
}
printf("\n");
}
void timerPlay(int t)
{
glutTimerFunc(20,timerPlay,t+1);///計時器,用來做內插的
float a=(t%50)/50.0;
if(t%50==0)readNew();
for(int i=0; i<8; i++)
{
angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];///內插公式
}
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='p') glutTimerFunc(20,timerPlay,0);
if(key=='s')
{
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0; i<8; i++)
{
fprintf(fout,"%.1f\t",angle[i]);
printf("%.1f\t",angle[i]);
}
}
if(key=='1')angleID=1;
if(key=='2')angleID=2;
if(key=='3')angleID=3;
if(key=='4')angleID=4;
if(key=='a')x-=0.1;
if(key=='d')x+=0.1;
if(key=='s')
{
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0; i<8; i++)
{
fprintf(fout,"%.1f\t",angle[i]);
printf("%.1f\t",angle[i]);
}
fprintf(fout,"\n");
printf("\n");
}
glutPostRedisplay();
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
{
///Lighting
glColor3ub(166,132,100);
GLfloat pos[]= {0, 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();
glTranslatef(0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(angle[1], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm();///這是個Arm ver 1 Right Upper Arm
glPushMatrix();
glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(angle[2], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm(); ///Right Lower Arm
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(-angle[3], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm();///這是個Arm ver 1 Left Upper Arm
glPushMatrix();
glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(-angle[4], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm(); ///Left Lower Arm
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{
angle[angleID]-= y - oldY;
oldY=y;
oldX=x;
/*if(fout==NULL) fout=fopen("a.txt","w+");
for(int i=0; i<10; i++)
{
printf("%f",angle[i]);
fprintf(fout,"%f ",angle[i]);
}
printf("\n");
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)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言