在这里我有两个点,其中一个是固定的,另一个是通过鼠标单击更改的这红色
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
//Include OpenGL header files, so that we can use OpenGL
#include <glut.h>
using namespace std;
void init();
void display();
float a = 200.0, b= 200.0;
void setPoint(float x, float y){
glColor3f(1.0, 0.0, 0.0); //drawing object
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
int m=50;
int n=50;
void setPoint2(int m,int n){
glColor3f(1.0, 1.0, 0.0); //drawing object
glBegin(GL_POINTS);
glVertex2f(m,n);
glEnd();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 400.0, 0.0, 300.0);
}
void display(){
glClear( GL_COLOR_BUFFER_BIT); //defult point
glPointSize(10.0);
setPoint(a,b);
setPoint2(m,n);
glFlush();
}
void update(){
while(a!=m || b!=n){
if(a>m&&b>n)
{
m++;
n++;
setPoint2(m,n);
}
else if(a>m&&b<n)
{
m++;
n--;
setPoint2(m,n);
}
else if(a<m&&b>n)
{
m--;
n++;
setPoint2(m,n);
}
else if(a<m&&b<n)
{
m--;
n--;
setPoint2(m,n);
}
// glTranslatef(m,n,0);
}
}
void mouse(int mouse, int state, int x, int y){
switch(mouse){
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN){
cout << x << " -> " << y << endl;
/*a=x;
b=y;*/
a = x/1.0;
b = 300.0 - (y/1.0);
glutPostRedisplay();
}
break;
}
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(400,300);
glutInitWindowPosition(200,100);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
//glutTimerFunc(25,update, 0); //Add a timer
glutMainLoop();
return 0;
}
在调试红点(变量一)时,鼠标单击固定点(黄色)的问题,它没有像我想要的那样移动到红色