Week14筆記
step01-1
寫檔(為了記錄動畫的動作)
0.new empty file,存檔成week14-1_fprintf裡的week14.cpp
1.fopen() 開檔案
2.printf() ==> fprintf()
3.fclose() 關檔案
按build後再按build and run
#include <stdio.h>
int main()
{///檔案指標 fout 開啟檔案(檔名,write模式)
FILE * fout = fopen("file.txt" , "w+");
printf( "Hello World\n");
fprintf(fout,"Hello World\n");
fclose(fout);///關閉檔案
}
step01-2
讀檔file input
0.new empty file,存檔成week14-3_fprintf_fscanf裡的week14-2.cpp
1.把剛剛week14-1.cpp拿來用
2.另外一組file*fin
3.scanf ()==> fscanf()
4.fclose()
///week14-2
#include <stdio.h>
int main()
{///檔案指標 fout 開啟檔案(檔名,write模式)
FILE * fout = fopen("file.txt" , "w+");
fprintf(fout,"3.1415926\n");
fclose(fout);///關閉檔案
float angle=0;
FILE * fin = fopen("file.txt","r");
fscanf(fin,"%f",&angle); ///沒加&會當掉
printf("讀到了角度:%f",angle);
fclose(fin);
}
step01-3
把上週的程式 week13_rect_many_TRT 拿來改成 week14
0.File New Project,GLUT專案 week14_angles_fprintf
///week14_angles_fprintf 改自 week13_rect_many_TRT
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;///0號關節,1號關節...
FILE * fout = NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf("%.1f",angle[i]);///小黑印出來
fprintf(fout , "%.1f", 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;
}
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();///請GLUT重劃畫面
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5);///身體
glPushMatrix();
glTranslatef(0.3,0.4,0);///把手臂掛在身上
glRotatef(angle[0],0,0,1);///對z軸旋轉
glTranslatef(-0.3,-0.4,0);///挑整手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);///把手肘掛回去
glRotatef(angle[1],0,0,1);///旋轉
glTranslatef(-0.7,-0.4,0);
glColor3f(0,1,0);///綠色的
glRectf(0.7,0.5,1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);///把手臂掛在身上
glRotatef(angle[2],0,0,1);///對z軸旋轉
glTranslatef(0.3,-0.4,0);///挑整手臂的旋轉中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0);///把手肘掛回去
glRotatef(angle[3],0,0,1);///旋轉
glTranslatef(0.7,-0.4,0);
glColor3f(0,1,0);///綠色的
glRectf(-0.7,0.5,-1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[])//main()主函式 進階版
{
glutInit(&argc,argv);//把參數送給glutInit初始化
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
///glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");//開GLUT視窗
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);//顯示用的函式
glutMainLoop();
}
step02-1
0.加新專案 week14_angles_fprintf_fscanf
1.複製前一個版本的程式
2.要寫void myRead()
3.鍵盤裡,按下"r"呼叫myRead()
///week14_angles_fprintf_fscanf
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;///0號關節,1號關節...
FILE * fout = NULL , *fin=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf("%.1f ",angle[i]);///小黑印出來
fprintf(fout , "%.1f ", angle[i]);///檔案印出來
}
printf("\n");///每呼叫一次小黑跳行
fprintf(fout,"\n");///每呼叫一次檔案也跳行
}
void myRead(){
if(fout!=NULL) {fclose(fout); fout=NULL;}
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();
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();///請GLUT重劃畫面
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5);///身體
glPushMatrix();
glTranslatef(0.3,0.4,0);///把手臂掛在身上
glRotatef(angle[0],0,0,1);///對z軸旋轉
glTranslatef(-0.3,-0.4,0);///挑整手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0);///把手肘掛回去
glRotatef(angle[1],0,0,1);///旋轉
glTranslatef(-0.7,-0.4,0);
glColor3f(0,1,0);///綠色的
glRectf(0.7,0.5,1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);///把手臂掛在身上
glRotatef(angle[2],0,0,1);///對z軸旋轉
glTranslatef(0.3,-0.4,0);///挑整手臂的旋轉中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0);///把手肘掛回去
glRotatef(angle[3],0,0,1);///旋轉
glTranslatef(0.7,-0.4,0);
glColor3f(0,1,0);///綠色的
glRectf(-0.7,0.5,-1.0,0.3);///下手肘
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char *argv[])//main()主函式 進階版
{
glutInit(&argc,argv);//把參數送給glutInit初始化
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
///glutInitWindowSize(600,600);
glutCreateWindow("week13_rect_TRT");//開GLUT視窗
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);//顯示用的函式
glutMainLoop();
}
step02-2
0.file.txt放在奇怪的目錄
1.GLUT 專案需要freeglut.dll 所以working dir被設到 freeglut/bin裡
2.在cbp project檔案裡 有working dir的設定 工作執行的目錄
3.使用Notepad++把.cbp的 working_dir 路徑改成.後存檔
4.再把桌面freeglut/bin裡的freeflut.dll移動到專案資料夾裡
step03-1
glutTimerFunc()計時器
0.viod timer(int t)寫timer函式
1.glutTimerFunc(等多久,timer,t參數):
2.開新專案 week14_timer
3.其他GLUT 10行程式
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
printf("起床,現在時間: %d\n",t);
}
void display(){
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 timer");
glutTimerFunc(1000,timer,1);
glutTimerFunc(2000,timer,2);
glutTimerFunc(3000,timer,3);
glutTimerFunc(4000,timer,4);
glutTimerFunc(5000,timer,5);
glutDisplayFunc(display);
glutMainLoop();
}
step03-2
五秒後開始計時
播放聲音PlaySound(),先下載 do.wav
1.繼續改 week14_timer_one_by_one
2.#include <mmsystem.h>
3.PlaySound("do.wav",NULL,SND_ASYNC);
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
printf("起床,現在時間: %d\n",t);
glutTimerFunc(1000,timer,t+1);
}
void display(){
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 timer");
glutTimerFunc(5000,timer,0);///設定五秒後第0個timer
glutDisplayFunc(display);
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void timer(int t){
printf("起床,現在時間: %d\n",t);
PlaySound("do.wav",NULL,SND_ASYNC);
glutTimerFunc(2000,timer,t+1);
}
void display(){
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 timer");
glutTimerFunc(5000,timer,0);///設定五秒後第0個timer
glutDisplayFunc(display);
glutMainLoop();
}
會有聲音








沒有留言:
張貼留言