Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

2年前关闭。



Improve this question




当我使用glScale(size/window_size)绘制四面体(window_size = 600x600)并输入size = 600时,它绘制的图片不正确

[1

当我输入
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 ,因此您正在逐​​步更改矩阵。 glScale 将当前矩阵乘以常规缩放矩阵。

参见 glScale :



在执行缩放之前,请使用 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();
}

09-06 15:13