Sign in to follow this  
swinchen

GLFT_Font && Frustum :(

Recommended Posts

swinchen    224
Hi All, I have been trying to write font out for a while now and it is driving me crazy. I found this neat little font library called GLFT_Font (http://www.cs.rit.edu/~jpt2433/glftfont/). I got to compile the test fine, but when I try to set up a perspective with glFrustum (indead of glOrtho) it doesnt work anymore. Any ideas? I have underestimated the difficulty in displaying simple test in a GL environment!

Share this post


Link to post
Share on other sites
cozman    583
Hey, I'm the author of GLFT_Font actually.

GLFT_Font (as of 0.2) is only meant to be used in Ortho mode.. are you sure you want to write text in perspective mode? Most of the time when drawing text it can be done in Ortho, it is possible to draw your scene in the glFrustum view and then set the glOrtho view for drawing the font.

If you are simply trying to add text that should always be facing the screen and shouldn't be affected by perspective (I think of it as drawn "on" the screen rather than "in" the scene) setting the mode with glOrtho before you draw text and glFrustum before you draw the 3D stuff is what you should be doing.

I don't really do a lot of stuff in 3D, so maybe someone who does can tell me/you how people usually draw bitmap fonts in glFrustum mode.

Share this post


Link to post
Share on other sites
swinchen    224
Thanks for the response.

I wonder what sort of hit performance takes by switching to Ortho, and then back to Frustum each frame. Hmmm, probably not that much?

Thanks again, I will try giving that a shot.

Share this post


Link to post
Share on other sites
cozman    583
As long as you try to limit the switches, it shouldn't be noticable. If you're doing something like:


glFrustum
// draw some of scene
glOrtho
// draw some text
glFrustum
// draw some more of scene
glOrtho
// draw some more text


you should try to clean things up and organize them, but a few switches aren't going to create a bottleneck, any game with a HUD has at least one

Share this post


Link to post
Share on other sites
swinchen    224
Well lets see... I tried this (I reset all the modelview parameters each time... not sure I have to do this?) It doesnt seem to work.

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

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


#define DELTA 0.001f

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();
}

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

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

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};



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

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
f.beginDraw(5, 5) << j++ << f.endDraw();

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

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
glLightfv(GL_LIGHT1,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT1,GL_POSITION,lightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLineWidth(3.0f);

glTranslatef(0.0f,0.0f,-5.0f);
drawPoint();

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
cozman    583
for future reference if you're posting code either use [ code ] or [ source ] tags.

I played around with your code for a minute and it looks like glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); is the culprit, you need it to draw the font, but when enabled your triangle isn't drawn. After drawing the text, I added
a glDisable(GL_BLEND) and it fixed your problem.



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);




Best of luck, and hope that GLFT_Font works well for you.

Share this post


Link to post
Share on other sites
swinchen    224
Thanks! Worked like a charm.

I wonder though if it is possible to reduce this a little.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(....);
...

glLoadIdentity();
glFrustum(....);
...

glMatrixMode(GL_MODELVIEW);
//Set things related to model view here...

//And then inside my "game" loop...

do
{
glMatrixMode(GL_PROJECTION);
glOrtho(...);

glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
f.beginDraw(5, 5) << j++ << f.endDraw();
glDisable(GL_BLEND);

glMatrixMode(GL_PROJECTION);
glFrustum(...);

glMatrixMode(GL_MODELVIEW);
//do everything else.
} while(...);

This doesnt seem to work. I would like for it to "remember" all my settings for glOrtho, and glFrustum so I don't need to set up _everything_ each time. Is this standard and I am complaining about nothing? Something doesnt seem right with code, almost like it is doing way more than it needs to.

Share this post


Link to post
Share on other sites
swinchen    224
Alright, I am _REALLY_ close to making this work. Only one slight problem: the text shows up as a rectangle.

The triangle renders fine, and the text is trying. I am still doing something incorrectly though.

Which opperations act on the modelview matrix? I don't know if any of the setup in my init_gl() function has to be done after switching to GL_MODELVIEW

Thanks for any help!


#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)
{

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};

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
glLightfv(GL_LIGHT1,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT1,GL_POSITION,lightPosition);
glEnable(GL_LIGHT1);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLineWidth(3.0f);

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);

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

glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this