1.FILE檔案 fopen()開檔 fprintf()寫檔
#include <stdio.h>
int main(){
///檔案指標 fout是開啟檔...write+
FILE * fout=fopen("檔名.txt","w+");
printf("Hello world\n");
fprintf(fout,"Hello world in file\n");
}

2.配合for迴圈印很多數字
#include <stdio.h>
int main(){
///檔案指標 fout是開啟檔...write+
FILE * fout=fopen("檔名2.txt","w+");
for(int i=0;i<20;i++){
printf("Hello %d\n",i);
fprintf(fout,"Hello %d\n",i);
}
}

3.陣列的值,用for迴圈來印出來
#include <stdio.h>
int angle[10];
int main(){
FILE * 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");
///避免數字黏在一起
}

4.利用TRT及angle陣列,做出上下手臂,共4個關節
#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();
}
5.利用keyboard選angleID,利用mouse及motion來讓angleID跟著動,並更新畫面
#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();
}
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();
}

6.在motion時,順便saveAll(),裡面會用第1節課教的迴圈及陣列fprintf()寫檔案
keyboard()裡的key=='r' 一樣fopen()開檔, fscanf()讀檔, 再glutPostRedisplay()重畫。
(記得前面要宣告FILE檔案指標哦 )
#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> ///TODO3: 外掛
FILE * fout=NULL;///TODO3
void saveAll()///TODO3: (自己寫的函式)全部存檔
{
if(fout==NULL) fout=fopen("motion.txt", "w+");///TODO3:開檔
for(int i=0; i<10; i++){///TODO3: for迴圈,把陣列全用
printf( "%d ", angle[i]);///TODO3: 印畫面
fprintf(fout, "%d ", angle[i]);///TODO3: 寫檔案
}
printf( "\n");///TODO3: 跳行, 畫面不會亂
fprintf(fout, "\n");///TODO3: 跳行, 畫面不會亂
}
void motion(int x, int y)
{///TODO2:滑動,更新 angleID對應的angle[]
angle[angleID] += (x-oldX);
oldX=x;
saveAll();///TODO3: (自己寫的函式)全部存檔
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();
}

7.整理程式碼,區域變數vs.全域變數, 函式宣告vs.函式定義
///#include最好放在最前面,放中間不好!!!!
#include <GL/glut.h>
#include <stdio.h> ///TODO3: 外掛
///全域變數(global variable)最好放在最前面,放中間不好!!!!
FILE * fout=NULL;///TODO3
FILE * fin=NULL;///TODO4: 宣告檔案指標
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angleID=0;///0,1,2,3
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)
{///TODO2:備份(old)mouse位置
oldX=x; oldY=y;///TODO2:備份(old)mouse位置
}
///用函式來做事, 漂亮!!!
void saveAll()///TODO3: (自己寫的函式)全部存檔
{ ///小心!裡面宣告,不會讓外面記起來
if(fout==NULL) fout=fopen("motion.txt", "w+");///TODO3:開檔
for(int i=0; i<10; i++){///TODO3: for迴圈,把陣列全用
printf( "%d ", angle[i]);///TODO3: 印畫面
fprintf(fout, "%d ", angle[i]);///TODO3: 寫檔案
}
printf( "\n");///TODO3: 跳行, 畫面不會亂
fprintf(fout, "\n");///TODO3: 跳行, 畫面不會亂
}
void motion(int x, int y)
{///TODO2:滑動,更新 angleID對應的angle[]
angle[angleID] += (x-oldX);
oldX=x;
saveAll();///TODO3: (自己寫的函式)全部存檔
glutPostRedisplay();///TODO2:重畫畫面
}
void readAll()
{
if(fin==NULL) fin=fopen("motion.txt", "r");///TODO4 開檔
for(int i=0; i<10; i++){
///scanf( "%d", &a[i]);
fscanf(fin, "%d", &angle[i]);///TODO4 讀檔
}
}
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'){
readAll();///這樣才漂亮!!!!
glutPostRedisplay();///TODO4 重畫
}
}
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();
}

notepad++的compare外掛好好用


沒有留言:
張貼留言