Jump to content
  • Advertisement
Sign in to follow this  
swinchen

Font rendering with GLFT_Font and GLFW

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am still a bit of a beginner, but I feel this may a more appropriate category. I am trying to write text in the top left corner of the viewport while in glOrtho, and then switch back to a glFrustum and draw the rest of my very basic 3D scene. The 3D scene works fine, but the text shows up as a rectangle in the top left corner. It may have something to do with blending, I am not sure. If you can offer any tips please let me know. Sam.
#include <GL/glfw.h>
#include <GL/gl.h>
#include <cmath>
#include "GLFT_Font.hpp"

const float PI = 3.1415927f;
const int WIDTH = 800;
const int HEIGHT = 600;

void myPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
  GLdouble ymax;

  ymax = zNear * tan(fovy * PI / 360.0f);
  glFrustum(-ymax*aspect, ymax*aspect, -ymax, ymax, zNear, zFar);
}

void drawPoint()
{

  glBegin(GL_TRIANGLES);
  glNormal3f(0.0f,0.44721359549996f,0.89442719099992f);
  glVertex3f(0.0f,1.0f,0.0f);
  glVertex3f(0.5f,0.0f,0.5f);
  glVertex3f(-0.5f,0.0f,0.5f);

  glNormal3f(-0.89442719099992f,0.44721359549996f,0.0f);
  glVertex3f(0.0f,1.0f,0.0f);
  glVertex3f(-0.5f,0.0f,0.5f);
  glVertex3f(-0.5f,0.0f,-0.5f);

  glNormal3f(0.0f, 0.44721359549996f,-0.89442719099992f);
  glVertex3f(0.0f,1.0f,0.0f);
  glVertex3f(-0.5f,0.0f,-0.5f);
  glVertex3f(0.5f,0.0f,-0.5f);

  glNormal3f(0.89442719099992f,0.44721359549996f,0.0f);
  glVertex3f(0.0f,1.0f,0.0f);
  glVertex3f(0.5f,0.0f,-0.5f);
  glVertex3f(0.5f,0.0f,0.5f);
  glEnd();

  glBegin(GL_QUADS);
  glNormal3f(0.0f, -1.0f, 0.0f);
  glVertex3f(0.5f,0.0f,0.5f);
  glVertex3f(0.5f,0.0f,-0.5f);
  glVertex3f(-0.5f,0.0f,-0.5f);
  glVertex3f(-0.5f,0.0f,0.5f);
  glEnd();
}

void init_gl(void)
{


  glEnable(GL_TEXTURE_2D);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glFrontFace(GL_CW);
  glEnable(GL_CULL_FACE);
  glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

  // LIGHTING SETUP
  float matAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f};
  float matDiff[] = {1.0f, 1.0f, 1.0f, 1.0f};
  float ambientLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
  float diffuseLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
  float lightPosition[] = {0.0f, 0.0f, 10.0f, 1.0f};

  glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
  glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);

  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);
  glEnable(GL_COLOR_MATERIAL);
  // END LIGHTING SETUP

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  myPerspective(75.0f, WIDTH / (float)HEIGHT, 1.0f, 100.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void enable2d(void)
{
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0, WIDTH, HEIGHT, 0, -1.0, 1.0);

  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
}

void disable2d(void)
{
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
}

int main(int Arg_N, char ** Arg_V)
{
  glfwInit();
  glfwOpenWindow(WIDTH, HEIGHT, 8, 8, 8, 8, 0, 0, GLFW_WINDOW);

  init_gl();

  GLFT_Font f;
  f.open("cour.ttf",12);

  GLfloat roty = 0;
  GLfloat rotx = 0;
  int j = 0;
  do
  {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
      roty += 0.2;
    if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
      roty -= 0.2;

    if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS)
      rotx += 0.2;
    if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS)
      rotx -= 0.2;

    glTranslatef(0.0f,0.0f,-5.0f);
    glRotatef(roty,0.0f,1.0f,0.0f);
    glRotatef(rotx,1.0f,0.0f,0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    drawPoint();

    enable2d();
    f.beginDraw(0,0) << j++ << f.endDraw();
    disable2d();

    glfwSwapBuffers();
  }
  while ( glfwGetWindowParam(GLFW_OPENED) &&
          glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS);

  glfwCloseWindow();
  glfwTerminate();

  return(0);
}

