2014年6月12日 星期四

WeeK15

#include <GL/glut.h>
float a=0;
float angle=0,anglex=0,angley=90;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r')
{
a+=0.1;
angle=anglex*(1-a)+angley*a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");

glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
*內插公式:anglex*(1-比重)+angley*比重
#include <GL/glut.h>
float a=0;//比重 
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float anglex[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angley[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();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r')
{
a+=0.1;
for(int i=0;i<20;i++)
angle[i]=anglex[i]*(1-a)+angley[i]*a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
*使用Timer按P之後自動旋轉
#include <GL/glut.h>

float a=0;//比重 
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float anglex[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angley[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();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
a+=0.1;
for(int i=0;i<20;i++)angle[i]=anglex[i]*(1-a)+angley[i]*a;
}
void timer(int t)
{
glutTimerFunc(100,timer,0);//每100ms動一次 
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r')readNext();
else if(key=='p')glutTimerFunc(0,timer,0);//立即播放 
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
*滑鼠控制旋轉和紀錄角度
#include <stdio.h> 
#include <GL/glut.h>
float a=0;//比重 
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float anglex[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angley[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FILE *fout=NULL, *fin=NULL;
int angleID=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
if(fin==NULL)fin=fopen("this_is_my_file_for_animation.txt","r");
a+=0.05;
for(int i=0;i<20;i++)angle[i]=anglex[i]*(1-a)+angley[i]*a;
if(a>1.0)
{
a=0.0;
for(int i=0;i<20;i++)
{
anglex[i]=angley[i];
fscanf(fin,"%f",&angley[i]);
}
}
}
int 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();
}

void timer(int t)
{
glutTimerFunc(100,timer,0);//每100ms動一次 
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0')angleID=0;
if(key=='1')angleID=1;
if(key=='s')
{
if(fout==NULL)fout=fopen("this_is_my_file_for_animation.txt","w+");
for(int i=0;i<20;i++)
{
printf("%f",angle[i]);
fprintf(fout,"%f",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
if(key=='r')readNext();
else if(key=='p')glutTimerFunc(0,timer,0);//立即播放 
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}




沒有留言:

張貼留言