Troubles with FTGL and OpenGL

Started by
7 comments, last by OneMoreToGo 17 years, 9 months ago
I am trying to use FTGL with OpenGL, to do some simple font testing. (I'm using SDL as my context.) Unfortunatly, when I compile and run, I see no text. I'm doing everything right according to the FTGL FAQ and readme. I've also tried multiple differente methods of glOrtho'ing, different MaxtrixMode and stuff. Here's the source:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include "SDL.h" #include "OpenGL/gl.h" #include "OpenGL/glu.h" #include "IL/il.h" #include "IL/ilu.h" #include "IL/ilut.h" #include "FTGL_all.h" int timeToQuit = 0; FTGLBitmapFont font("/System/Library/Fonts/Geneva.dfont"); /* Set up SDL, OpenGL & DevIL video stuff. */ void setupVideo(int width_sv, int height_sv, int bpp_sv, int fullscreen_sv) { /* SDL. */ SDL_Init(SDL_INIT_VIDEO); if (fullscreen_sv > 0) { SDL_SetVideoMode(width_sv, height_sv, bpp_sv, SDL_OPENGL | SDL_HWSURFACE | SDL_FULLSCREEN); } else { SDL_SetVideoMode(width_sv, height_sv, bpp_sv, SDL_OPENGL | SDL_HWSURFACE); } /* OpenGL. */ glViewport(0, 0, width_sv, height_sv); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, width_sv, height_sv, 0.0); glMatrixMode(GL_MODELVIEW); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(0.0f); /* DevIL. */ ilInit(); iluInit(); ilutInit(); ilutRenderer(ILUT_OPENGL); } /* Drawing function. */ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); font.Render("Hello, World!"); glFlush(); SDL_GL_SwapBuffers(); } int main(int argc, char *argv[]) { setupVideo(320, 240, 32, 0); //glMatrixMode(GL_PROJECTION); //glLoadIdentity(); //gluOrtho2D(0.0, 1.0, 1.0, 0.0); //glTranslatef(0.5, 0.5, 0.5); font.FaceSize(14); while (timeToQuit != 1) { display(); } return(0); } The matrix block in main is where I try different modes and stuff. As I understand it, GL_MODELVIEW is for drawing models and stuff, and projection is where I'd manage my "camera", this is correct, no? And does glTranslatef move the position of where things will be drawn, or move the viewport through 3D space? Sorry, I understand this, but only vaguely. I promise, I did read the chapter in the red book about this, but at the end I still wasn't sure what was going on, in a sense. Thanks!
Advertisement
GL_PROJECTION is to set the projection matrix so you would call that matrix and then call gluPerspective or Ortho or glFrustum() ect...


For your glTranslatef() that will move to the point you want and then make that point the current drawing location for your model, texture, whatever. It affects all matrices. So if you are drawing many objects you will need to look into

glPushMatrix()
and
glPopMatrix()

think of them as a way to reset the modelview without resetting it all to zero and losing your positions for all the objects you already have drawn. As for FTGL I can't comment I haven't used it, maybe someone else can help you who has...
Ok, hang on. I'm still not quite sure I get it. o_0

When drawing a model, of say... a tree in a game It would be sorta like this, no?

glMatrixMode(GL_MODELVIEW);
glTranslatef(treeX, treeY, treeZ);
drawMyTree();

But if I then went to GL_PROJECTION (where I would put my "camera" stuff, as I understand it), the coords would still be loaded onto glTranslate, the ones I just setup, is that what you mean?

But if I say wanted to do multiple models, I could just do this, no?

glMatrixMode(GL_MODELVIEW);
glTranslatef(model1x, model1y, model1z);
drawModel1();
glTranslatef(model2x, model2y, model2z);
drawModel2();

And then so on and so forth, correct?


Anyway, related to FTGL, I inserted an SDL_delay command into my display loop, and now I see my font, rendered on the screen. Then it loops, and draws it again, but this time... to the right more. Is FTGL using my translate's, or just moving right, assuming that I'm just adding more words on, like in standard writing?

Thanks!
When you call your camera code it is done in the modelview also. Now the code you posted about the models and translatef is correct but like I said you are going to end up wanting to use glPushMatrix() and glPopMatrix()...

glMatrixMode(GL_MODELVIEW);//reset viewport to our original viewport sizeglViewport(0, 0, gWidth, gHeight);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();	g_Camera.LookAt();glPushMatrix();glTranslatef(model1x, model1y, model1z);drawModel1();glPopMatrix();glPushMatrix();glTranslatef(model2x, model2y, model2z);drawModel2();glPopMatrix();


HTH
I'm pretty sure FTGLBitmapFont needs to use some OpenGL functions in its constructor. As of right now it is being created before you have a valid OpenGL context. OpenGL calls are ignored if there is no valid context. - Nevermind, it's done in FaceSize.

Also, you will need to position it with glRasterPos* since it is a bitmap font.

EDIT:
Quote:Original post by OneMoreToGo
Anyway, related to FTGL, I inserted an SDL_delay command into my display loop, and now I see my font, rendered on the screen. Then it loops, and draws it again, but this time... to the right more. Is FTGL using my translate's, or just moving right, assuming that I'm just adding more words on, like in standard writing?
Whoops, I missed that the first time. Ignore the part above about creating the font. Using glRasterPos* still stands however. The reason you see the font moving to the right is because when doing any bitmap drawing in OpenGL it moves the raster position to the end of what you drew, so each drawn character moves the raster position to the correct spot for the next character. This means the raster position will be at the end of the string you previously drew unless you set it to something else.
Oh, ok. Thanks! I still have a lot to learn about the various positioning and matrix systems of OpenGL. :)

