如何在C 中的OpenGL中通过矩形显示文本



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

#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();
}

您可以告诉我评论,如果您需要询问其他任何问题。我使用CodeBlocks测试此程序。

好吧,一天后我明白了,我很愚蠢:')

#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();
}

相关内容

  • 没有找到相关文章

最新更新