2014年5月30日 星期五

week15


#include <GL/glut.h>
float a = 0, angle = 0, angleOld = 0, angleNew = 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 = angleOld*(1-a) + angleNew*a;
 }
 glutPostRedisplay();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("Week15");
 glutKeyboardFunc(keyboard);
 glutDisplayFunc(display);
 glutMainLoop();
}



#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 angleOld[20] = {0,90,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();
glTranslatef(angle[3], angle[4], angle[5]);
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();
glPopMatrix();
glutSwapBuffers();
}

void readNext()
{
a += 0.1;
for(int i = 0; i<20 ;i++) angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
}

void timer (int t)
{
glutTimerFunc(10, timer, 0);
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 angleOld[20] = {0,90,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};
int angleID = 0;
FILE *fout = NULL, *fin = NULL;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(angle[3], angle[4], angle[5]);
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();
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] = angleOld[i] * (1-a) + angleNew[i] * a;
if(a>=1.0)
{
a = 0.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);
readNext();
glutPostRedisplay();
}

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 keyboard(unsigned char key, int x, int y)
{
if(key == '0')angleID = 0;
if(key == '1')angleID = 1;
if(key == '2')angleID = 2;
if(key == '3')angleID = 3;
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");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
 glutMotionFunc(motion);
 glutMouseFunc(mouse);
glutMainLoop();
}

沒有留言:

張貼留言