Archived

This topic is now archived and is closed to further replies.

glPrint("and doing more than 1!!!");

This topic is 5723 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

Hi, im working on opengl at the moment, and have stumbled across a problem concerning the glPrint bit. I have this glPrint("Hello Well, whats up? %7.2f", cnt1); as you may now see, im taking the nehe tutorials (thanks, great site :D) but i cant get 2 lines of text at once, IE Hello Well, whats up? is what i want, what i get is Hello Well, whats up? I have tryed using /n, and just differtant combinations of / and n :D:D any help appreciated

Share this post


Link to post
Share on other sites
hmm, well my glPrintf function takes all the format specifiers printf takes.


  
void glPrintf( const char *format, ...) //used like printf to display text

{
va_list args;
va_start(args,format);
numchars+=vsprintf(GLTEXT+numchars, format, args);
va_end( args );
}


I haven''t looked at nehe''s function, but if it works using vsprintf then it should be the same. And yes, you should use "\n" not "/n"

www.elf-stone.com

Share this post


Link to post
Share on other sites
Looks like wglUseFontBitmaps won''t allow you to print text with line breaks. You have to write your own font engine.

If you''ve got DX8 SDK, take a look at D3DFont. I ported D3DFont to OpenGL and enhanced it. I can send you my class if you want to look at it, but 1) it''s a part of a larger project, and may not be usable by itself without modifications and 2) it''s long and complicated, although I tried to do a good job at commenting.

Share this post


Link to post
Share on other sites
The problem with wglUseFontBitmaps (I think) is that the display lists print whatever character they were made for and advance the current raster position to the right. If you want line breaks, you have to advance raster position downward. But since you call one list for each letter, how does the list for ''\n'' know where your string starts? It has no way of knowing it, and therefore there''s no reliable way to return back to horizontal position where string rendering started. There''s little benefit in advancing vertical coordinate for letter output without returning to the beginning horizontal position, like this:

This is\nsome text
becomes
This is
some text

So you have to write your own font class that keeps track of screen coordinates. I believe it''s possible to modify NeHe''s code to do line breaks by using glGet with raster position arguments and handing ''\n'' manually, but considering that calling a display list for every character is extremely slow, you''re better off writing a new font engine.

I hope this explains why line breaks don''t work with NeHe''s code.

Share this post


Link to post
Share on other sites
right. as i said im new to OpenGL. Its really hard to understad what your saying. I understand about moving screen co-ords, but as far as building a font engine is concerned, i wouldnt know where to begin!

Thanks for all your reply nevertheless :D

Share this post


Link to post
Share on other sites
AFAIK, handling newlines in GL is a bit more complicated than doing a ''naive'' glPrint function. You need to first save the raster position, then parse the string and decompose it in individual lines. glPrint the first line, restore the raster position, move down by the size of the font, save the raster position, lather, rinse, repeat.

Examine the code for glPrint and see if it does that.

Share this post


Link to post
Share on other sites
You shouldn't use functions you don't know HOW and WHY they work.

There are more worlds than the one that you hold in your hand...

[edited by - _DarkWIng_ on April 10, 2002 3:22:52 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So _DarkWIng_, I guess no beginners should ever use int main() then, eh?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You missed the point. Most beginners won''t know why the program starts in main, they just know it does. Heck, most won''t even know that "main" IS a function.

Share this post


Link to post
Share on other sites
Actually no, I didn''t.

They don''t need to know why or how the crt calls main, as they never manually call it themselves. They should however know what the functions they call do.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
ffs, if im gonna study the history, present and future of every command, what every possibility of that command is, why thats used, all the alternatives, maybe make a website about it etc. I wouldnt even be past the hello world program yet.

So... What do you zsuggest? When i wanna get something done quickly and i have 20 commands for example!!!!

I do intend to reuse this, slowly i learn more about it, every time i use it.

and Int Main() is used because everywhere needs somewhere defined to start i reckon

Share this post


Link to post
Share on other sites
Looking up 20 functions in MSDN doesn''t take much time and it''s worth it. You''ll just be back here asking about some stupid bug if you don''t know what your code is actually doing.

Try to relax.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Mayhaps you could try lesson33, it has a glPrint function which is much better.

Or look have a look at my NetMULE code, it also uses a enhanched version of glPrint.

[edited by - Belni on April 11, 2002 4:55:01 AM]

Share this post


Link to post
Share on other sites
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>


external int g_width;
external int g_height;


void TextOut(float x,float y,float z,void *font,char *str) {

glRasterPos3f(x,y,z);
for(char *c=str; *c != '\0'; c++) {
glutBitmapCharacter(font,*c);
}
}


void OrthoProjection(void) {

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2d(0,g_width,0,g_height);
glScalef(1.0f,-1.0f,1.0f);
glTranslatef(0.0f,-h,0.0f);
glMatrixMode(GL_MODELVIEW);
}


void ResetProjection(void) {

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}


void OglPrintf(float x,float y,int spacint,void *font,char *str) {

int x1 = x;
for(char *c=str; *c != '\0'; c++) {
glRasterPos2f(x1,y);
glutBitmapCharacter(font,*c);
x1 += glutBitmapWidth(font,*c)+spacing;
}
}


If your not using glut them I suppose you could just use glRasterPos3f to place the next line and then use your glPrint.

[edited by - MindCode on April 11, 2002 10:47:41 AM]

Share this post


Link to post
Share on other sites
I was thniking a bit differenet. Alot of people just include a bunch of code into their projects and they have no idea how and why it works. They use it for some time before they realize it would be nice to have something changed or just add some feature. But they have NO idea what to do becouse they realy don''t understand "their" code. And this is a classic example of this.
OK, you don''t need to know for jaust about every function what it does, but you should at least know for the ones you have source code for.

There are more worlds than the one that you hold in your hand...

Share this post


Link to post
Share on other sites
If you want some great bitmap font fun, just port D3DFont to OpenGL and fix as many bugs as you can find. You''ll get an excellent quality blazing speed and (hopefully) bug-free font engine.

Share this post


Link to post
Share on other sites


I rewrote nehe''s function and just made it snap into ortho and then I just use a simple glTranslatef to position the text.

my glPrint looks like glPrint(x,y,"",... );

If you havent done this and are not working in screen cords then it will be little anoying to find out where the text is gonna be but you could just do the following.

glPushMatrix();
glTranslatef(x,y,z);
glPrint("hello");
glTranslatef(x,y2, z);
glPrint("well, whats up?");
glPopMatrix();

only thing is that as you move your camera with the above your text will also move.
I have no idea how to get /n to make a new line in glPrint, never actually tried it.

I recomend using ortho mode for text though. Nehe''s function doesnt, least not the one I had downloaded.


hey can you call main() from inside main? this would be recursive but would it only quit recursing when the program exits?

Share this post


Link to post
Share on other sites