One question left:

So, translatef moves the paintbrush (in a sense), and the next drawn thing will be "painted" there. glRasterpos does the same but for another type of drawing.

No, I'm pretty sure the red book shows glTranslateF being used to move the "camera" around..., is that in the GL_PROJECTION matrix? Cause right now, that's where I do all my drawing... as that's what the examples on the web seem to always demonstrate. Is that sufficient, or shall we say... wise, for simple 2D scrollers and stuff? It seems to work.

Thanks!
Oh, ok. Thanks! I still have a lot to learn about the various positioning and matrix systems of OpenGL. :)

One question left:

So, translatef moves the paintbrush (in a sense), and the next drawn thing will be "painted" there. glRasterpos does the same but for another type of drawing.

No, I'm pretty sure the red book shows glTranslateF being used to move the "camera" around..., is that in the GL_PROJECTION matrix? Cause right now, that's where I do all my drawing... as that's what the examples on the web seem to always demonstrate. Is that sufficient, or shall we say... wise, for simple 2D scrollers and stuff? It seems to work.

Thanks!
Quote:Original post by OneMoreToGo
...So, translatef moves the paintbrush (in a sense), and the next drawn thing will be "painted" there. glRasterpos does the same but for another type of drawing...
All glTranslate* does is multiply the current matrix by a translation matrix. This is almost always used on only the modelview matrix. Vertices are transformed by the modelview matrix when they are sent to OpenGL, this has the effect of translating an object by the amount specified (or translating the camera by the negative amount specified, see below).

glRasterPos* sets the raster position. The raster position is treated exactly like a vertex so you can think of it as glVertex* only for the bitmap and pixel drawing operations (ie: glDrawPixels) instead of geometry rendering. Since it is treated exactly like a vertex (it is transformed, clipped, lit, textured, etc the same way) there are some things to watch out for that may not be obvious. Read through "Avoiding 16 Common OpenGL Pitfalls" to learn about some of them, and other common mistakes.
Quote:Original post by OneMoreToGo
...No, I'm pretty sure the red book shows glTranslateF being used to move the "camera" around..., is that in the GL_PROJECTION matrix? Cause right now, that's where I do all my drawing... as that's what the examples on the web seem to always demonstrate. Is that sufficient, or shall we say... wise, for simple 2D scrollers and stuff? It seems to work...
This is another common mistake. The projection matrix is meant just for projection matrices. That means you should only be using glFrustum/gluPerspective/glOrtho/gluOrtho2D on the projection matrix unless you know exactly what you're doing (custom projection matrices can be useful in some cases, but those cases are very rare). Read "Help stamp out GL_PROJECTION abuse" for more detailed information about why that is.

You also need to remember that OpenGL has no notion of a "camera," per se. There's just the modelview matrix. This should contain both the modeling transformation (transforms objects from model space to world space) and the viewing transformation (transforms objects from world space to view space). Now since there's no "camera" there is only one matrix. So anything you do on the modelview matrix can be thought of as affecting either the object or the camera in some way. Now imagine a camera pointing at some object. Moving the object some distance in one direction will produce the same result as moving the camera the same distance in the opposite direction. This is why a view matrix is really the inverse of what you would think it would be if you thought of the camera as any other object.

I know this is all very confusing at first. Hopefully that helped to clear it up a little bit instead of confuse more (I'm bad at explaining things sometimes). I recommend reading the first few major sections of Chapter 3 in the Red Book (that's for OpenGL 1.1, if you have a copy of a newer version I recommend using that instead) several times until it starts to sink in. If you have any more questions you can PM me or create a new thread here and either myself or someone else will try to help you out.
Thank you, kalidor, for those links. I have actually read the text about stamping about GL_PROJECTION abuse, but then went on to read another that contradicted said text, so naturally, I was a bit confused. You mentioned that the projection matrix should only be used for glOrtho and related commands, is it ok to use those commands in my modelview matrix aswell? I kinda like drawing with pixel measurements rather that 0.2, 0.3 or 0.9 (for example).

Also, some fonts (like textuxe mapped) appear upside down, and it positions they should not. I know this is because I use the top left as (0,0), and not the bottom left as those fonts think I am using. I'm going to take a wild guess and assume that I can just switch up my glOrtho settings for drawing those, and then revert them.

This topic is closed to new replies.

Advertisement