- 打開 codeblocks-glut
- 備份main.cpp
- 今天第一步老師介紹了 fprintf
- 打完程式碼後freeglut-bin會出現檔名這個文字檔
- 程式碼:
#include <stdio.h>
int main( int argc,char*argv)
{
FILE * fout =NULL;///檔名的指標fout output用
fout = fopen("檔名.txt","w+");///開檔,使用w+模式
printf( "Hello World\n");
fprintf( fout,"Hello World\n");
}
- 把freeglut.dill複製到week15
- 把 working dir 開頭的 " " 裡面改成 .
- 剛剛改的是執行的工作目錄 working dir 改成現在的 .
- 介紹了fscanf
- 程式碼 :
#include <stdio.h>
int main( int argc,char*argv)
{
/// FILE * fout =NULL;///檔名的指標fout output用
/// fout = fopen("檔名.txt","w+");///開檔,使用w+模式
/// printf( "Hello World\n");
/// fprintf( fout,"Hello World\n");
FILE * fin = NULL;
fin = fopen("檔名.txt","r");
/// scanf("%d",&n);配int n
/// scanf("%f",&f);
char line [100];
fscanf(fin,"%s",line);
printf("你讀到了字串: %s\n",line);
fscanf(fin,"%s",line);
printf("你讀到了字串: %s\n",line);
}
=========================================================
- 下一階段製作了茶壺們
- 程式碼 :
#include <stdio.h>
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutMainLoop();
}
- 下一步加入 timer
- 程式碼 :
#include <stdio.h>
#include <GL/glut.h>
float angle=0,diff=2;
void timer(int t)
{
glutTimerFunc(30,timer,t+1);
angle+=diff;
if(angle>90)diff=-2;
if(angle<0)diff=+2;
glutPostRedisplay();///現在這個比較好,以前會用display()重劃
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//glutSolidTeapot(0.3);///身體
glPushMatrix();
glTranslatef(-0.3,0,0);///(3)掛在正確的地方
glRotatef(angle,0,0,1);///(2)旋轉
glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
glutSolidTeapot(0.3);///左手臂
glPushMatrix();
//glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutTimerFunc(0,timer,0);///step 04-2 設第一個timer做動畫
glutDisplayFunc(display);
glutMainLoop();
}
- 完成後會有一個茶壺在轉動的動畫
- 下一步
- 加入了keyboard
- 按1可以固定,按0繼續轉
- 程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
void timer(int t)
{
glutTimerFunc(30,timer,t+1);
angle[angleID]+=diff;
if(angle[angleID]>90)diff=-2;
if(angle[angleID]<0)diff=+2;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')angleID=1;
if(key=='0')angleID=0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow(" Week15 file ");
glutKeyboardFunc(keyboard);
glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutMainLoop();
}
- 加入右半邊
- 程式碼:
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05 本來只有1個角度,現在有很多個,初始為0
int angleID=0; ///Step05 現在要改第幾個角度!!!
void timer( int t ){
glutTimerFunc( 30, timer, t+1 );
angle[angleID] += diff; ///Step05
if(angle[angleID]>90) diff=-2; ///Step05
if(angle[angleID]<0) diff=+2; ///Step05
glutPostRedisplay();///現在這個比較好, 以前會用 display()重畫
}
void keyboard( unsigned char key, int x, int y){ ///Step05
if(key=='0') angleID=0; ///Step05
if(key=='1') angleID=1; ///Step05
if(key=='2') angleID=2; ///Step05
if(key=='3') angleID=3; ///Step05
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///左半邊
glTranslatef(-0.3,0,0);///(3)掛在正確的地方
glRotatef(angle[0], 0,0,1);///(2)旋轉 ///Step05
glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
glPushMatrix();
glTranslatef(-0.3,0,0);///(3)掛在正確的地方
glRotatef(angle[1], 0,0,1);///(2)旋轉 ///Step05
glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPopMatrix();
glPopMatrix();
glPushMatrix();///右半邊
glTranslatef(+0.3, 0,0 );
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3, 0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();
glTranslatef(+0.3, 0,0 );
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3, 0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15 file");
glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
glutDisplayFunc(display);
glutMainLoop();
}









沒有留言:
張貼留言