檔案網址:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
-
壓縮在同一個資料夾(WINDOWS要包含DATA跟GLUT32.DLL和SOURCE)
Download
freeglut 3.0.0 for MinGW壓縮檔案,在LIB裡面複製LIBFREEGLUT,貼上改成LIBGLUT32。
開啟CODEBLOCK。FILE(左上角)->NEW->PROJECT->GLUT PROJECT->(命名檔案)->下一步->路徑改成FREEGLUT資料夾的路徑。.
(1)今天的主題:
- 主題:檔案。
- 主題:關節,擺動作。
- 複習:TRT。
- 作業:機器人擺動作。
(2)///切記,READ一次只能讀到一行(目前狀況),write(目前是按了之後會把之後讀到的資料給送進文件中。)(但要在關閉CMD之後才會真實的運作,所以狂按W會導致上次的還沒成功存檔之前,又上傳一次,這會導致。上一次上傳的資料被消除,所以W不要一直按。)
#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
FILE * fin = NULL;///NOW_FILE_READ (1)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1, 1, 1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///右邊
glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
oldX = x;///NOW2
}
void motion(int x, int y)///NOW2
{///NOW2
angle[angleID] += x-oldX;///NOW3
oldX = x;///NOW2
if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
for(int i=0;i<20;i++){///NOW3
printf(" %.2f ", angle[i]);///NOW3
fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
}
printf("\n");///NOW3
fprintf(fout, "\n");///NOW_FILE
display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
if(key=='1') angleID=1;///NOW3
if(key=='2') angleID=2;///NOW3
if(key=='3') angleID=3;///NOW3
if(key=='4') angleID=4;///NOW3
if(key=='w'){///NOW_FILE
fout = fopen("motion.txt", "w+");///NOW_FILE (2)
}
if(key=='r'){
if(fin==NULL) fin = fopen("motion.txt", "r");///NOW_FILE_READ (2)
///scanf("%d", &n);
for(int i=0;i<20; i++){///NOW_FILE_READ
fscanf(fin, "%f", &angle[i]);///NOW_FILE_READ
printf(" %.2f ", angle[i]);///順便印出來看數字對不對
}
}
glutPostRedisplay();///NOW_FILE_READ 和 display()很像
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///可以開大一點的window
glutCreateWindow("Week 13 angle motion file");
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glutMainLoop();
}
//
FILE * fout=NULL;///宣告檔案指標。
FILE * fout2=NULL;///宣告檔案指標。寫進另一個檔案。
FILE * fin2=NULL;///宣告檔案指標。讀另一個檔案。
fout = fopen("filename.txt","w+");///開檔案,寫進去///大寫沒意義。RWBA。
fprintf(fout,"HelloWorld\n");///印進去。
同理 scanf的改寫就是 fscanf(fout,"%c");**注意,這類方式fout後段不是w+,是r+或r
//
///
EX衍伸知識區
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
by:"S's Journal"
///
沒有留言:
張貼留言