#include <stdio.h>
int main()
{
FILE * fout = fopen("motion.txt" , "w+");
fprintf(fout , "hello world\n");
}
在motion檔裡印出
#include <stdio.h>
int a[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main()
{
FILE * fout = fopen("motion.txt" , "w+");
for(int i=0; i<16; i++){
fprintf(fout, "%d ", a[i]); **加空格 , 不然會黏住**
}
fprintf(fout, "\n"); **印完陣列跳行**
for(int i=0; i<16; i++){
fprintf(fout, "%d ", a[i]);
}
fprintf(fout, "\n");
}
2. 畫上下手臂
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1,1,1); ///白色
glutSolidTeapot(0.3); ///body
glPushMatrix(); ///右邊
glTranslatef(0.3, 0,0);
glRotatef(angle[0], 0,0,1); ///轉動
glTranslatef(0.2, 0,0); ///最重要,旋轉中心要放在中心
glColor3f(1,0,0); ///紅色
glutSolidTeapot(0.2); ///右上手臂
glPushMatrix(); ///右下手臂
glTranslatef(0.2, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.2, 0,0);
glColor3f(1,1,0);///黃
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix(); ///左邊
glTranslatef(-0.3, 0,0);
glRotatef(angle[3], 0,0,1); ///轉動
glTranslatef(-0.2, 0,0); ///最重要,旋轉中心要放在中心
glColor3f(0,1,0); ///綠色
glutSolidTeapot(0.2); ///左上手臂
glPushMatrix();
glTranslatef(-0.2, 0,0);
glRotatef(angle[4], 0,0,1);
glTranslatef(-0.2, 0,0);
glColor3f(0,0,1); ///藍色
glutSolidTeapot(0.2); ///左下手臂
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");
glutDisplayFunc(display);
glutMainLoop();
}
3. 控制上下手臂(鍵盤 , 滑鼠 , 檔案)
int angleID=0; ///TODO2:要動哪個關節
int oldX=0, oldY=0; ///TODO2:
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0; ///TODO2: 要動的關節
if(key=='1') angleID=1; ///TODO2: 要動的關節
if(key=='2') angleID=2; ///TODO2: 要動的關節
if(key=='3') angleID=3; ///TODO2: 要動的關節
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y; ///TODO2: mouse按下去時, 記得在哪裡
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX); ///TODO2: 讓關節動起來
oldX=x;
glutPostRedisplay();
}

4. 按下r鍵,利用fopen()開另一個檔案, 接著用fscanf()讀入關節, 用glutPostRedisplay()更新畫面
5. 利用timer()來慢慢把動作讀進來,像動畫一樣








沒有留言:
張貼留言