我正在编写一段代码,当用户单击屏幕时,它会标记一个点,当他继续单击屏幕上的其他点时,它将用线连接这些点。
我不知道为什么,但我正在使用两种不同的屏幕计划。当我用鼠标单击时,它会在屏幕上标记一个点,只有在单击鼠标左键时我才能看到该点,而当未单击该按钮时,我会看到一个干净的屏幕。
我的代码标记点并将它们与线链接:
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/