0

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

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

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

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,但是是否存在一种只有一个屏幕面的工作方式?

4

1 回答 1

1

当您单击时,您只会在屏幕上看到一些东西,因为这是您绘制和更新缓冲区的唯一时间。单击鼠标时,您应该只更新 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
}
于 2015-04-22T03:52:08.570 回答