【问题标题】:Glut characters display function issue opengl过剩字符显示功能问题opengl
【发布时间】: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;


}

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:

    首先你的问题没有很好地定义。我假设你想使用你的代码做第一张图片。

    在您的代码中,您在同一位置绘制“D”和“E”并缩放输出。要实现最终输出,您必须稍微平移和旋转所有字符。您可以通过稍微修改当前的打印功能来做到这一点。

    先把print里面1/152的scalef值改成1/552。添加一个带有参数的rotatef。然后使用相关的位置和旋转参数调用打印函数。有关用法,请参见函数的opengl文档。

    编辑:

    这是我的代码:

    void print(float x, float y, float z,char* text) { 
        glPushMatrix();
        glTranslatef(x, y, 0);
        glScalef(1/552.0,1/552.0,1);
        glRotatef(z,0,0,1);
        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);
        char *ch="DEVELOP";
        for (int i = 0; i <7; ++i)
        {
            float rad=M_PI/180;
            double ang=135-15*i;
            double ang_letter=45-15*i;
            print(0.6*cos(ang*rad), 0.6*sin(ang*rad),ang_letter,ch+i);
        }
        glFlush();
    }
    

    【讨论】:

    • 我清楚地定义了问题我知道所有这些函数 Rotatef 和 scalef 但不知道我在这些函数中给出的坐标
    • @JohnDoe 你画了一个半径为 .80 的圆。所以很明显 translate 参数的范围是 -.80 到 +.80 。如果要将其保持在上半部分,则 y 坐标应为 0 到 0.8。根据第一张图片尝试 x 与 [-.5,+.5] 和 y 与 [+.5,+.7]。
    • 我给出了这些坐标,但什么都没有显示你可以用代码示例glColor3f(1.0f,1.0f,1.0f) print(-0.5f, 0.5f, "D")
    • @JohnDoe 您可能删除了打印中的 scalef。这就是为什么你看不到字母太大的原因。试试 552 的比例。
    • void print(float x, float y, char* text) { glPushMatrix(); glTranslatef(x, y, 0); glScalef(1/552.0,1/552.0,1); glutStrokeCharacter(GLUT_STROKE_ROMAN, *text); glPopMatrix(); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多