LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Ruslan1994 on October 23, 2014, 18:29:14

Title: Print text with gluPerspective
Post by: Ruslan1994 on October 23, 2014, 18:29:14
I need to print text on the screen with some parameters, but I use gluPerspective. In slick-util tutoial in LWJGL documentation used glOrtho. How can I do it?
Title: Re: Print text with gluPerspective
Post by: Daslee on October 24, 2014, 08:03:39
Set gluPerspective and draw 3d things and after done rendering 3d, set glOrtho and draw 2d things such as text.
Title: Re: Print text with gluPerspective
Post by: Ruslan1994 on November 03, 2014, 15:17:07
Quote from: Daslee on October 24, 2014, 08:03:39
Set gluPerspective and draw 3d things and after done rendering 3d, set glOrtho and draw 2d things such as text.
I did it, but I see this
private UnicodeFont onScreen;
private boolean running = true;
private float rotation = 10.0f;

public Game() {
       initDisplay();
       initFonts();

       while (running) {
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

           init3D();
           test1();
           
           init2D();
           onScreen.drawString(0.0f, 0.0f, Float.toString(rotation));
           
           if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
               running = false;
           }
           
           Display.update();
           Display.sync(60);
       }
       
       Display.destroy();
       System.exit(0);
   }
   
   private void test1() {
       glPushMatrix();
       glLoadIdentity();
       glTranslatef(0.0f, 0.0f, -5.0f);
       glRotatef(rotation, 1.0f, 1.0f, 0.0f);
       glBegin(GL_QUADS);
           // top
           glColor3f(0.0f, 1.0f, 0.0f);    // blue
           glVertex3f(1.0f, 1.0f, -1.0f);
           glVertex3f(-1.0f, 1.0f, -1.0f);
           glVertex3f(-1.0f, 1.0f, 1.0f);
           glVertex3f(1.0f, 1.0f, 1.0f);
           // down
           glVertex3f(1.0f, -1.0f, 1.0f);
           glVertex3f(-1.0f, -1.0f, 1.0f);
           glVertex3f(-1.0f, -1.0f, -1.0f);
           glVertex3f(1.0f, -1.0f, -1.0f);
           // front
           glColor3f(1.0f, 1.0f, 0.0f);    // yellow
           glVertex3f(1.0f, 1.0f, 1.0f);
           glVertex3f(-1.0f, 1.0f, 1.0f);
           glVertex3f(-1.0f, -1.0f, 1.0f);
           glVertex3f(1.0f, -1.0f, 1.0f);
           // back
           glVertex3f(1.0f, -1.0f, -1.0f);
           glVertex3f(-1.0f, -1.0f, -1.0f);
           glVertex3f(-1.0f, 1.0f, -1.0f);
           glVertex3f(1.0f, 1.0f, -1.0f);
           // left
           glColor3f(1.0f, 0.0f, 0.0f);    // red
           glVertex3f(-1.0f, 1.0f, 1.0f);
           glVertex3f(-1.0f, 1.0f, -1.0f);
           glVertex3f(-1.0f, -1.0f, -1.0f);
           glVertex3f(-1.0f, -1.0f, 1.0f);
           // right
           glVertex3f(1.0f, 1.0f, -1.0f);
           glVertex3f(1.0f, 1.0f, 1.0f);
           glVertex3f(1.0f, -1.0f, 1.0f);
           glVertex3f(1.0f, -1.0f, -1.0f);
       glEnd();
       rotation += 0.5f;
       glPopMatrix();
   }
   
   private void initDisplay() {
       try {
           Display.setDisplayMode(new DisplayMode(960, 640));
           Display.setTitle("test");
           Display.setResizable(false);
           Display.create();
       }
       catch (LWJGLException e) {
           e.printStackTrace();
           Display.destroy();
           System.exit(0);
       }
       
       Mouse.setGrabbed(true);
   }
   
   private void initFonts() {
       Font awtFont = new Font("Consolas", Font.BOLD, 16);
       onScreen = new UnicodeFont(awtFont);
       onScreen.getEffects().add(new ColorEffect(java.awt.Color.white));
       onScreen.addAsciiGlyphs();
       try {
           onScreen.loadGlyphs();
       } catch (SlickException e) {
           e.printStackTrace();
       }
   }
   
   private void init2D() {
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluOrtho2D(0.0f, (float) Display.getWidth(), (float) Display.getHeight(), 0.0f);
       glMatrixMode(GL_MODELVIEW);
       glDisable(GL_DEPTH_TEST);
   }
   
   private void init3D() {
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective(45.0f, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, 100.0f);
       glMatrixMode(GL_MODELVIEW);
       glEnable(GL_DEPTH_TEST);
       glDepthFunc(GL_LEQUAL);
   }
Title: Re: Print text with gluPerspective
Post by: Daslee on November 03, 2014, 17:00:14
You should enable blending, so font texture will have transparency. Add these two lines after initializing display:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Title: Re: Print text with gluPerspective
Post by: Ruslan1994 on November 03, 2014, 17:47:11
Quote from: Daslee on November 03, 2014, 17:00:14
You should enable blending, so font texture will have transparency. Add these two lines after initializing display:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Thanks, text is looking nice now, but what about cube? Can you explain me why it so strange?
Title: Re: Print text with gluPerspective
Post by: Cornix on November 03, 2014, 18:06:23
Is the cube supposed to be textured? If it is you probably forgot to re-bind the texture of the cube.

If the cube is not supposed to be textured you forgot to unbind the font texture or to disable texturing while drawing the cube.
Title: Re: Print text with gluPerspective
Post by: Daslee on November 03, 2014, 19:46:13
Quote from: Ruslan1994 on November 03, 2014, 17:47:11
Quote from: Daslee on November 03, 2014, 17:00:14
You should enable blending, so font texture will have transparency. Add these two lines after initializing display:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Thanks, text is looking nice now, but what about cube? Can you explain me why it so strange?
Forgot about the cube, but Cornix already answered it. Use glBindTexture(GL_TEXTURE_2D, 0) after drawing the text, or bind another texture that you need.
Title: Re: Print text with gluPerspective
Post by: Ruslan1994 on November 04, 2014, 14:22:31
Thank you, guys, you are amazing :)