1. 紀錄動作,存檔
第一步: 開啟一空白檔案,存在桌面
第二步:打程式-Hello World
第三部:讀取、排版
2.機器人程式碼(茶壺版):
(1.)開一個glut project
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glutSolidTeapot(0.2);///身體
glPushMatrix();///右側
glTranslatef(0.2, 0,0 );
glRotatef(angle[0], 0,0,1);
glTranslatef(0.2, 0,0 );
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///右上手臂
glPushMatrix();
glTranslatef(0.2, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.2, 0,0);
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.2);///右下手臂
glPopMatrix();
glPopMatrix();///右側
glPushMatrix();///左側
glTranslatef(-0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(-0.2, 0,0 );
glColor3f(0,1,0);///綠色
glutSolidTeapot(0.2);///左上手臂
glPushMatrix();
glTranslatef(-0.2, 0,0);
glRotatef(angle[3], 0,0,1);
glTranslatef(-0.2, 0,0);
glColor3f(0,0,1);///藍色
glutSolidTeapot(0.2);///左下手臂
glPopMatrix();
glPopMatrix();///右側
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMainLoop();
}
(2.) 鍵盤滑鼠控制
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glutSolidTeapot(0.2);///身體
glPushMatrix();///右側
glTranslatef(0.2, 0,0 );
glRotatef(angle[0], 0,0,1);
glTranslatef(0.2, 0,0 );
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///右上手臂
glPushMatrix();
glTranslatef(0.2, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.2, 0,0);
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.2);///右下手臂
glPopMatrix();
glPopMatrix();///右側
glPushMatrix();///左側
glTranslatef(-0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(-0.2, 0,0 );
glColor3f(0,1,0);///綠色
glutSolidTeapot(0.2);///左上手臂
glPushMatrix();
glTranslatef(-0.2, 0,0);
glRotatef(angle[3], 0,0,1);
glTranslatef(-0.2, 0,0);
glColor3f(0,0,1);///藍色
glutSolidTeapot(0.2);///左下手臂
glPopMatrix();
glPopMatrix();///右側
glutSwapBuffers();
}
int angleID=0;///0,1,2,3
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{///TODO2:備份(old)mouse位置
oldX=x; oldY=y;///TODO2:備份(old)mouse位置
}
void motion(int x, int y)
{///TODO2:滑動,更新 angleID對應的angle[]
angle[angleID] += (x-oldX);
oldX=x;
glutPostRedisplay();///TODO2:重畫畫面
}
void keyboard(unsigned char key, int x, int y)
{///TODO2: 鍵盤,選angleID
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMouseFunc(mouse);///TODO2:備份(old)mouse位置
glutMotionFunc(motion);///TODO2:滑動,更新 angleID對應的angle[]
glutKeyboardFunc(keyboard);///TODO2: 鍵盤,選angleID
glutMainLoop();
}
(3.) 印出手臂移動位置
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glutSolidTeapot(0.2);///身體
glPushMatrix();///右側
glTranslatef(0.2, 0,0 );
glRotatef(angle[0], 0,0,1);
glTranslatef(0.2, 0,0 );
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///右上手臂
glPushMatrix();
glTranslatef(0.2, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.2, 0,0);
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.2);///右下手臂
glPopMatrix();
glPopMatrix();///右側
glPushMatrix();///左側
glTranslatef(-0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(-0.2, 0,0 );
glColor3f(0,1,0);///綠色
glutSolidTeapot(0.2);///左上手臂
glPushMatrix();
glTranslatef(-0.2, 0,0);
glRotatef(angle[3], 0,0,1);
glTranslatef(-0.2, 0,0);
glColor3f(0,0,1);///藍色
glutSolidTeapot(0.2);///左下手臂
glPopMatrix();
glPopMatrix();///右側
glutSwapBuffers();
}
int angleID=0;///0,1,2,3
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{///TODO2:備份(old)mouse位置
oldX=x; oldY=y;///TODO2:備份(old)mouse位置
}
#include <stdio.h> ///
FILE * fout=NULL;
void saveALL()
{
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++)
{
printf("%d",angle[i]);
fprintf(fout,"%d",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void motion(int x, int y)
{///TODO2:滑動,更新 angleID對應的angle[]
angle[angleID] += (x-oldX);
oldX=x;
saveALL();
glutPostRedisplay();///TODO2:重畫畫面
}
void keyboard(unsigned char key, int x, int y)
{///TODO2: 鍵盤,選angleID
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMouseFunc(mouse);///TODO2:備份(old)mouse位置
glutMotionFunc(motion);///TODO2:滑動,更新 angleID對應的angle[]
glutKeyboardFunc(keyboard);///TODO2: 鍵盤,選angleID
glutMainLoop();
}
(4.)鍵盤 'r'讀取
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glutSolidTeapot(0.2);///身體
glPushMatrix();///右側
glTranslatef(0.2, 0,0 );
glRotatef(angle[0], 0,0,1);
glTranslatef(0.2, 0,0 );
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///右上手臂
glPushMatrix();
glTranslatef(0.2, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.2, 0,0);
glColor3f(1,1,0);///黃色
glutSolidTeapot(0.2);///右下手臂
glPopMatrix();
glPopMatrix();///右側
glPushMatrix();///左側
glTranslatef(-0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(-0.2, 0,0 );
glColor3f(0,1,0);///綠色
glutSolidTeapot(0.2);///左上手臂
glPushMatrix();
glTranslatef(-0.2, 0,0);
glRotatef(angle[3], 0,0,1);
glTranslatef(-0.2, 0,0);
glColor3f(0,0,1);///藍色
glutSolidTeapot(0.2);///左下手臂
glPopMatrix();
glPopMatrix();///右側
glutSwapBuffers();
}
int angleID=0;///0,1,2,3
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{///TODO2:備份(old)mouse位置
oldX=x; oldY=y;///TODO2:備份(old)mouse位置
}
#include <stdio.h>
FILE * fout=NULL;
void saveALL()
{
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++)
{
printf("%d",angle[i]);
fprintf(fout,"%d",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void motion(int x, int y)
{///TODO2:滑動,更新 angleID對應的angle[]
angle[angleID] += (x-oldX);
oldX=x;
saveALL();
glutPostRedisplay();///TODO2:重畫畫面
}
FILE * fin=NULL;
void keyboard(unsigned char key, int x, int y)
{///TODO2: 鍵盤,選angleID
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='r')
{
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++)
{
fscanf(fin, "%d",&angle[i]);
}
glutPostRedisplay();
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMouseFunc(mouse);///TODO2:備份(old)mouse位置
glutMotionFunc(motion);///TODO2:滑動,更新 angleID對應的angle[]
glutKeyboardFunc(keyboard);///TODO2: 鍵盤,選angleID
glutMainLoop();
}















沒有留言:
張貼留言