#include <stdio.h>
int main()
{
FILE * font=fopen("檔名.txt","w+");
printf( "Hello world\n");
fprintf(font,"Hello workd in file\n");
}
→run
→出現 file.o、file.exe、檔名txt
→修改程式碼
#include <stdio.h>
int main()
{
FILE * font=fopen("檔名.txt","w+");
for(int i=0;i<20;i++)
{
printf( "Hello %d\n",i);
fprintf(font,"Hello %d\n",i);
}
}
→修改程式碼
#include <stdio.h>
int angle[10];
int main()
{
FILE * font=fopen("motion.txt","w+");
for(int i=0;i<10;i++)
int angle[10];
int main()
{
FILE * font=fopen("motion.txt","w+");
for(int i=0;i<10;i++)
{
printf( "%d ",angle[i]);
fprintf(font,"%d ",angle[i]);
}
printf("\n");
fprintf(font,"\n");
}
printf( "%d ",angle[i]);
fprintf(font,"%d ",angle[i]);
}
printf("\n");
fprintf(font,"\n");
}
關節動作
#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); →身體
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();
}
加上 motion()、mouse()、keyboard()函式
(1)
int angleID=0;
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y; ←備份(old)mouse位置
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX); ←滑動,更新 angleID對應的angle[]
oldX=x;
glutPostRedisplay(); ←重畫畫面
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y; ←備份(old)mouse位置
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX); ←滑動,更新 angleID對應的angle[]
oldX=x;
glutPostRedisplay(); ←重畫畫面
}
void keyboard(unsigned char key, int x, int y)
{ ←鍵盤,選angleID
void keyboard(unsigned char key, int x, int y)
{ ←鍵盤,選angleID
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
glutMouseFunc(mouse); ←備份(old)mouse位置
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
int main()
{
glutMotionFunc(motion); ←滑動,更新 angleID對應的angle[]
glutKeyboardFunc(keyboard); ←鍵盤,選angleID
glutKeyboardFunc(keyboard); ←鍵盤,選angleID
}
(2)
#include <stdio.h> ←外掛
FILE * fout=NULL;
void saveAll() ←全部存檔
void saveAll() ←全部存檔
{
if(fout==NULL) fout=fopen("motion.txt", "w+"); ←開檔
for(int i=0; i<10; i++) ←for迴圈,把陣列全用
if(fout==NULL) fout=fopen("motion.txt", "w+"); ←開檔
for(int i=0; i<10; i++) ←for迴圈,把陣列全用
{
printf( "%d ", angle[i]); ←印畫面
fprintf(fout, "%d ", angle[i]); ←寫檔案
}
printf( "\n"); ←跳行,畫面不會亂
fprintf(fout, "\n"); ←跳行,畫面不會亂
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
oldX=x;
saveAll(); ←全部存檔
glutPostRedisplay(); ←重畫畫面
}
printf( "%d ", angle[i]); ←印畫面
fprintf(fout, "%d ", angle[i]); ←寫檔案
}
printf( "\n"); ←跳行,畫面不會亂
fprintf(fout, "\n"); ←跳行,畫面不會亂
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
oldX=x;
saveAll(); ←全部存檔
glutPostRedisplay(); ←重畫畫面
}
完整
**#include最好放在最前面,放中間不好
#include <GL/glut.h>
#include <stdio.h>
**全域變數(global variable)最好放在最前面,放中間不好
FILE * fout=NULL;
FILE * fin=NULL; ←宣告檔案指標
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angleID=0;
int oldX, oldY;
**C/C++因為函式使用前要先宣告or定義,有時候會順序不對
void display();
void motion(int x, int y);
void mouse(int button, int state, int x, int y);
void keyboard(unsigned char key, int x, int y);
void saveAll();
**可以在程式的最前面, 把全部的函式的外形, 先宣告一次
**不要寫完整的定義
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();
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void saveAll() **裡面宣告不會讓外面記起來
{
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0; i<10; i++)
#include <GL/glut.h>
#include <stdio.h>
**全域變數(global variable)最好放在最前面,放中間不好
FILE * fout=NULL;
FILE * fin=NULL; ←宣告檔案指標
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angleID=0;
int oldX, oldY;
**C/C++因為函式使用前要先宣告or定義,有時候會順序不對
void display();
void motion(int x, int y);
void mouse(int button, int state, int x, int y);
void keyboard(unsigned char key, int x, int y);
void saveAll();
**可以在程式的最前面, 把全部的函式的外形, 先宣告一次
**不要寫完整的定義
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();
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
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)
{
angle[angleID] += (x-oldX);
oldX=x;
saveAll();
glutPostRedisplay();
}
void readAll()
{
if(fin==NULL) fin=fopen("motion.txt", "r");
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)
{
angle[angleID] += (x-oldX);
oldX=x;
saveAll();
glutPostRedisplay();
}
void readAll()
{
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0; i<10; i++)
{
///scanf( "%d", &a[i]);
fscanf(fin, "%d", &angle[i]);
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='r'){
readAll();
glutPostRedisplay();
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
///scanf( "%d", &a[i]);
fscanf(fin, "%d", &angle[i]);
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='r'){
readAll();
glutPostRedisplay();
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week14 file");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}

沒有留言:
張貼留言