【问题标题】:Drawing a polygon when the mouse is clicked单击鼠标时绘制多边形
【发布时间】:2012-04-22 08:21:03
【问题描述】:

考虑这段代码:

#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

double width=600;
double height=600;

void processMouse(int button, int state, int x, int y)
{
    glColor4f(1.0,0.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glFlush();
}

static void render()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glutMouseFunc(processMouse);
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);                                         
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
    glutInitWindowSize(width, height);
    glutCreateWindow("Board");  
    glutDisplayFunc(render);
    glutMainLoop();
}

render函数执行完毕,每次点击,都要启动函数processMouse。 因此,如果单击鼠标,所有窗口都应变为红色并显示说明:

    glColor4f(1.0,0.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glFlush();

但是当我单击鼠标时,我注意到一个奇怪的行为:只有窗口的一部分被着色,左下角的部分(而不是整个屏幕)。 窗口保持这种状态,直到我打开一个谷歌浏览器窗口。如果我打开一个谷歌浏览器(或其他图形应用程序),所有窗口都变成红色。 为什么这个?我对更复杂的程序也有问题,似乎有时 glVertex 指令被忽略了。如果我尝试使用 fprintf 调试程序,似乎一切正常,一切似乎都如预期的那样(例如,我试图打印鼠标坐标在 processMouse 函数中,它们没问题),除了我画的东西被忽略了。

编辑: 我已经修改了这段代码,但它仍然有同样的问题:

#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

double width=600;
double height=600;
bool down=false;;

// http://elleestcrimi.me/2010/10/06/mouseevents-opengl/


static void render()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    if(down)
    {
        glColor4f(1.0,0.0,0.0,0.0);
        glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
        glEnd();
        glFlush();
    }
}

void processMouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN)
    {
        down=true;
        glutPostRedisplay();
    }
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);                                         
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
glutInitWindowSize(width, height);
glutCreateWindow("Board"); 
glutMouseFunc(processMouse);
glutDisplayFunc(render);
glutMainLoop();
}

仍然只有一部分屏幕变红。

PS:使用 glutSwapBuffers() 解决,谢谢。

【问题讨论】:

  • 我在您的代码中的任何地方都找不到down=false。这意味着立即绘制完全一次
  • 在第一个声明中:bool down = false,是的,目标是只做一次。

标签: c opengl glut


【解决方案1】:

当您在 GLUT 中使用双缓冲时,您需要调用 glutSwapBuffers() 来查看绘制结果。

将此添加到您的 render() 函数的末尾,它将正常工作。

【讨论】:

  • 我已经修改了代码(见编辑),但我仍然有同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多