第一節課
主題:存檔、讀檔
step01
程式碼:
#include <stdio.h>
int main(int argc,char** argv)
{
FILE*fout=NULL;
fout=fopen("檔名.txt","w+");
printf("Hello World\n");
fprintf(fout,"Hello World\n");
}
圖片:
接下來用notepad++開啟專案檔把working_dir改成"."
一樣可以執行成功 只是在codeblocks的專案檔裡面會有a.txt
step03
用fscaanf來讀檔案
一個印出hello 一個印出world
程式碼:
#include <stdio.h>
int main(int argc,char** argv)
{
// FILE*fout=NULL;
// fout=fopen("檔名.txt","w+");
// printf("Hello World\n");
// fprintf(fout,"Hello World\n");
FILE*fin=NULL;
fin=fopen("檔名.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("現在讀到的是%s\n",line);
fscanf(fin,"%s",line);
printf("現在讀到的是%s\n",line);
}
圖片:
step04
先叫出茶壺
程式碼:
#include <stdio.h>
#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);
glutCreateWindow("week15 many angles");
glutDisplayFunc(display);
glutMainLoop();
}
圖片:
step05
程式碼:
#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();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
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 many angles");
glutDisplayFunc(display);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
step05-2
程式碼:
#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();
}
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,1);
glTranslatef(-0.3,0,0); glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15 many angles");
glutDisplayFunc(display);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
圖片:
step05-3
程式碼:
#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();
}
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,1);
glTranslatef(-0.3,0,0); glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glutSolidTeapot(0.3);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();///Step05-2
glTranslatef(+0.3,0,0);
glRotatef(-angle, 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();///step05-3
glTranslatef(+0.3,0,0);
glRotatef(-angle, 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();///Step05-3
glPopMatrix();///Step05-2
glPopMatrix();///Step05-2
glutSwapBuffers();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15 many angles");
glutDisplayFunc(display);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
沒有留言:
張貼留言