在GLUT中用鼠标绘制一个矩形

时间:2023-02-09 23:39:44

I am fairly new to using GLUT, and I have been attempting to compile a program (which I found here, first response) that uses the mouse to draw a rectangle by recording the starting and ending points of a click-and-drag.

我是使用GLUT的新手,我一直在尝试编译一个程序(我在这里找到,第一个响应),它使用鼠标通过记录点击和拖动的起点和终点来绘制矩形。

As a clean copy/paste, it will compile but not draw anything. It just displays a white screen, even after changing the background color to black (in the setup() function). I've read several sources to verify that this program doesn't miss anything in its draw and reshape functions, and it's all there.

作为一个干净的复制/粘贴,它将编译但不绘制任何东西。即使将背景颜色更改为黑色(在setup()函数中),它也只显示白色屏幕。我已经阅读了几个来源来验证这个程序在绘制和重塑函数中不会遗漏任何内容,而且它就在那里。

I create a window, set the viewport to the window dimensions, and then use the gluOrtho2D function to set the mapping (since the window and viewport are the same dimensions, I set the mapping to the window dimensions). The mouse callback records where I left-click, and where I release left-click, then calls the glutPostRedisplay() function to redraw the window with the new coordinates. After a bit of debugging, I discovered the coordinates are recorded and saved appropriately, and are measured in pixels (x and y are integers between 0 and window dimension), so I should be able to draw a rectangle from one vertex to the other vertex using the coordinates. But, like I said, it only displays a white screen.

我创建一个窗口,将视口设置为窗口尺寸,然后使用gluOrtho2D函数设置映射(因为窗口和视口是相同的尺寸,我将映射设置为窗口尺寸)。鼠标回调记录我左键单击的位置,以及我左键单击的位置,然后调用glutPostRedisplay()函数以使用新坐标重绘窗口。经过一些调试后,我发现坐标被适当地记录和保存,并以像素为单位测量(x和y是0和窗口维度之间的整数),所以我应该能够从一个顶点到另一个顶点绘制一个矩形使用坐标。但是,就像我说的那样,它只显示一个白色的屏幕。

So, is there something wrong with the way I am drawing the rectangle? Am I mapping the window incorrectly? I am seriously lost, and any feedback would be greatly appreciated.

那么,我绘制矩形的方式有问题吗?我是否错误地映射了窗口?我很丢失,任何反馈都会非常感激。

EDIT2: I changed the glutInitDisplayMode from GLUT_SINGLE to GLUT_DOUBLE, and that fixed the whole non-interactive white screen thing. Now it will draw a rectangle with the mouse with a flipped y-coordinate (which I fixed), and it works great now. Thank you very much for the suggestion.

EDIT2:我将glutInitDisplayMode从GLUT_SINGLE更改为GLUT_DOUBLE,并修复了整个非交互式白屏事件。现在它将使用带有翻转y坐标(我已修复)的鼠标绘制一个矩形,现在效果很好。非常感谢你的建议。

Here is my program (EDIT1: added comments):

这是我的程序(EDIT1:添加了评论):

#include <cstdlib>
#include <GL/glut.h>    

using namespace std;

GLsizei width, height;

struct Position
{
    Position() : x(0), y(0) {}
    float x;
    float y;
};

Position start;  // Records left-click location
Position finish; // Records left-click release location

void display()
{
    glClear(GL_COLOR_BUFFER_BIT); // clear window

    glColor3ub(rand()%256, rand()%256, rand()%256); // generates random color
    glBegin(GL_QUADS);
        glVertex2f(start.x,start.y);
        glVertex2f(finish.x,start.y);
        glVertex2f(finish.x,finish.y);
        glVertex2f(start.x,finish.y);
    glEnd();

    glutSwapBuffers(); // display newly drawn image in window
}

void reshape( int w, int h )
{
    glViewport( 0, 0, (GLsizei)w, (GLsizei)h ); // set to size of window
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    gluOrtho2D( 0.0, (float)w, 0.0, (float)h );

    width = w;  // records width globally
    height = h; // records height globally

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
    switch(button)
    {
    case GLUT_LEFT_BUTTON:

        if(state==GLUT_DOWN) 
        {
            start.x = x; //x1
            start.y = y; //y1                          
        }
        if(state==GLUT_UP)
        {
            finish.x = x; //x2
            finish.y = y; //y2
        }
        break;  
        glutPostRedisplay();
    }
}

void motion( int x, int y )
{
    finish.x = x;
    finish.y = y;
    glutPostRedisplay();
}

void setup()
{
    glClearColor(0.0, 0.0, 0.0, 1.0); // *should* display black background
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,100);
    glutCreateWindow("");

    setup();

    // initializing callbacks
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
    return 0;
}

1 个解决方案

#1


3  

As my comment suggested:

正如我的意见所示:

change:

更改:

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

to:

至:

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

#1


3  

As my comment suggested:

正如我的意见所示:

change:

更改:

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

to:

至:

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);