利用codeblocks呼叫產生檔案
code
#include <stdio.h>
int main()
{
FILE * fout=fopen("output.txt","w+");
///FILE * 檔案指標 fout
/// 檔案file
/// open打開
/// "要開啟的檔名""w+表示要寫/+檔案"
printf("Hello World!\n");
fprintf(fout,"Hello World\n");
}
***EX***
Step2
讀取浮點值
code
#include <stdio.h>
float angle=0, angle2=10, angle3=90;
int main()
{
FILE * fout=fopen("outanglemathput.txt","w+");
///FILE * 檔案指標 fout
/// 檔案file
/// open打開
/// "要開啟的檔名""w+表示要寫/+檔案"
fprintf(fout,"%.3f %.3f %.3f\n",angle,angle2,angle3);
}
Step3
標記座標((專案的txt檔 在freeglut資料夾裡
code
///打光函式前都刪掉
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float angle=0;
int oldX=0;
void mouse(int botton, int state, int x,int y)
{
oldX=x;
}
void motion(int x,int y)
{
angle += x-oldX;
oldX=x;
if(fout==NULL){
fout = fopen("outtrans.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內需更改
///
glutMouseFunc(mouse);
glutMotionFunc(motion);
///
///glutReshapeFunc(resize);
glutDisplayFunc(display);
///glutKeyboardFunc(key);
///glutIdleFunc(idle);
glClearColor(1,1,1,1);
///glEnable(GL_CULL_FACE);
///glCullFace(GL_BACK);
Step4
紀錄路徑
藉由按空白鍵 跑紀錄路徑

code
FILE * fout=NULL;
FILE * fin=NULL;
float angle=0;
int oldX=0;
void keyboard(unsigned char key, int x, int y)
{
if(fin==NULL)
{
fin = fopen("outtrans.txt","r");
}
fscanf(fin, "%f",&angle);
glutPostRedisplay();
}
void mouse(int botton, int state, int x,int y)
{
oldX=x;
}
.
.
.
.
///
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
///
Step5
讀入關節

But非常之難
code
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
//float angle=0, angle2, angle3, angle4, angle5, angle6;
float angle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};///Now4: 用陣列
int oldX=0, now=0;///angle[now]
void keyboard(unsigned char key, int x, int y)///Now3: 要記得main()註冊
{///Now3: 用keyboard來讀入動畫資料
if(key=='1') now=1;///按1就會改變1號關節
if(key=='2') now=2;///按2就會改變3號關節
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='5') now=5;
if(key=='6') now=6;
if(key=='7') now=7;
if(key=='8') now=8;
if(key=='9') now=9;
if(key=='r' || key=='R'){///Now4: 按'r'才會讀入關節的角度,一次讀20個
if(fin==NULL){///Now3: TODO:一開始是空指標,所以會進來if(..)
fin=fopen("output.txt", "r");///Now4: r表示讀入read
}
for(int i=0;i<20;i++) fscanf(fin, "%f", &angle[i]);///Now4: 用陣列從檔案fin讀進來
}
if(key=='w' || key=='W' || key=='s' || key=='S'){///Now4: 按's'才會寫關節的角度,一次20個
if(fout==NULL){///TODO:一開始是空指標,所以會進來if(..)
fout = fopen("output.txt", "w+");///第一節教的開檔
}///TODO:之後就不再是空指標了
for(int i=0;i<20;i++) fprintf(fout, "%.3f\n", angle[i]);///TODO:把角度印到檔案裡
}
// fscanf(fin, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
// &angle[0], &angle[1], &angle[2], &angle[3], &angle[4], &angle[5],
// &angle[6], &angle[7], &angle[8], &angle[9], &angle[10], &angle[11],
// &angle[12], &angle[13], &angle[14], &angle[15], &angle[16], &angle[17],
// &angle[18], &angle[19] );
glutPostRedisplay();///Now3: 讀入資料,馬上重畫
}
void mouse(int botton, int state, int x,int y)
{
oldX=x;
}
void motion(int x, int y)///TODO: 用mouse motion來改關節angle
{///TODO:
angle[now] += x-oldX; ///角度稍微增減, 以前教過把大象放到冰箱去的程式碼
oldX=x;///把舊的更新成新的
glutPostRedisplay();///TODO:重畫畫面
}
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,0,1);///
glTranslatef(0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPushMatrix();
glTranslatef(0.4, 0,0);///掛上去
glRotatef(angle[1], 0,0,1);///
glTranslatef(0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4, 0,0);///掛上去
glRotatef(angle[2], 0,0,1);///
glTranslatef(-0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPushMatrix();
glTranslatef(-0.4, 0,0);///掛上去
glRotatef(angle[3], 0,0,1);///
glTranslatef(-0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
Step4
紀錄路徑
藉由按空白鍵 跑紀錄路徑
code
FILE * fout=NULL;
FILE * fin=NULL;
float angle=0;
int oldX=0;
void keyboard(unsigned char key, int x, int y)
{
if(fin==NULL)
{
fin = fopen("outtrans.txt","r");
}
fscanf(fin, "%f",&angle);
glutPostRedisplay();
}
void mouse(int botton, int state, int x,int y)
{
oldX=x;
}
.
.
.
.
///
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
///
Step5
讀入關節
But非常之難
code
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
//float angle=0, angle2, angle3, angle4, angle5, angle6;
float angle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};///Now4: 用陣列
int oldX=0, now=0;///angle[now]
void keyboard(unsigned char key, int x, int y)///Now3: 要記得main()註冊
{///Now3: 用keyboard來讀入動畫資料
if(key=='1') now=1;///按1就會改變1號關節
if(key=='2') now=2;///按2就會改變3號關節
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='5') now=5;
if(key=='6') now=6;
if(key=='7') now=7;
if(key=='8') now=8;
if(key=='9') now=9;
if(key=='r' || key=='R'){///Now4: 按'r'才會讀入關節的角度,一次讀20個
if(fin==NULL){///Now3: TODO:一開始是空指標,所以會進來if(..)
fin=fopen("output.txt", "r");///Now4: r表示讀入read
}
for(int i=0;i<20;i++) fscanf(fin, "%f", &angle[i]);///Now4: 用陣列從檔案fin讀進來
}
if(key=='w' || key=='W' || key=='s' || key=='S'){///Now4: 按's'才會寫關節的角度,一次20個
if(fout==NULL){///TODO:一開始是空指標,所以會進來if(..)
fout = fopen("output.txt", "w+");///第一節教的開檔
}///TODO:之後就不再是空指標了
for(int i=0;i<20;i++) fprintf(fout, "%.3f\n", angle[i]);///TODO:把角度印到檔案裡
}
// fscanf(fin, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
// &angle[0], &angle[1], &angle[2], &angle[3], &angle[4], &angle[5],
// &angle[6], &angle[7], &angle[8], &angle[9], &angle[10], &angle[11],
// &angle[12], &angle[13], &angle[14], &angle[15], &angle[16], &angle[17],
// &angle[18], &angle[19] );
glutPostRedisplay();///Now3: 讀入資料,馬上重畫
}
void mouse(int botton, int state, int x,int y)
{
oldX=x;
}
void motion(int x, int y)///TODO: 用mouse motion來改關節angle
{///TODO:
angle[now] += x-oldX; ///角度稍微增減, 以前教過把大象放到冰箱去的程式碼
oldX=x;///把舊的更新成新的
glutPostRedisplay();///TODO:重畫畫面
}
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,0,1);///
glTranslatef(0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPushMatrix();
glTranslatef(0.4, 0,0);///掛上去
glRotatef(angle[1], 0,0,1);///
glTranslatef(0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4, 0,0);///掛上去
glRotatef(angle[2], 0,0,1);///
glTranslatef(-0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPushMatrix();
glTranslatef(-0.4, 0,0);///掛上去
glRotatef(angle[3], 0,0,1);///
glTranslatef(-0.4, 0,0);///把柄放中間
glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
沒有留言:
張貼留言