1.
1-1:
程式碼:
#include <stdio.h>
int main()
{
FILE*fout=fopen("file.txt","w+");///開啟一個file,名字為file.txt
w+ = write+順便新增
printf("Hello world\n");
fprintf(fout,"Hello world\n");///file裡的字
fclose(fout);
}
1-2:
#include <stdio.h>
int main()
{
FILE*fout=fopen("file2.txt","w+");
fprintf(fout,"angle1 %d\n",999);
fclose(fout);
char line[200];///字串
int a;///angle
FILE*fin=fopen("file2.txt","r");
fscanf(fin,"%s %d",line,&a);
printf("讀到了字串:%s 及整數%d\n",line,a);
fclose(fin);
}
2.
2-1:
程式碼:
#include <GL/glut.h>
#include <stdio.h>///為了能用printf、fprintf、fopen......
float angle[20],oldX=0;
int angleID=0; ///0:第0個關節,1:第1個關節
FILE*fout=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf("%.2f",angle[i]);
fprintf(fout,"%.2f",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
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;
}
void mouse(int button,int state,int x,int y)
{
oldX = x ;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(+0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc , argv );
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
黃底是新增的程式碼;
紫底是上周程式碼
2-2:
程式碼:
#include <GL/glut.h>
#include <stdio.h>///為了能用printf、fprintf、fopen......
float angle[20],oldX=0;
int angleID=0; ///0:第0個關節,1:第1個關節
FILE*fout=NULL,*fin=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf("%.2f",angle[i]);
fprintf(fout,"%.2f",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void myRead(){
if(fout!=NULL){
fclose(fout);
fout=NULL;
}///如果fout正在跑其他東西,把其他東西關掉
if(fin==NULL)fin=fopen("file.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%f",&angle[i]);
}
glutPostRedisplay();///重畫畫面
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='r'){myRead();}///按r重製
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
oldX = x ;
}
void motion(int x,int y)
{
angle[angleID] += (x-oldX);
myWrite();
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(+0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(+0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc , argv );
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
3.動畫
3-1:
程式碼:
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){///t的單位是毫秒,所以1000=1秒
printf("鬧鐘%d 我起床了\n",t);
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000,timer,t+1);///(等多久,timer,隔多久下一個)
printf("設好鬧鐘,再回去睡");
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14 timer");///等多久開始
glutTimerFunc(3000,timer,0);
glutDisplayFunc(display);
glutMainLoop();
}
3-2:加聲音
程式碼:
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void timer(int t){///t的單位是毫秒,所以1000=1秒
printf("鬧鐘%d 我起床了\n",t);
PlaySound("do.wav",NULL,SND_ASYNC);///播聲音
///音檔要放在freeglut的bin
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000,timer,t+1);///第一個=等多久,第二個=timer,隔多久下一個
printf("設好鬧鐘,再回去睡");
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14 timer");///等多久開始
glutTimerFunc(3000,timer,0);
glutDisplayFunc(display);
glutMainLoop();
}







沒有留言:
張貼留言