(一)printf結合資料讀取
首先新增一個console專案>存桌面
然後打上
#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");
}
}
執行之後,會在桌面專案資料夾中看到一個txt檔
將檔案用右箭以Notepad++開啟
就會看到成是執行的結果
就完成用printf結合資料讀取了
將檔案用右箭以Notepad++開啟
就會看到成是執行的結果
就完成用printf結合資料讀取了
(三)劃出一個圖形,複習上次的TRT
先開個新GLUT專案
在CodeBlocks打上
#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();
}
就可以跑出一個大ㄉㄟˊ垢
(四)幫ㄉㄟˊ垢加上手臂with TRT
#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();///右手上臂
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///橘色
glutSolidTeapot( 0.2 );
glPushMatrix();///右手下臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0.3,0.1);///橘色
glutSolidTeapot( 0.2 );
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);
glutCreateWindow("week 13 motion TRT angle file");
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
以上就會生出一條手,分成上下手臂
再加上一段TRT,改位置
貼在右手臂PopMatrix後、一行PopMatrix及glutSwapBuffers前
就可以長出左手
雙手的上臂及下臂我用紅色跟橘色表示
glPushMatrix();///左手上臂
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();///左手下臂
glTranslatef(-0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0.3,0.1);///orange
glutSolidTeapot( 0.2 );///left lower arm
glPopMatrix();
glPopMatrix();
(五)按鍵盤控制哪隻手臂旋轉剛剛上面的都是兩隻手會一起旋轉現在要寫指令,當按1、2、3、4的時候可以控制分別要轉動哪個手臂
在void motionvoid 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}
沒有留言:
張貼留言