c - OpenGL中鼠标点击改变屏幕面

标签 c opengl mouseevent screen onmouseclick

我正在编写一段代码,当用户单击屏幕时,它会标记一个点,当他继续单击屏幕上的其他点时,它将用线连接这些点。

我不知道为什么,但我正在使用两种不同的屏幕计划。当我用鼠标单击时,它会在屏幕上标记一个点,只有在单击鼠标左键时我才能看到该点,而当未单击该按钮时,我会看到一个干净的屏幕。

我的代码标记点并将它们与线链接:

void exerciseThree(int x, int y){
 write("Exercise Three", -5, 18);

 float cx = 0, cy = 0;

 if(x != 0 && y != 0 && isFinished == false){
     glPushMatrix(); 
        glPointSize(6.0f);
        //Draw the points
        glBegin(GL_POINTS);
            cx = ((x * (maxx - minx)) / width) + minx;
            cy = (((height - y) * (maxy - miny)) / height) + miny;
            glVertex2f(cx , cy);
        glEnd();

        if(lastCx != 0 && lastCy != 0){
            glBegin(GL_LINE_STRIP);
            glVertex2f(lastCx , lastCy);
            glVertex2f(cx , cy);
            glEnd();      
        }

        lastCx = cx;
        lastCy = cy;    
     glPopMatrix();
 }

 write("Press 0 (zero) to come back.", -10, -18);
}

鼠标功能:

void mouse(int button, int state, int x, int y){
 switch(button){
       case GLUT_LEFT_BUTTON:
            if( option == 3){
                 exerciseThree(x, y);
                 projecao();
                 glutSwapBuffers();
            }

       break;         
 }    
}

我知道我应该处理鼠标的 GLUT_DOWN 和 GLUT_UP ,但是是否存在一种仅使用一个屏幕面的工作方式?

最佳答案

当您单击时,您只能在屏幕上看到某些内容,因为这是您绘制和更新缓冲区的唯一时间。您应该只在单击鼠标时更新 x/y 坐标列表。但是,您应该每次在主循环中绘制点并调用 glutSwapBuffers() ,以便无论按下按钮如何它们都始终显示在屏幕上。

流程应该类似于以下代码:

ArrayOfPoints[];

while(Running)
{
    CheckForMouseInput(); // Update array of points to draw if pressed

    DrawPoints(); // Use same code draw code from exerciseThree()

    glutSwapBuffers(); // Update video buffer
}

关于c - OpenGL中鼠标点击改变屏幕面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785965/

相关文章:

c - 'fgets' 在 'printf' 之后运行

c - C中全局变量的操作

angularjs - 如何正确使用 ngMousedown?

从 Windows 服务调用批处理文件

c++ - 优化数学表达式

cocoa - 当用户将窗口移动到不同的显示器时,OpenGL 上下文停止工作

c++ - openGL glx.h 缺少 xcode 4 mac lion 形式

qt - 使用 QPainter 时对象是透明的

java - 处理:如何阻止所有监听器捕获鼠标事件

java - Swing 持久弹出窗口