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

标签 c++ opengl coordinate-transformation

<分区>

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

[ Output 1 .

当我输入 size < 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在一个循环中,你正在逐步改变你的矩阵。 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();
}

关于c++ - glScaled(size/window_size) 不正确的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899655/

相关文章:

c++ - 从复合键创建 C++ 映射

c++ - 带有无符号tickCount/Wrapping的耗时

java - Java围绕另一个点旋转一个点

c++ - 在 C++ 中预见 std::bad_alloc

c++ - C++20 模块实现分区的 hpp/cpp 拆分

c++ - 链接问题 C++、glew 和 SDL

opengl - 如何在 Clarion 中正确传递数组地址?

opengl - 将 2D 图像堆栈转换为 3D 图像,体积渲染

python-2.7 - 在 Python 中将纬度、经度和海拔高度转换为本地 ENU 坐标

java - 如何使用 Canvas 进行缩放