1

我正在尝试创建缩放旋转六边形的效果。我通过更改窗口来完成此操作。一旦它“放大”它应该“缩小”,然后不断重复。我已经设法放大得很好,从我的代码的外观来看,它也应该缩小,但是一旦放大,就什么也没有了。我已经调试了我的代码,我可以看出这些变量确实在这一行增加了:

gluOrtho2D(cx - w, cx + w, cy -h, cy +h);

但是我仍然看不到我的六边形“缩小”。任何帮助,将不胜感激。我很确定它很简单,我忘记了。但它一直在躲避我。我的代码如下:

#include <cstdlib>
#include <GL/glut.h>
#include <cmath>
#define PI 3.14159265
#define ZOOM_IN 1
#define ZOOM_OUT -1

using namespace std;

const int screenWidth = 500;
const int screenHeight = 500;
    float cx = 0.0, cy = 0.0;       //center of viewport (cx, cy)
    float h=1.2, w = 1.2;           //window size
    int NumFrames = 10;             //frames
    int frame = 0;
    int direction = ZOOM_IN;


//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myinit() {
    glClearColor (1.0, 1.0, 1.0, 1.0);          //set the background color to white 
    glColor3f (0.0, 0.0, 0.0);                  //set the foreground color to black
    glPointSize (3.0);                          //set the point size to 3 X 3 pixels
    glViewport (0.0, 0.0, 500.0, 500.0);        //set the viewport to be the entire window

    //set up a world window to screen transformation
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
//  glMatrixMode (GL_MODELVIEW);

}


//<<<<<<<<<<<<<<<<<<<<<<< hexswirl >>>>>>>>>>>>>>>>>>>>
void hexswirl() {
    double angle;                       //the angle of rotation
    double angleInc = 2*PI/6.0;         //the angle increment
    double inc = 5.0/50;                //the radius increment
    double radius = 5.0/50.0;           //the radius to be used


    //clear the background
    glClear (GL_COLOR_BUFFER_BIT);

    //draw the hexagon swirl
    for (int j = 0; j <= 50; j++) {
        //the angle of rotation depends on which hexagon is 
        //being drawn.
        angle = j* (PI/180.0);

        //draw one hexagon
        glBegin (GL_LINE_STRIP);
            for (int k=0; k <= 6; k++) {
                angle += angleInc;
                glVertex2d(radius * cos(angle), radius *sin(angle));

            }
        glEnd();

        //determine the radius of the next hexagon
        radius += inc;
    }
    //swap buffers for a smooth change from one
    //frame to another
    glutSwapBuffers();
    glutPostRedisplay();
    glFlush();
}


//<<<<<<<<<<<<<<<<<<<<<<< viewZoom >>>>>>>>>>>>>>>>>>>>
void viewZoom(int i) {


    if(direction == ZOOM_IN) {
        //change the width and height of the window each time
        w *= 0.9;
        h *= 0.9; 
    } 
    if(direction == ZOOM_OUT) {
        w /= 0.9;
        h /= 0.9;
        }   

    if(i%10 == 0) {
        direction = -direction;
    }
        //change the window and draw the hexagon swirl
        gluOrtho2D (cx - w, cx + w, cy - h, cy + h);

        hexswirl();

        glutPostRedisplay();
        glutTimerFunc(200, viewZoom,i+1);

}
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv) {
    glutInit(&argc, argv);  
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(100,100);
    glutCreateWindow("hexanim");

    glutDisplayFunc(hexswirl);
    viewZoom(1);
    myinit();

    glutMainLoop();
    return 1;
}
4

1 回答 1

1

我想出了解决问题的方法。我仍然不知道为什么我的窗口在“放大”后没有重绘,但我决定通过改变我的视口来实现它。我最终换掉了:

gluOrtho2D (cx - w, cx + w, cy - h, cy + h);

为了

    cx = screenWidth / w;
    cy = screenHeight / h;
        glViewport((screenWidth-cx)/2, (screenHeight-cy)/2, cx, cy);

(并进行了与之相关的所有相应更改)。

于 2013-09-27T01:53:29.383 回答