我试图在OpenGL的画布上打印一行包含变量和点的文本我的代码如下:
void display()
{
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
char string[50];
sprintf(string, "Base Rotation: %d", numVertices);
renderMyText(-0.4, 0.35, string);
glPointSize(20);
glBegin(GL_POINTS);
glVertex2f(characterX, characterY);
dx = vertices[numVertices-1].x-ox;
dy = vertices[numVertices-1].y-oy;
dt = glutGet(GLUT_ELAPSED_TIME);
characterX = ox + dx / sqrt(dx*dx+dy*dy) * Velocity * dt;
characterY = oy + dy / sqrt(dx*dx+dy*dy) * Velocity * dt;
printf("%f %f", characterX, characterY);
glEnd();
glFlush();
}
我使用了一种不同的方法,当鼠标移动时更新点的位置这段代码运行良好,方块更新了位置并完美移动,直到我添加了文本行。
现在的情况是,一旦我启动程序,方块和文本就会出现,但一旦我在窗口中移动鼠标,方块就会消失,只剩下文本,我希望他们两个留在窗口中有人看到什么不对吗?
最佳答案
我解决了这个问题,所以我觉得应该加上解决方案:
我是以一种错误的方式处理这个问题的,我应该用一种空闲的方法更新我的坐标值,比如:
void idle()
{
//dx is last mouse x - last box x
dx = vertices[numVertices-1].x-ox;
//dy is last mouse y - last box y
dy = vertices[numVertices-1].y-oy;
dt = 50;
//dt helps to control the chasing charcters speed
characterX = ox + dx / sqrt(dx*dx+dy*dy) * Velocity * dt;
characterY = oy + dy / sqrt(dx*dx+dy*dy) * Velocity * dt;
//equations to move the character after the cursor by moving it along the slope of the line between the two points
ox = characterX;
oy = characterY;
//update object x and y for next calculation
if((numVertices > 5) && characterX >= vertices[numVertices-1].x - 1 && characterX <= vertices[numVertices-1].x + 1 && characterY >= vertices[numVertices-1].y -1 && characterY <= vertices[numVertices-1].y + 1) {
endGame = true;
//vertices over 5, so that we don't accidentially die when we start, this collision detection code works on a threshold of contact of one
//between the cursor and object on the X and Y
}
glutPostRedisplay();
}
然后glutPostRedisplay调用display方法,在这里我使用坐标计算来更改屏幕上点的位置:
void display() {
glColor3f(0,255,0); //set the in game text to green
if(endGame == true) {
glColor3f(255,0,0);
//if the game is over set the text to red
}
glClear (GL_COLOR_BUFFER_BIT);
glPointSize(20);
glBegin(GL_POINTS);
glVertex2f(characterX, characterY);
glEnd();
glFlush();
glutSwapBuffers();
}
关于c - 在窗口OpenGL中一起打印Variable和Polygon,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13326913/