使用 Ortho2D 移动当前图形在 GLUT 中不起作用

Moving current drawings using Ortho2D doesn't work in GLUT

本文关键字:GLUT 不起作用 图形 Ortho2D 移动 使用      更新时间:2023-10-16

我有一个用C++/OpenGL做的迷你项目,它允许用户使用鼠标在屏幕上绘制形状。

现在我想允许用户使用他的键盘左右移动/平移屏幕,假设屏幕上有 4 个形状在不同的地方,我希望它们在按下"A"键时全部向左移动。

我已经设法在

以前的项目中做到了这一点,甚至放大/缩小,但在这个程序中有些东西不起作用,我设法做的最接近的事情是用户在按下"A"键后创建的所有新形状都在新位置而不是他的鼠标所在位置。

这是我的主要,初始化,显示和键盘代码:

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(W_WIDTH, W_HEIGHT);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("OpenGL Excercise");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutPassiveMotionFunc(My_mouse_routine);
    createMenu();
    glutMainLoop();
}

void init()
{
    // Select a clear color
    glClearColor(0.933, 0.933, 0.933, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    gluOrtho2D(0, W_WIDTH, W_HEIGHT, 0);
}
void display() {/* clear all pixels */
    // draw text at the bottom
    char* text_to_draw = "Press right mouse button for menu...";
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, W_WIDTH, 0, W_HEIGHT);
    glColor3f(0.129, 0.129, 0.129);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glRasterPos2i(10, W_HEIGHT - 15);  // move in 10 pixels from the left and bottom edges
    for (int i = 0; i < (int)strlen(text_to_draw); ++i) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text_to_draw[i]);
    }
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);

    glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27:
        exit(0);
    case 'A':
    case 'a':
        glMatrixMode(GL_PROJECTION); //start editing the projection matrix
        glLoadIdentity(); //remove current projection
        glPopMatrix();
        gluOrtho2D(1,0,1,0); //create new one
        glPushMatrix();
        glMatrixMode(GL_MODELVIEW); //back to editing the modelview matrix
        glFlush();
    default:
        break;
    }
    glutPostRedisplay();
}

最后,下面是一个矩形绘图的示例:

GLfloat colors[][3] = { { 0.957f, 0.263f, 0.212f },{ 0.298f, 0.686f, 0.314f },{ 0.129f, 0.588f, 0.953f }, };
static int col = 0;
glColor3f(colors[col][0], colors[col][1], colors[col][2]);
glBegin(GL_POLYGON);
glVertex3f(mouse_x - 50, mouse_y - 50, 0.0);
glVertex3f(mouse_x + 50, mouse_y - 50, 0.0);
glVertex3f(mouse_x + 50, mouse_y + 50, 0.0);
glVertex3f(mouse_x - 50, mouse_y + 50, 0.0);
glEnd(); /* don't wait!!! start processing buffered OpenGL routines */
col += 1;
if (col == sizeof(colors) / sizeof(colors[0])) {
    col = 0;
}
glutPostRedisplay();

我知道在这个网站和数百个其他网站上有很多关于这个问题的问题,我几乎肯定它与矩阵有关,但我不知道我做错了什么。

  1. 为什么要在击键A更改投影矩阵?

    我假设你想使用 WSAD(这对像我这样的老派 OPQAM 人来说非常不舒服)来平移视图。

    第一个问题是您在击键时更改GL_PROJECTION A但在呈现时重置它display()呈现击键代码而不做任何事情。为了解决这个问题,您应该在init()函数中初始化GL_PROJECTION。顺便说一句,您在那里缺少glMatrixMode并将其从display()中删除.

    另一个问题是您在GL_PROJECTION中使用相机/视图矩阵,而不是称为GL_MODELVIEW

    • GL_PROJECTION矩阵滥用。

    当您使用高级照明和雾时,这将导致您出现严重问题(除非您切换到 GLSL)。

    最后,当您通过正交设置视图时,您应该仅平移glTranslate 这至少是我的观点。

  2. 您的场景存储在哪里?

    从您的文本中,我认为这应该是一些简单的矢量绘画,例如能够在场景中添加/绘制形状的编辑器。因此,您需要某种能够存储场景所有信息的列表,例如:

    • 对象的类型
    • 控制点
    • 颜色

    在您的代码中没有看到此类信息,相反,我看到直接使用鼠标坐标,这对于预览最后添加的点很好,但对于更复杂的形状(如线条)会失败。

    下面来看看:

    • C++中的简单拖放示例

    对于如何完成的一些想法。