glScaled(size/window_size) 不正确的图像

glScaled(size/window_size) incorrect image

当我使用glScale(size/window_size)画一个四面体(window_size = 600x600)并且输入尺寸=600时,它画错了

[1。

当我输入尺寸 < 600 时,它不绘制任何东西,直到 1200 时,它绘制与较低尺寸相同的图像,在 1200 之后再次绘制任何东西。怎么做正确?

int size_cat;
std::cin >> size_cat;
screen = SDL_CreateWindow("Jack.exe", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_OPENGL);

int size_h = 2 * size_cat;

SDL_GLContext context = SDL_GL_CreateContext(screen);
glClearColor(0, 0, 0, 1);
glRotatef(25, 1, 1, 0);
while (1)
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
                return 0;
            case SDL_KEYDOWN:
                if (event.key.repeat) break;
                switch (event.key.keysym.scancode)
                {
                    case SDL_SCANCODE_1: glColor3b(127, 0, 0); break;
                    case SDL_SCANCODE_2: glColor3b(0, 127, 0); break;
                    case SDL_SCANCODE_3: glColor3b(0, 0, 127); break;
                }
                break;              
        }
    }

    glClear(GL_COLOR_BUFFER_BIT);

    glScalef(size_cat/600, size_cat/600, size_cat/600);

    glBegin(GL_LINES);

    glVertex3f(0.5, 1, -0.5);
    glVertex3f(0.5, 0, -0.5);

    glVertex3f(0.5, 0 ,-0.5);
    glVertex3f(0.5, 0, 0.5);    

    glVertex3f(0.5, 0, 0.5);
    glVertex3f(-0.5, 0, -0.5);

    glVertex3f(-0.5, 0, -0.5);
    glVertex3f(0.5, 0, -0.5);

    glVertex3f(0.5, 1, -0.5);
    glVertex3f(0.5, 0, 0.5);

    glVertex3f(0.5, 1, -0.5);
    glVertex3f(-0.5, 0, -0.5);

由于您使用 glScale in a loop, you are changing your matrix progressively. glScale 将当前矩阵乘以通用缩放矩阵。

glScale:

glScale produces a nonuniform scaling along the x, y, and z axes. The three parameters indicate the desired scale factor along each of the three axes.

The current matrix (see glMatrixMode) is multiplied by this scale matrix, and the product replaces the current matrix.


在进行缩放之前,使用 glLoadIdentity 将当前矩阵替换为单位矩阵:

while (1)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(25, 1, 1, 0);
    glScalef(size_cat/600, size_cat/600, size_cat/600);

    .....
}


或者使用 glPushMatrix and glPopMatrix:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(25, 1, 1, 0);
while (1)
{
    glPushMatrix();
    glScalef(size_cat/600, size_cat/600, size_cat/600);

    .....

    glPopMatrix();
}