OpenGL: 2D-Text in 3D-Raum einfügen



  • In vielen 3D-Spiele sieht man unten an der Ecke, wieviel Punkte oder Leben man hat. Mich würde gerne Interessieren, wie man die (2D-)Labels in OpenGL hinzufügt. Ich möchte nämlich so ähnlich wie bei Celestia oder Google Earth auch, an der unteren Ecke eine Information ausgeben. Zum Beispiel mit welche Taste man die Erde dreht oder wie tief hinein bzw hinaus gezoomt wurde. Würde mich freuen, wenn Ihr mir par Codebeispiele posten könntet. Danke!

    Viele Grüße,
    littletux



  • Hi,
    Fonts.
    Tschui



  • Hi, guck dir mal void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); an, damit kannst du eine Projektionsmatrix einstellen bei der du normal in 2D rendern kannst.

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity;
    glOrtho ( 0,WIDTH,0,HEIGHT,-1,1)
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity;
    

    Kannst hier mal vorbei schauen
    http://wiki.delphigl.com/index.php/glOrtho

    Gruß, Daniel_S



  • bool NLFont::m_drawbb = false;
    
    NLFont::NLFont() 
    : m_font(NULL), NLIGameObject()
    {
        m_zorder = 0.99f;
    }
    
    NLFont::NLFont(const char* name, const char* font, int size )
    : m_font(NULL), NLIGameObject()
    {
        m_zorder = 0.99f;
        this->openFont(name, font, size);
    }
    
    NLFont::~NLFont()
    {
       delete m_font;
    }
    
    void NLFont::openFont(const char* name, const char* file, int size)
    {
        m_name = name;
        m_font = new FTTextureFont(file);
        if ( m_font->Error() )
        {
            NLError(std::string("NLFont: Cannot load font '") + file + std::string("'."));
            return;
        }
        m_font->FaceSize(size);
        m_font->UseDisplayList(true);
    }
    
    f32 NLFont::getHeight() const
    {
        if ( m_font )
        {
            return m_font->Ascender();
        }
        return 0;
    }
    
    f32 NLFont::getXAdvance(const std::string& text) const
    {
        if ( m_font )
        {
            return m_font->Advance(text.c_str());
        }
        return 0;
    }
    
    f32 NLFont::getXAdvance(const std::wstring& text) const
    {
        if ( m_font )
        {
            return m_font->Advance(text.c_str());
        }
        return 0;
    }
    
    f32 NLFont::getXAdvance() const 
    {
        if ( m_font )
        {
            if ( m_Wmessage.length() != 0 )
            {
                return m_font->Advance(m_Wmessage.c_str());
            }
            else
            {
                return m_font->Advance(m_message.c_str());
            }
        }
        return 0;
    }
    
    void NLFont::setColor(const NLColor3f& color)
    {
        m_color = color;
    }
    
    void NLFont::renderObject()
    {
        if ( m_font )
        {        
            GLfloat curCol[4];
            GLint   curMatrixMode;
    
            glGetFloatv(GL_CURRENT_COLOR, curCol);
            glGetIntegerv(GL_MATRIX_MODE, &curMatrixMode);
    
            glPushAttrib(GL_COLOR_BUFFER_BIT);
            glPushMatrix();
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
    
            glTranslatef(0,0, m_zorder);        
            glScalef(1,-1,1);
    
            glColor3fv(m_color.data());
            if ( m_message.length() != 0 )
            {
                m_font->Render(m_message.c_str(), -1,FTPoint(this->getPosX(),- this->getPosY() ,0));
            }
            else
            {
                m_font->Render(m_Wmessage.c_str(), -1,FTPoint(this->getPosX(),- this->getPosY() ,0));
            } 
            if ( NLFont::getDrawBoundingBox() )
            {
                this->getBoundingBox().renderObject();
            }
            glPopAttrib();
            glPopMatrix();
            glMatrixMode(curMatrixMode);
            glColor4fv(curCol);
        }
    }
    
    void NLFont::renderObject(const char* msg, const NLVector2f& pos) 
    {
        m_message  = msg;  
        m_position = pos;
    	this->renderObject();
    }
    
    void NLFont::renderObject(const wchar_t* msg, const NLVector2f& pos) 
    {
        m_Wmessage = msg;  
        m_position = pos;
    	this->renderObject();
    }
    
    void NLFont::renderObject(const char* msg, const f32 x, const f32 y)
    {
    	if ( x != -1 )
    	{
    		m_position.setX(x);
    	}
    	if ( x != -1 )
    	{
    		m_position.setY(y);
    	}
    	m_message = std::string(msg);
    	this->renderObject();
    }
    
    void NLFont::renderObject(const wchar_t* msg, const f32 x, const f32 y)
    {
    	if ( x != -1 )
    	{
    		m_position.setX(x);
    	}
    	if ( x != -1 )
    	{
    		m_position.setY(y);
    	}
    	m_Wmessage = msg;
    	return this->renderObject();
    }
    
    NLQuad& NLFont::getBoundingBox() 
    {
        f32 x = this->getXAdvance(m_message);
        f32 y = this->getHeight();
        NLVector2f temp = m_position;
        temp.setY(m_position.y()-y);
    
        m_boundingbox.setZOrder(m_zorder);
        m_boundingbox.create(NLVector2f(x, y), temp, NLVector2f(0,0), m_zorder, "FontBoundingBox");
        m_boundingbox.setColor(NLCOLOR4F_MAGENTA);
        m_boundingbox.setLineDraw(true);
        return m_boundingbox;
    }
    
    NLQuad NLFont::getBoundingBox( const char* text, const NLVector2f& position ) 
    {
        NLQuad bb;
        f32 x = this->getXAdvance(text);
        f32 y = this->getHeight();
        NLVector2f temp = position;
        temp.setY(m_position.y()-y);
    
        bb.setZOrder(m_zorder);
        bb.create(NLVector2f(x, y), temp, NLVector2f(0,0), m_zorder, "FontBoundingBox");
        bb.setColor(NLCOLOR4F_MAGENTA);
        bb.setLineDraw(true);
        return bb;
    }
    

    Ich verwende FTGL, siehe obiger Code.
    Geht sehr gut damit.
    http://img826.imageshack.us/img826/5066/screenshotwa.png
    Alternativ kannst auch CEGUI nehmen, da hast gleich nen GUI Toolkit dabei oder OpenGUI etc etc. Google hilft.
    http://sourceforge.net/projects/ftgl
    rya.



  • @Scorcher24 Ich weiß, meine Antwort kommt etwas spät, aber ich musste diese Woche etwas länger arbeiten. Danke für Deine Hilfe! Du hast Dir wirklich Mühe gegeben, mir ein Beispeil-Code zu posten. Ist wirklich super! Danke! 🙂

    Viele Grüße,
    littletux



  • Verwendest du OpenGL 3 oder höher, dann muss die Textausgabe shaderbasiert erfolgen.
    Vielleicht hilft dir noch dieses Programmbeispiel weiter:

    http://www.spieleprogrammierung.net/2010/03/opengl-3-textausgabe-font-rendering.html


Anmelden zum Antworten