glScaled(size / window_size)图像不正确

时间:2017-10-23 22:50:43

标签: c++ opengl coordinate-transformation

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

[Output 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);

1 个答案:

答案 0 :(得分:0)

由于您在循环中使用glScale,因此您将逐步更改矩阵。 glScale将当前矩阵乘以一般缩放矩阵。

请参阅glScale

  

glScale会在xyz轴上产生不均匀的缩放。这三个参数表示沿三个轴中每个轴的所需比例因子。

     

当前矩阵(参见glMatrixMode)乘以此比例矩阵,产品将替换当前矩阵。


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