1.檔案
1)codeblock>File>New>Project>Console application
2)
#include <stdio.h>
FILE * fout=NULL;
int main()
{
fout = fopen("filename.txt","w+");
for(int i=0;i<20;i++)
{
fprintf(fout,"Hello World\n");
}
}
結果會在城市資料夾新增一個txt文字檔
3)
#include <stdio.h>
FILE * fout2=NULL;
FILE * fin3=NULL;
float angle[20];
int main()
{
fout2 = fopen("motion.txt","w+");
for(int i=0;i<20;i++)
{
fprintf(fout2,"%.2f\n",angle[i]);
}
}
2.關節擺動作
#include <GL/glut.h>
float angle[20];///NOW2
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(0, 1, 1);///red
glutSolidTeapot( 0.2 );///right arm
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)
{
oldX = x;
}
void motion(int x, int y)
{
angle[1] += x-oldX;
oldX = x;
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
3.擺動作
#include <GL/glut.h>
float angle[20];
int angleID=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);(3)掛上去
glRotatef(angle[1], 0,0,1); ///(2)旋轉
glTranslatef(0.15, 0, 0);/// (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(0.2, 0, 0); (3)掛上去
glRotatef(angle[2], 0,0,1); ///(2)旋轉
glTranslatef(0.15, 0, 0); (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.2, 0, 0); (3)掛上去
glRotatef(angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.15, 0, 0); (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(-0.2, 0, 0);/// (3)掛上去
glRotatef(angle[4], 0,0,1); ///(2)旋轉
glTranslatef(-0.15, 0, 0);/// (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)
{
oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
}
printf("\n");
display();
}
void keyboard(unsigned char key, int x, int y)
{///NOW3
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
}
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);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
4.讀錄檔
#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];
int angleID=1;
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);
glutSolidTeapot( 0.3 );///body
glPushMatrix();///右邊
glTranslatef(0.2, 0, 0);/// (3)掛上去
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.15, 0, 0);/// (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(0.2, 0, 0);/// (3)掛上去
glRotatef(angle[2], 0,0,1); ///(2)旋轉
glTranslatef(0.15, 0, 0);///(1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.2, 0, 0);/// (3)掛上去
glRotatef(angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.15, 0, 0);/// (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right arm
glPushMatrix();
glTranslatef(-0.2, 0, 0);///(3)掛上去
glRotatef(angle[4], 0,0,1);///(2)旋轉
glTranslatef(-0.15, 0, 0);/// (1)旋轉中心
glColor3f(1, 0, 0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)
{
oldX = x;
}
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
}
printf("\n");
fprintf(fout, "\n");///NOW_FILE
display();///NOW2
}
void keyboard(unsigned char key, int x, int y)
{///NOW3
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
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);
glutCreateWindow("Week 13 angle motion file");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
先跑一次專案
然後再次跑的是喉按'r'可以呈現剛剛的動作
沒有留言:
張貼留言