NO.01
///先開一個沒有專案的共白檔案
#include <stdio.h>///使用標準standard輸入輸出IO
int main()
{
FILE * fout=fopen("output.txt","w+");///01 開啟當案
///FILE * 檔案指標 fout
/// 檔案file
/// open開啟
/// "要開啟的檔名","w+"表示要寫/+檔案
printf("Hello\n");
fprintf(fout,"Hello\n");
}
此時會產生一 .txt 檔,內有輸入的值
NO.02
///先開一個沒有專案的空白檔案
#include <stdio.h>///使用標準standard輸入輸出IO
int main()
{
FILE * fout=fopen("output.txt","w+");///01 開啟當案
///FILE * 檔案指標 fout
/// 檔案file
/// open開啟
/// "要開啟的檔名","w+"表示要寫/+檔案
printf("Hello\n");
fprintf(fout,"Hello\n");
int n;
scanf("%d",&n);
///故意讓程式還未讀到輸入時無法結束,
///檔案就無法正常關閉,檔案長度就會為0
}
NO.03
創建新的 freeglut 專案
照老師給的程式碼
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;///檔案一開始是空的 NULL
float angle=0;
int oldX=0;
void mouse(int button,int state,int x,int y)
{
oldX=x;
}
void motion(int x,int y)///用滑鼠改變關節的 angle
{
angle+=x-oldX;///角度稍微增減,以前教過的大象放入法
oldX=x;///把舊的更新成新的
if(fout==NULL)///一開始是空指標,所以會進來if(...)
{
fout=fopen("output.txt","w+");///第一節交的開檔
///之後不再是空指標了
}
fprintf(fout,"%.3f\n",angle);///把角度印到檔案裡
printf("%.3f\n",angle);///把角度印出來
glutPostRedisplay();///重複畫面
}
void display()///以下是老師提供的
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);///正中間,當成身體的茶壼
glPushMatrix();
glTranslatef(0.4, 0,0);///掛上去
glRotatef(angle, 0,0,1);///旋轉
glTranslatef(0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
///上面是我們增加的程式碼
///打光的部分留著 (陣列,main() ) 其他都刪掉
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 };
/* Program entry point */
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");
///準備2函式註冊好
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
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;
}
執行結果
正確關閉後再 bin 資料夾的 output.txt 會紀錄旋轉值
NO.04
沒有留言:
張貼留言