2017-02-18 76 views
1

我的代碼看起來像這樣在此代碼中的修改將不勝感激。我試過了,只有矩形看起來不是文字。如何在OpenGL中用C++在矩形上顯示文本?

#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來測試這個程序。

+0

[使用GLUT簡單地打印文本(http://stackoverflow.com/a/20866623/7550472)可能幫幫我。 –

+0

@KaushikNP我仍然無法做到這一點,我在OpenGL有點小菜鳥,如果你能在程序中提出建議,那將會很棒。 –

+0

@АлексейНеудачин我沒有得到它。如果你想在第二個問題,你可以在這裏發送拉請求https://github.com/heysadboy/Advanced-Graphics-and-Animation。謝謝。 –

回答

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(); 
} 
相關問題