#include<windows.h>
#include<math.h>
#include <gl/Glut.h>
#include <iostream>
#include <vector>
using namespace std;
const GLint screenWidth = 1050;
const GLint screenHeight = 200;
struct GLPoint{
GLdouble x, y;
};
GLPoint ptA[9] = {{100,0},{200,50},{200,200},{130,200},{130,130},{80,130},{80,200},{0,200},{0,50}};
GLPoint ptB[9] = {{100,50},{200,50},{200,100},{130,100},{130,200},{80,200},{80,100},{0,100},{0,50}};
GLPoint ptC[9] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
void myDisplay(void);
void myInit(void);
void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void drawTween(GLPoint ptA[],GLPoint ptB[],int n, float t);
int main(int argc, char ** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth,screenHeight);
glutInitWindowPosition(100,100);
glutCreateWindow("内插动画");
setWindow(0,screenWidth,0,screenHeight);
setViewport(0,screenWidth,0,screenHeight);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
glViewport(left,right,right-left,top-botton);
}
void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left,right,botton,top);
}
void drawTween(GLPoint ptA[],GLPoint ptB[],int n, float t){
for(int i = 0 ; i < n; i++){
ptC[i].x = ptA[i].x + (ptB[i].x - ptA[i].x) * t;
ptC[i].y = ptA[i].y + (ptB[i].y - ptA[i].y) * t;
}
glBegin(GL_LINE_LOOP);
for (int j = 0; j< n ; j++)
{
glVertex2d(ptC[j].x, screenHeight - ptC[j].y);
}
glEnd();
glFlush();
}
void myDisplay(void){
//cout<<sizeof(ptA)/sizeof(ptA[0])<<endl; //数组长度
glClear(GL_COLOR_BUFFER_BIT);
GLdouble t = 0.0;
for ( int i = 0; i<5; i++)
{
setWindow(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
glViewport(i*200,0,1000,200);
drawTween(ptA,ptB,9,t);
t+=0.25;
}
//drawTween(ptA,9,0);
/*glClear(GL_COLOR_BUFFER_BIT);
drawTween(ptA,ptB,9,0.5);*/
}
void myInit(void){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);
glLineWidth(2.0);
}