/ week13 /
主題 : 檔案
主題 : 關節、擺動作
(一) 主題 : 檔案
(1) 開啟一個 codeblocks
(2) 開啟一個project ( file > new > project )
(3) 點選 console application
(4) 打上程式碼
( 以下為程式碼 )
#include <stdio.h>
FILE * fout =NULL; ///宣告 檔案指標 變數
int main()
{
fout =fopen("filename.txt","w+"); ///開檔案 等一下要寫
for(int i=0;i<20;i++)
{
fprintf(fout,"Hello World\n");
}
}
(5) build and run之後你剛剛打的東西會出現在你的資療夾中的記事本
(6) 修改一下程式碼
( 以下為程式碼 )
#include <stdio.h>
FILE * fout =NULL; ///宣告 檔案指標 變數
float angle[20];
int main()
{
fout =fopen("motion.txt","w+"); ///開檔案 等一下要寫
for(int i=0;i<20;i++)
{
fprintf(fout," %.1f",angle[i]);
///要小心 沒有空格就會失敗
///%f會印出太多0 不好看 要小樹點下一位 %.1f
}
}
此維修改的地方
(7) build and run之後你剛剛打的東西會出現在你的資療夾中的記事本
(二) 主題 : 關節、擺動作 ( 複習 )
(1) 開啟一個新project ( file > new > project )
(2) 開啟一個 GLUT project 專案
(3) 到 這裡 下載壓縮檔
(4) 到lib裡面複製 libfreeglut 改名為libglut32
(5) 複製資料夾連結
(6) 即可以得到
(7) 貼上老師給的程式碼,得到一個茶壺
( 以下為程式碼 )
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutDisplayFunc(display);
glutMainLoop();
}
(8) 轉動茶壺手臂 點及滑鼠可以轉動
( 以下為程式碼 )
#include <GL/glut.h>
float angle[20];///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[1] += (x-oldX);///NOW
oldX = x;///NOW
display();///NOW
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
(9) 貼上程式碼 得到手肘( 關節 )
( 以下為程式碼 )
#include <GL/glut.h>
float angle[20];///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[1] += (x-oldX);///NOW
oldX = x;///NOW
display();///NOW
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
(10) 貼上程式碼 得到左右手臂手肘( 關節 )
( 以下為程式碼 )
#include <GL/glut.h>
float angle[20];///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///left Arm
glTranslatef(-0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(-0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[1] += (x-oldX);///NOW
oldX = x;///NOW
display();///NOW
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
(11) 貼上程式碼 移動手臂可以跑出數值
( 以下為程式碼 )
#include <stdio.h> ///(0) ///NOW4
#include <GL/glut.h>
FILE * fout=NULL;///(1) ///NOW4
float angle[20];///NOW
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///left Arm
glTranslatef(-0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(-0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[angleID] += (x-oldX);///NOW ///NOW3
oldX = x;///NOW
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
display();///NOW
}
void keyboard(unsigned char key, int x, int y)///NOW3
{
if(key=='1') angleID=1;///NOW3
if(key=='2') angleID=2;///NOW3
if(key=='3') angleID=3;///NOW3
if(key=='4') angleID=4;///NOW3
if(key=='w' || key=='W'){///NOW4 小寫的 w
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
(12) 貼上程式碼 按下鍵盤即可以讓手臂轉動
( 以下為程式碼 )
#include <stdio.h> ///(0) ///NOW4
#include <GL/glut.h>
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5
float angle[20];///NOW
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///left Arm
glTranslatef(-0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(-0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[angleID] += (x-oldX);///NOW ///NOW3
oldX = x;///NOW
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
display();///NOW
}
void keyboard(unsigned char key, int x, int y)///NOW3
{
if(key=='1') angleID=1;///NOW3
if(key=='2') angleID=2;///NOW3
if(key=='3') angleID=3;///NOW3
if(key=='4') angleID=4;///NOW3
if(key=='w' || key=='W'){///NOW4 小寫的 w
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
}
if(key=='r'){///NOW5
if(fin==NULL) fin = fopen("motion.txt", "r");///NOW5
for(int i=0; i<20;i++){///NOW5
fscanf(fin, "%f", &angle[i]);///NOW5
}///NOW5
}
glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言