我使用以下函数在一个点中绘制一个字符串:
void print(std::string text,int x, int y)
{
glRasterPos2f(x,y);
int len =(int)text.size();
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,text[i]);
}
};
现在我需要绘制字符串而不让它超出边界(矩形) 我该怎么做呢?
编辑:我在OpenGL中寻找模板技术,并以这种方式进行修改:
void print(std::string text,Point point=Point(), Rectangle rect=Rectangle())
{
glRasterPos2f(point.x,point.y);
int len =(int)text.size();
if(!rect.isEmpty())
{
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
glStencilMask(0xFF);
glClear(GL_STENCIL_BUFFER_BIT);
rect.draw(GL_STENCIL);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilMask(0x00);
glStencilFunc(GL_EQUAL, 0, 0xFF);
glStencilFunc(GL_EQUAL, 1, 0xFF);
}
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,text[i]);
}
glDisable(GL_STENCIL_TEST);
};
对于Rectangle和Point,它们只是结构,具有以下变量:
Point: GLfloat x,y,z
Rectangle : Size size, Point origin
Size: GLfloat width,height
代码不起作用,如果我尝试在矩形内绘制一个字符串,它不会留在矩形内。