(2) 輸入程式碼,印出 Hello World
#include <stdio.h>
int main()
{
FILE * fout = fopen("output.txt", "w+");
printf("Hello World\n");
}

(3) 修改程式碼
將 printf("Hello World\n"); 改為 fprintf(fout , "Hello World\n");
執行程式時, 讓 Hello World 以檔案的形式輸出

----------------------------------------------------------------------------------
(1) 在 Codeblocks 開啟 GLUT 程式
到moodle下載freeglut-MinGW-3.0.0-1.mp.zip > 解壓縮 > freeglut > lib >
複製libfreeglut.a > 貼上並重新命名為libglut32.a > 開啟Codeblocks > File >
New > Project > GLUT
(2) 修改程式碼, 利用上周教的機器人關節手臂
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL; //宣告檔案的指標
float angle=60, 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+");
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);
glPushMatrix();
glutSolidTeapot(0.3); //畫出茶壺
glPushMatrix(); //利用上周考的TRT旋轉方法
glTranslatef(0.4, 0, 0);
glRotatef(angle, 0,0,1);
glTranslatef(0.4, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
angle = x;
glutPostRedisplay();///Now:
}
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); //加入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();
return EXIT_SUCCESS;
}

(3) 按下 s 鍵時,會印出角度值

(4) 在motion函式中加入程式碼
void motion(int x, int y)
{
angle = x;
if(fout==NULL) fout=fopen("output.txt", "w+");
fprintf(fout, "%.3f %.3f %.3f\n", angle, angle2, angle3);
printf("%.3f %.3f %.3f\n", angle, angle2, angle3);
glutPostRedisplay();
}
移動關節時,會自動印出角度值
沒有留言:
張貼留言