如何在C ++中用OpenGL在矩形上显示文本?

时间:2017-02-18 18:01:19

标签: c++ opengl graphics

我的代码看起来像这样此代码中的修改将不胜感激。我试过,只有矩形出现不是文字。

#include <GL/glut.h>
#include<bits/stdc++.h>

using namespace std;

void init2D(float r, float g, float b)
{
    glClearColor(r, g, b, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}

void RenderToDisplay()
{
    int l,lenghOfQuote, i;
    char str[80];
    strcpy(str,"Have courage and be kind");
    cout<<str;
    lenghOfQuote = (int)strlen(str);

    for (i = 0; i < lenghOfQuote; i++)
    {
        glColor3f(1.0, 0.0, 0.0);
        glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
    }

}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POLYGON);

    glVertex3f(150.0f, 200.0f, 0.0f);
    glColor3f(0.940, 0.37, 0.47);
    glVertex3f(450.0f, 200.0f, 0.0f);
    glColor3f(0.940, 0.37, 0.47);
    glVertex3f(450.0f, 400.0f, 0.0f);
    glColor3f(0.69, 0.27, 0.57);
    glVertex3f(150.0f, 400.0f, 0.0f);
    glColor3f(0.69, 0.27, 0.57);

    glEnd();

    RenderToDisplay();

    glFlush();

}

int main(int argc, char *argv[])
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(0,0);

    glLineWidth(3);

    glutCreateWindow("Assignment Q2");
    init2D(0.0, 0.0, 0.0);
    glutDisplayFunc(display);
    glutMainLoop();

}

如果您需要提出其他任何问题,可以告诉我评论。我使用代码块来测试这个程序。

1 个答案:

答案 0 :(得分:0)

好了一天后我得到了它,我真是太笨了:')

#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void init2D(float r, float g, float b)
{
    glClearColor(r, g, b, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}

void rectangle()
{
    glBegin(GL_POLYGON);
    glColor3f(0.4,0,0.8);
    glVertex3f(150.0f, 200.0f, 0.0f);
    glColor3f(0.4,0,0.8);
    glVertex3f(450.0f, 200.0f, 0.0f);
    glColor3f(0.6,0,0.6);
    glVertex3f(450.0f, 400.0f, 0.0f);
    glColor3f(0.6,0,0.6);
    glVertex3f(150.0f, 400.0f, 0.0f);
    glEnd();
}

void text()
{
    char menu[80];
    strcpy(menu,"Have courage and be kind");
    int len;
    len = strlen(menu);

    glColor3f(1,1,1);

    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glLoadIdentity();

    gluOrtho2D( 0, 600, 0, 600 );

    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();

    glLoadIdentity();

    glRasterPos2i(190, 300);


    for ( int i = 0; i < len; ++i )
    {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, menu[i]);
    }

    glPopMatrix();

    glMatrixMode( GL_PROJECTION );
    glPopMatrix();
    glMatrixMode( GL_MODELVIEW );
}

void display()
{

    glClear(GL_COLOR_BUFFER_BIT);

    rectangle();
    text();

    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowPosition(0, 0);
    glutInitWindowSize(600, 600);
    glutCreateWindow("Assignment 1 Question 2");
    init2D(0.0f, 0.0f, 0.0f);
    glutDisplayFunc(display);

    glutMainLoop();
}