2016 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2016年5月30日 星期一
week15和鋼鐵人一起拯救世界a柏廷
TODO:用EXCEL作內插
TODO :在OPENGL/GLUT作動作內插
TODO :10個ROT 去內插播放(TIMER)計時器
程式碼: 可以存入每個位置的數值 利用TIMER函式將兩個數值內插多個數值,可以讓動作變得更平滑流暢
#include <stdio.h>
#include <GL/glut.h>
float pos=0, oldX=0.7, newX=0.1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(pos, 0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
}
void timer (int t)
{
float alpha=t/100.0;
pos = newX*alpha+oldX*(1-alpha);
printf("%d %f %f \n",t,alpha,pos);
glutTimerFunc(100,timer,t+1);
glutPostRedisplay();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("123");
glutDisplayFunc(display);
glutTimerFunc(100,timer,0);
glutMainLoop();
}
Week15_許皓翔 - 河蟹牌Part 12 愛睏模式
今天要用 Excel 做內插噢 , wow ..
T-R-T小考
右手座標系 Z軸朝外面
手握拳的方向為正 反方是負
手握拳的方向為正 反方是負
EXCEL內插
公式1
唉呦公式2
new為最下面 old為最上面
插入表格(線性)
結果
今日程式碼 :
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
待補.
W15
課堂作業一
使用Excel輸入內差公式
練習如何使用內差公式作內差運算


課堂作業二
在OpenGL/GLUT做運動內插

程式如下
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}

課堂作業三
使用十個以上內插座出機器人動作
使用Excel輸入內差公式
練習如何使用內差公式作內差運算


課堂作業二
在OpenGL/GLUT做運動內插

程式如下
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}

課堂作業三
使用十個以上內插座出機器人動作
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
Week15 鄭棕寶
課堂作業1:
步驟1:依照下圖用Excel做出內插法



課堂作業2:
步驟1:用Code::Blocks做出內插法
步驟2:將開設定的咒語等都設定完畢
步驟3:將程式碼輸入
(程式碼如下)
#include <stdio.h>
#include <GL/glut.h>
float pos=0, oldX=0.7, newX=0.1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(pos,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
float alpha=t/100.0;
pos = newX*alpha+oldX*(1-alpha);
printf("%d %f %f\n",t,alpha,pos);
glutTimerFunc(100,timer,t+1);
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(100,timer,0);
glutMainLoop();
}

課堂作業3:
在上禮拜的機器人身上用上內插法
步驟1:叫出機器人的程式碼與今天所學的內插法
(以下程式是老師給的裡面的內容需要研究)
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
步驟1:依照下圖用Excel做出內插法



課堂作業2:
步驟1:用Code::Blocks做出內插法
步驟2:將開設定的咒語等都設定完畢
步驟3:將程式碼輸入
(程式碼如下)
#include <stdio.h>
#include <GL/glut.h>
float pos=0, oldX=0.7, newX=0.1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(pos,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
float alpha=t/100.0;
pos = newX*alpha+oldX*(1-alpha);
printf("%d %f %f\n",t,alpha,pos);
glutTimerFunc(100,timer,t+1);
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(100,timer,0);
glutMainLoop();
}

在上禮拜的機器人身上用上內插法
步驟1:叫出機器人的程式碼與今天所學的內插法
(以下程式是老師給的裡面的內容需要研究)
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
