開啟一個全新的空檔並命名為week13_fprintf.cpp

將程式碼打上
#include <stdio.h>
int main ()
{
FILE * fout = fopen("output.txt","w+"); ///w+:要寫(write)新增檔案
printf("Hello world\n");
}
執行過後會發現,在桌面上產生出了一個output.txt檔案
接著對程式碼作稍為的修改,讓原本在codeblocks執行視窗會印出來的Hello word印到output.txt裡
#include <stdio.h>
int main ()
{
FILE * fout = fopen("output.txt","w+");
fprintf(fout,"Hello world\n");
}
Week 13 - Part 2 - 裡用鍵盤來寫檔
開啟一個新的專案
將程式碼打上
#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL;
float angle=0,angle2=90,angle3=60;
void keyboard(unsigned char key,int x,int y)
{
if(key=='s'||key=='S'||key=='w'||key=='W'){
if(fout==NULL) fout=fopen("output.txt","w+");///只有第一次按下s/w建(為空值),才會open
printf("%.3f %.3f %.3f\n",angle,angle2,angle3);
fprintf(fout,"%.3f %.3f %.3f\n",angle,angle2,angle3);
}
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
執行過後,在 C:\Users\N\Desktop\CCE\2下\電腦圖學\freeglut\bin 沒有output.txt檔
在按下s鍵之後,發現在 C:\Users\N\Desktop\CCE\2下\電腦圖學\freeglut\bin 產生了output.txt
接著將程式碼做修改的動作,好讓我們按下S/W可以記錄茶壺轉動的角度
Week 13 - Part 3 - 裡用數字鍵盤來控制關節
將老師給的程式碼開啟,執行後可以用程式碼來控制關節
#include <stdio.h>
#include <GL/glut.h>
FILE * fout=NULL;
FILE * fin = NULL;
float angle[20];
int now=0;///用數字鍵來切換關節
void keyboard(unsigned char key, int x, int y){
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='5') now=5;
if(key=='6') now=6;
if(key=='7') now=7;
if(key=='8') now=8;
///啟用檔案
if(key=='s' ||key=='S' ||key=='w' ||key=='W' ){
if(fout==NULL) fout=fopen("output.txt", "w+");
for(int i=0;i<20;i++){
printf("%.3f ",angle[i]);
fprintf(fout, "%.3f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
///讀檔
if(key=='r' ||key=='R'){
if(fin==NULL) fin=fopen("output.txt", "r");
for(int i=0;i<20;i++){
fscanf(fin, "f",&angle[i]);
}
glutPostRedisplay();
}
}
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0.4,0,0);
glutSolidTeapot(0.3);///右手臂
glPushMatrix();
glTranslatef(0.4,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.4,0,0);
glutSolidTeapot(0.3);///右手肘
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0);
glRotatef(angle[3],0,0,1);
glTranslatef(-0.4,0,0);
glutSolidTeapot(0.3);///左手臂
glPushMatrix();
glTranslatef(-0.4,0,0);
glRotatef(angle[4],0,0,1);
glTranslatef(-0.4,0,0);
glutSolidTeapot(0.3);///左手肘
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y){
angle[now]=x;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
}







沒有留言:
張貼留言