Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <GL/glu.h>
- #include <windows.h>
- #include <fstream>
- #include <vector>
- #include <GL/glut.h> // gl.h and glu.h are already included in glut.h
- using namespace std;
- #define MAX_N 1004
- ifstream fiA("A.txt");
- ifstream fiB("B.txt");
- float** A;
- float** B;
- int numberOfVertexes;
- void init() {
- // select clearing (background) color
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 800.0, 0.0, 600.0);
- }
- void addPoint(float** shape,int n,int k)
- {
- int interval = n/k;
- for (int i=n;i<n+k;i++)
- shape[i] = new float[2];
- int i=0;
- int d=interval;
- vector<float> points[2];
- while (i<n)
- {
- points[0].push_back(shape[i][0]);
- points[1].push_back(shape[i][1]);
- d--;
- if (d==0&&k!=0)
- {
- int nexti=(i+1)%n;
- float nx=(shape[i][0]+shape[nexti][0])/(2.0);
- float ny=(shape[i][1]+shape[nexti][1])/(2.0);
- points[0].push_back(nx);
- points[1].push_back(ny);
- //points[0].push_back(shape[nexti][0]);
- //points[1].push_back(shape[nexti][1]);
- k--;
- d = interval;
- }
- }
- for (int i=0;i<n+k;i++)
- {
- for (int j=0;j<2;j++)
- shape[i][j] = points[i][j];
- }
- }
- void initShape() {
- int n,m;
- fiA>>n;
- fiB>>m;
- A = new float*[n];
- B = new float*[m];
- for (int i=0;i<n;i++)
- {
- A[i] = new float[2];
- fiA>>A[i][0]>>A[i][1];
- }
- for (int i = 0; i < m;i++)
- {
- B[i] = new float[2];
- fiB>>B[i][0]>>B[i][1];
- }
- if (n>m)
- {
- addPoint(B,m,(n-m));
- }
- else
- addPoint(A,n,(m-n));
- numberOfVertexes = max(n,m);
- }
- float tweening(float a, float b, float t) {
- return (1-t)*a + t*b;
- }
- void drawTweening(float** A, float** B, int n, float t) {
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < n; ++i) {
- float Px = tweening(A[i][0], B[i][0], t);
- float Py = tweening(A[i][1], B[i][1], t);
- glVertex2f(Px, Py);
- }
- glEnd();
- glFlush();
- }
- void display(void) {
- for (float t = 0.0, deltaT = 0.01; ; t += deltaT) {
- glClear(GL_COLOR_BUFFER_BIT);
- drawTweening(A, B, numberOfVertexes, t);
- glutSwapBuffers();
- if (t >= 1.0 || t < 0.0) deltaT = -deltaT;
- Sleep(50);
- }
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- // single buffer and RGBA
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- // window size
- glutInitWindowSize(800, 600);
- // window position
- glutInitWindowPosition(100, 100);
- // title bar
- glutCreateWindow("Tweening");
- // call initialization routines
- init();
- initShape();
- // Register callback function to display graphics
- glutDisplayFunc(display);
- // Enter main loop and process events
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement