
打開東西的程式碼
#include <stdio.h>
FILE*fout=NULL,*fin=NULL;
int main(int argc , char**argv)
{
if(fout==NULL)
fout=fopen("output.txt","w+");
fprintf(fout,"Hello World");
}

會產生出這個檔案
裡面會有HELLO WORLD
接下來寫完整程式碼
將整個架構架起來

完整程式碼
#include <GL/glut.h>
#include <stdio.h>
FILE*fout=NULL,*fin=NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BIFFER_BIT);
glutSolidTeapot(0.3);
glutSwapsBuffers();
}
void motion (int x, int y)
{
if(fout==NULL) fout=fopen("output.txt","w+");
fprintf(fout,"%d,$d\n",x,y);;
}
int main(int argc , char**argv)
{
glutInit (&argc,argv);
glutInitDisplayMode(GL_DOUBLE | GL_DEPTH);
glutCreatWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainloop();
}
接下來要寫咒語
opengl
freeglut
glu32
gli32
winmm
還要加上freeglut裡面的include和檔案


將freeglut裡面的lib的freeglut.dll
拉進桌面的03160392資料夾裡面
接下來讓茶壺跟者滑鼠遊邊走過的路徑走
#include <GL/glut.h>
#include <stdio.h>
float rot[10]={0};
int rotNow=0, oldX=0, oldY=0;;
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();
}
FILE *fout=0, *fin=0;
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]);
}
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
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");
}
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("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
老師給的程式碼
裡面包含讓機器人動
沒有留言:
張貼留言