OpenGL绘图缩放一个多边形而不是两个

bttbmeg0  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(130)

我试图改变第二个薄片(多边形)的比例,但保持第一个相同。目前它看起来像是改变他们两个到相同的比例,我如何解决这个问题?
多边形是用draw()方法绘制的,我想通过setproperties()方法更改比例。

jum4pzuy

jum4pzuy1#

将传递给setProperties()的比例因子保存到成员变量中,并在draw()期间使用这些比例因子,方法是在初始glPushMatrix()之后执行glScalef()


# include <GL/glut.h>

const int FLAKE_COUNT = 5;

enum ROTATE_DIRECTION { CLOCKWISE = -1, ANTICLOCKWISE = 1 };

class Flake {
    float colorR, colorG, colorB;

    // vertex coordinates of one-half arm of a flake
    float flakeside[16][2] = { 0.0 , 0.0,
                                2.0 , 0.5,
                                7.0,  0.5,
                                10.0, 5.5,
                                12.0, 5.5,
                                10.0, 0.5,
                                15.0, 0.5,
                                17.0, 5.0,
                                18.5, 5.0,
                                17.0, 0.5,
                                20.0, 0.5,
                                21.0, 3.0,
                                22.0, 3.0,
                                21.5, 0.5,
                                24.0, 0.5,
                                24.5, 0.0 };

    ROTATE_DIRECTION rot_direction;

    float flakeside_tformed[16][2];

    float mSx = 1.0f;
    float mSy = 1.0f;

    void drawflakeside() 
    {
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 2; j++) {
                flakeside_tformed[i][j] = flakeside[i][j];
            }
        }

        glBegin(GL_LINE_LOOP);
        glVertex2f(flakeside_tformed[0][0], flakeside_tformed[0][1]);
        glVertex2f(flakeside_tformed[1][0], flakeside_tformed[1][1]);
        glVertex2f(flakeside_tformed[2][0], flakeside_tformed[2][1]);
        glVertex2f(flakeside_tformed[3][0], flakeside_tformed[3][1]);
        glVertex2f(flakeside_tformed[4][0], flakeside_tformed[4][1]);
        glVertex2f(flakeside_tformed[5][0], flakeside_tformed[5][1]);
        glVertex2f(flakeside_tformed[6][0], flakeside_tformed[6][1]);
        glVertex2f(flakeside_tformed[7][0], flakeside_tformed[7][1]);
        glVertex2f(flakeside_tformed[8][0], flakeside_tformed[8][1]);
        glVertex2f(flakeside_tformed[9][0], flakeside_tformed[9][1]);
        glVertex2f(flakeside_tformed[10][0], flakeside_tformed[10][1]);
        glVertex2f(flakeside_tformed[11][0], flakeside_tformed[11][1]);
        glVertex2f(flakeside_tformed[12][0], flakeside_tformed[12][1]);
        glVertex2f(flakeside_tformed[13][0], flakeside_tformed[13][1]);
        glVertex2f(flakeside_tformed[14][0], flakeside_tformed[14][1]);
        glVertex2f(flakeside_tformed[15][0], flakeside_tformed[15][1]);
        glEnd();

        glBegin(GL_LINE_LOOP);
        glVertex2f(flakeside_tformed[0][0], -flakeside_tformed[0][1]);
        glVertex2f(flakeside_tformed[1][0], -flakeside_tformed[1][1]);
        glVertex2f(flakeside_tformed[2][0], -flakeside_tformed[2][1]);
        glVertex2f(flakeside_tformed[3][0], -flakeside_tformed[3][1]);
        glVertex2f(flakeside_tformed[4][0], -flakeside_tformed[4][1]);
        glVertex2f(flakeside_tformed[5][0], -flakeside_tformed[5][1]);
        glVertex2f(flakeside_tformed[6][0], -flakeside_tformed[6][1]);
        glVertex2f(flakeside_tformed[7][0], -flakeside_tformed[7][1]);
        glVertex2f(flakeside_tformed[8][0], -flakeside_tformed[8][1]);
        glVertex2f(flakeside_tformed[9][0], -flakeside_tformed[9][1]);
        glVertex2f(flakeside_tformed[10][0], -flakeside_tformed[10][1]);
        glVertex2f(flakeside_tformed[11][0], -flakeside_tformed[11][1]);
        glVertex2f(flakeside_tformed[12][0], -flakeside_tformed[12][1]);
        glVertex2f(flakeside_tformed[13][0], -flakeside_tformed[13][1]);
        glVertex2f(flakeside_tformed[14][0], -flakeside_tformed[14][1]);
        glVertex2f(flakeside_tformed[15][0], -flakeside_tformed[15][1]);
        glEnd();
    }

public:
    void draw() {
        glPushMatrix();
        glScalef(mSx, mSy, 1.0f);
        drawflakeside();

        glRotatef(60.0f, 0.0f, 0.0f, 1.0f);
        drawflakeside();

        glRotatef(60.0f, 0.0f, 0.0f, 1.0f);
        drawflakeside();

        glRotatef(60.0f, 0.0f, 0.0f, 1.0f);
        drawflakeside();

        glRotatef(60.0f, 0.0f, 0.0f, 1.0f);
        drawflakeside();

        glRotatef(60.0f, 0.0f, 0.0f, 1.0f);
        drawflakeside();
        glPopMatrix();
    }

    void setProperties(float cx, float cy, float sx, float sy, ROTATE_DIRECTION rot_direction) 
    {
        mSx = sx;
        mSy = sy;
    }
};

Flake flakes[5];

void initScene(void) {
    flakes[0].setProperties(0.0, 0.0, 1.0, 1.0, CLOCKWISE);
    flakes[1].setProperties(40.0, 40.0, 0.3, 0.3, ANTICLOCKWISE);
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //draw your scene here
    glColor3f(1, 1, 0);
    flakes[0].draw();
    glColor3f(1, 0, 0);
    flakes[1].draw();

    glutSwapBuffers();
}

void anim(void) {
    glutPostRedisplay();
}

int main(int argc, char**argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(550, 550);
    glutCreateWindow("SnowFlake");

    initScene();

    glutDisplayFunc(display);
    glutIdleFunc(anim);

    glutMainLoop();

    return 0;
}

相关问题