#include<GL/glut.h>
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='s'){
void keyboard(unsigned char key,int x,int y)
{
if(key=='s'){
}else if(key=='r'){
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
glutPostRedisplay();
}else if(key=='p'){
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
glutPostRedisplay();
}else if(key=='p'){
}
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
修改程式碼之後
#include <stdio.h>
#include<GL/glut.h>
int angleID=0;
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
FILE*fout=NULL,*fin=NULL;
void keyboard(unsigned char key,int x,int y)
{
if(key=='0'){
angleID=1;
}else if(key=='1'){
angleID=1;
}else if(key=='2'){
angleID=2;
}else if(key=='s'){
if(fout==NULL)fout=fopen("this_is_my_saved_angles.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%f",angle[i]);
fprintf(fout,"\n");
}else if(key=='r'){
if(fin==NULL) fin=fopen("this is my saved angles.txt","r");
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
if(a>=1){
a=0;
for(int i=0;i<20;i++){
angleOld[i]=angleNew[i];
fscanf(fin,"%f",&angleNew[i]);
}
}
glutPostRedisplay();
}else if(key=='p'){
}
}
float oldX=0,oldY=0;
void mouse(int button,int state,int x,int y){
oldX=x;oldY=y;
}
void motion(int x,int y){
angle[angleID] += (x-oldX); oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
加入程式碼 讓茶壺可以自動撥放 不用手動
#include <stdio.h>
#include<GL/glut.h>
int angleID=0;
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
FILE*fout=NULL,*fin=NULL;
void readNext(){
if(fin==NULL) fin=fopen("this_is_my_saved_angles.txt","r");
a+=0.1;
for(int i=0;i<20;i++)
angle[i]=angleOld[i]*(1-a)+ angleNew[i]*a;
if(a>=1){
a=0;
for(int i=0;i<20;i++){
angleOld[i]=angleNew[i];
fscanf(fin,"%f",&angleNew[i]);
}
}
}
void timer (int t){
glutTimerFunc(100,timer,0);//100表示要100ms=0.1秒 就會在較一次timer
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='0'){
angleID=1;
}else if(key=='1'){
angleID=1;
}else if(key=='2'){
angleID=2;
}else if(key=='s'){
if(fout==NULL)fout=fopen("this_is_my_saved_angles.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%f",angle[i]);
fprintf(fout,"\n");
}else if(key=='r'){
readNext();
glutPostRedisplay();
}else if(key=='p'){
glutTimerFunc(0,timer,0);
}
}
float oldX=0,oldY=0;
void mouse(int button,int state,int x,int y){
oldX=x;oldY=y;
}
void motion(int x,int y){
angle[angleID] += (x-oldX); oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}



沒有留言:
張貼留言