Share this post


Link to post
Share on other sites
Advertisement
hey, it's me again :)

you have to have blending enabled for the font to draw because the font is drawn as a texture with alpha transparency, if you look at your earlier post you had the correct blending there when the font worked

Share this post


Link to post
Share on other sites
Man you are a life saver! Thank you. I don't entirely understand why I need to enable blending and then disable it. Maybe I can find something about it my OpenGL for Game Programmers book. Here is the working version.


#include <GL/glfw.h>
#include <GL/gl.h>
#include <cmath>
#include "GLFT_Font.hpp"

const float PI = 3.1415927f;
const int WIDTH = 800;
const int HEIGHT = 600;

void myPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble ymax;

ymax = zNear * tan(fovy * PI / 360.0f);
glFrustum(-ymax*aspect, ymax*aspect, -ymax, ymax, zNear, zFar);
}

void drawPoint()
{

glBegin(GL_TRIANGLES);
glNormal3f(0.0f,0.44721359549996f,0.89442719099992f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,0.0f,0.5f);
glVertex3f(-0.5f,0.0f,0.5f);

glNormal3f(-0.89442719099992f,0.44721359549996f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.5f,0.0f,0.5f);
glVertex3f(-0.5f,0.0f,-0.5f);

glNormal3f(0.0f, 0.44721359549996f,-0.89442719099992f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.5f,0.0f,-0.5f);
glVertex3f(0.5f,0.0f,-0.5f);

glNormal3f(0.89442719099992f,0.44721359549996f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,0.0f,-0.5f);
glVertex3f(0.5f,0.0f,0.5f);
glEnd();

glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.5f,0.0f,0.5f);
glVertex3f(0.5f,0.0f,-0.5f);
glVertex3f(-0.5f,0.0f,-0.5f);
glVertex3f(-0.5f,0.0f,0.5f);
glEnd();
}

void init_gl(void)
{
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// LIGHTING SETUP
float matAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f};
float matDiff[] = {1.0f, 1.0f, 1.0f, 1.0f};
float ambientLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
float diffuseLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
float lightPosition[] = {0.0f, 0.0f, 10.0f, 1.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
// END LIGHTING SETUP


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
myPerspective(45.0f, WIDTH / (float)HEIGHT, 1.0f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void inline enable2d(void)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

void inline disable2d(void)
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

int main(int Arg_N, char ** Arg_V)
{
glfwInit();
glfwOpenWindow(WIDTH, HEIGHT, 8, 8, 8, 8, 0, 0, GLFW_WINDOW);

init_gl();

GLFT_Font f;
f.open("cour.ttf",18);

GLfloat roty = 0;
GLfloat rotx = 0;
GLfloat rotz = 0;
int j = 0;
do
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glLoadIdentity();


if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
roty += 0.01;
if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
roty -= 0.01;

if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS)
rotx += 0.01;
if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS)
rotx -= 0.01;

if(glfwGetKey(GLFW_KEY_F1) == GLFW_PRESS)
rotz += 0.01;
if(glfwGetKey(GLFW_KEY_F2) == GLFW_PRESS)
rotz -= 0.01;

glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(180*cos(roty),0.0f,1.0f,0.0f);
glRotatef(180*sin(rotx),1.0f,0.0f,0.0f);
glRotatef(180*cos(rotz),0.0f,0.0f,1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
drawPoint();

enable2d();
glColor3f(1.0f,1.0f,1.0f);
glEnable(GL_BLEND);
f.beginDraw(5,5) << "RotX: " << rotx << ", RotY: " << roty << ", RotZ: " << rotz << f.endDraw();
glDisable(GL_BLEND);
disable2d();

glfwSwapBuffers();
}
while ( glfwGetWindowParam(GLFW_OPENED) &&
glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS);

glfwCloseWindow();
glfwTerminate();

return(0);
}



p.s. Is there a list of available tag's like source or code? thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by swinchen
p.s. Is there a list of available tag's like source or code? thanks.


yes, see the forum FAQ (icon in the top right of the screen below the menu)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!