【发布时间】:2016-02-21 09:21:31
【问题描述】:
我在像这张图片这样的框架上显示字符时遇到问题。在这张图片中画了一个红色的圆圈,上面写着一些字符。
我的代码工作正常,除了字符显示功能。我不知道我在glRotatefglTranslatefglScalef这些函数中给出了什么坐标来根据图片显示字符。
这是代码:
using namespace std;
float x = 1, y = 1;
void init(void) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void drawCircle(float pointX, float pointY, float Radius, int PointsDrawn)
{
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i < PointsDrawn; i++)
{
float theta = i * (2.0f * (float)M_PI / PointsDrawn);
float x = Radius * cos(theta);
float y = Radius * sin(theta);
glVertex2f(x + pointX, y + pointY);
}
glEnd();
}
void print(float x, float y, char* text)
{
glPushMatrix();
glScalef(5,5,2);
glTranslatef(x, y, 0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, *text);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
drawCircle(0.0f, 0.0f, 0.80f, 360);
glColor3f(1.0f,1.0f,1.0f);
print(-0.5f, 0.5f, "D");
glFlush();
}
void reshapeFunc(int width, int height) {
if (height == 0) {
height = 1;
}
else {
height = height;
}
float aspectRatio = (GLfloat)width / (GLfloat)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width <= height)
glOrtho(-x, x, -y / aspectRatio, y / aspectRatio, -1.0, 1.0);
else
glOrtho(-x*aspectRatio, x*aspectRatio, -y, y, -1.0, 1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(590, 590);
glutInitWindowPosition(50, 50);
glutCreateWindow("Frame");
init();
glutDisplayFunc(&display);
glutReshapeFunc(&reshapeFunc);
glutMainLoop();
return EXIT_SUCCESS;
}
【问题讨论】: