Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Draw GUI and text

  1. #1
    Newbie Newbie
    Join Date
    Jul 2017
    Posts
    1

    Lightbulb Draw GUI and text

    Hey !

    I recently started programming with OpenGL ans i want to draw a "GUI".
    I can draw text and i can draw quads with specific function i created.
    The text is okay, the quad too, but when i draw both, the text no longer appears, as if it were hidden by quad...

    Here is my code :
    Code :
    #include <GL\glew.h>
    #include <GL\freeglut.h>
     
    class Gui
    {
    private:
    	void Texte(int x, int y, char* txt);
    	void drawGuiBackground();
    public:
    	Gui();
    	~Gui();
    	void drawGui();
    };

    Code :
    #include "Gui.h"
    #include <iostream>
     
    void Gui::Texte(int x, int y, char * txt)
    {	
    	glRasterPos2f(x, y);
    	int len = (int)strlen(txt);
    	for (int i = 0; i < len; i++) { 
    		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, txt[i]); 
    	}
    }
     
    void Gui::drawGuiBackground()
    {
    	const double w = glutGet(GLUT_WINDOW_WIDTH);
    	const double h = glutGet(GLUT_WINDOW_HEIGHT);
    	glRasterPos2f(0, 0);
     
    	glBegin(GL_QUADS);
    	glColor3f(1.0f, 0.0f, 0.0);
    	glVertex2f(0.0, 0.0);
    	glVertex2f(w, 0.0);
    	glVertex2f(w, h / 10);
    	glVertex2f(0.0, h / 10);
    	glEnd();
     
    	glBegin(GL_QUADS);
    	glColor3f(0.0f, 1.0f, 0.0);
    	glVertex2f(0.0, 0.0);
    	glVertex2f(w/2, 0.0);
    	glVertex2f(w/2, h / 20);
    	glVertex2f(0.0, h / 20);
    	glEnd();
    }
     
    Gui::Gui()
    {
     
    }
     
     
    Gui::~Gui()
    {
     
    }
     
     
    void Gui::drawGui(){
    	glPushMatrix();
    	glLoadIdentity();
    	glDisable(GL_DEPTH_TEST);
    	glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT), -1.0, 1.0);
     
    	Texte(0, 12, "Hey !");
    	drawGuiBackground();
     
    	glEnable(GL_DEPTH_TEST);
    	glPopMatrix();
    }

    Without quad draw:
    Click image for larger version. 

Name:	txt.jpg 
Views:	36 
Size:	3.7 KB 
ID:	2411

    with quad draw:
    Click image for larger version. 

Name:	blol.jpg 
Views:	38 
Size:	4.6 KB 
ID:	2412

    If you have a solution I'm interested.

    Thanks
    Last edited by Mattous; 07-13-2017 at 07:08 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    418
    Disable depth testing first.
    Then draw your text after drawing your rectangles.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •