Advertisement

bitmap fonts onto bitmaps

Started by October 01, 2003 07:02 AM
15 comments, last by trager 21 years, 4 months ago
I''m going mad trying to find a way to put bitmap fonts onto bitmaps so they can be used as textures. I''ve been NeHe''s tut13 to display text straight to the screen, got a funny feeling that I wont be able to point this output to a bitmap very easily. Can anyone help point me in the right dirrection? Thanks trager
glReadPixels
use it like this

Lets say your window is called Window etc.
void glReadPixels(GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
GLvoid *pixels)

vector<GL_BYTE> pixels;
pixels.resize(GLWindow->Width*GLWindow->Height);
glReadPixels(
0,
0,
GLWindow->Width,
GLWindow->Height,
GL_RGBA,
GL_BYTE,
pixels);

// if you don't want the alpha values, you can use GL_RGB instead of GL_RGBA
// try GL_DEPTH_COMPONENT instead of GL_RGBA. lots of fun


NOTE:
most images use bytes. eg;bitmaps GL_BYTE would be appropriate, as would pngs, jpegs etc.

I use things like GL_FLOAT for image manipulation, but when saving use GL_BYTE.

ALOT of image formats are aligned to four bytes (eg; alot of bmps).
so even if you are using only GL_RGB, be careful that you haven't got the image aligned at four bytes etc.
don't worry, the image will still be viewable, just skewy and you can fix this up later on.

After you have called the function above, your bitmap is stored in "pixel";

if
void MySaveBitmapFunction(char *pixels,int width,int height)
{
//...code here
}
was your bitmap saving function, then basically something like this would save your image to a file

vector<GL_BYTE> pixels;
pixels.resize(GLWindow->Width*GLWindow->Height);
glReadPixels(
0,0,
GLWindow->Width,
GLWindow->Height,
GL_RGBA,
GL_BYTE,
pixels);
MySaveBitmapFunction(
pixels,
GLWindow->Width,
GLWindow->Height);



PS - this is off the top of my head. could be a few typos.



Beer - the love catalyst
good ol' homepage



Edit: forgot that I had to use &amplt to get < etc


[edited by - Dredge-Master on October 1, 2003 12:56:33 PM]
Beer - the love catalystgood ol' homepage
Advertisement
pps - I''m one of those people who likes vectors for arrays etc;


if you haven''t used it before, its part of the standard template library for C++ (STL).
It saves screwing around with memory management all the time.

#include <vector.h>


others aswell, like lists and so forth.
Beer - the love catalystgood ol' homepage
I got through that entire post without reading the word font or text once, so I''m kinda assuming that you are talking about something else....

which is a crying shame bcz thats a big post.

thanks anyway.

trager
it''s code that dumps the opengl image to a bitmap.

you said that you can already render the text to the opengl window, right?

then you said that you were having problems saving it to a bitmap correct?


so all you needed was code to save it to a bitmap.
Beer - the love catalystgood ol' homepage
PPPS - if you are using the bitmap in memory as a texture, just mipmap the pixel data like you would if you loaded from your bitmap loading code.
Beer - the love catalystgood ol' homepage
Advertisement
Right, Ok sorry, I don''t need to tell you I''m a Noob. I get you now.

Lets look at this an element at a time, firstly the parameters of glReadPixels.

I assume that x and y are the starting coordinates and the width and height are the dimensions of whats going to be copied, this right?

I''ve never used vectors and I''m totally at a loss over the meaning of the following:

vector pixels;
pixels.resize(GLWindow->Width*GLWindow->Height);

Could you enlighten me over that please.



Loads of questions I know but I really am that new...

Thanks in advanced for your time.

Trager

hehe - no problem mate

This will be my last post for the day, 4:30 and I have to get up at 8 to see my girlfriend off in the morning.

Anyway,

with glReadPixels

GLint x,
GLint y,
GLsizei width,
GLsizei height,


Width and Height are pretty obvious
1,1 means a 1x1 pixel. 16x16 is a 16 by 16 sized bitmap (which you already know.

You are also correct that x and y are the starting points of the image.

Note though that OpenGL uses Cartesian co-ordinates. This is the same as in maths etc for geometry.
0,0 is the lower left of the screen.


so if we had a 640x480 screen, we could use
x=0;
y=0;
width=640;
height=480;

and we would get a bitmap of our entire screen.

if we wanted to get the right top right corner of the screen only
eg the bit below marked with xs
...xxx...xxx............


we could use
x=320;
y=240;
width=320;
height=240;


The centre of the screen is at 320x200 (640/2 x 480/2)
This is the lower left of our desire image so we use this spot as x and y.

At the same time, our width and height are half the size, so it also becomes 320x240.



if we wanted the TOP LEFT of the screen we would use
xxx...xxx...............

x=0;
y=240;
width=320;
height=240;


I''m sure you get the drift

I''ll make the next post the one about the vectors
Beer - the love catalystgood ol' homepage
Vectors


Vectors are part of the STL (standard template library). It's for C++ (so it won't work in straight C - well it shouldn't, but you can usually compile any C code as C++ anyway)


Templates make things easier in general like memory management for arrays and stuff, or custom ones aswell (you can make your own etc).
Vector is a 2D array when you start with it. Well an empty one.


you start by including the header file
#include <vector.h>

below is the structure I will use as an example
typedef struct{char name[256];}TFile;now lets say instead of something like this where you either have to declare the size of the array at the start, or make it dynamic (memset, or using new) (example)   TFile file[10];//space for ten polygonscurrfile=0;void AddName(char name[256]){memcpy(file[currfile++].name,name,256);}This method will have a couple of errors if you try and add more than 10 names.Instead you can use your own memory management functions to set it up and resize it everytime you call addname.What vector does is just make it simpler, and safer (you can ever run past the end of the array and it doesn't let it leak memory - it still wills screw up, but it just returns empty values and so forth.   vector<TFile> file;  // the type goes inside the <> partvoid AddName(char name[256]){file.resize(file.size+1);memcpy(file[file.size-1].name,name,256);}

you can access a vector like an array, and you can also resize it to ## by calling file.resize[xx]. you can get the current size with file.size. Unless you declare it with a size, it starts at 0 (empty). You can also delete and insert values when you wish.


Once you get use to using them, vectors make it ALOT easier and safer to play with dynamically changing sizes of arrays.


In the example I gave you before, I use

vector pixels;
pixels.resize(GLWindow->Width*GLWindow->Height);

instead of

char pixels[GLWindow->Width*GLWindow->Height];

because you can't change the size of pixels in the second case if your window changes sizes.

you could declare it as
char *pixels;
and then used realloc() or new to change it, but its easier and safer just to call vector. involves less lines of typing (I'm lazy), and less maths.

It also avoids having to delete/deallocate the data after you finished with it since the template does it for you.

Edit: Time for bed - hope this helped. I will check it later when I get a chance incase I've confused you more and no one else helped.


[edited by - Dredge-Master on October 1, 2003 2:44:59 PM]
Beer - the love catalystgood ol' homepage
wow all makes perfect sense now thanks alot.

So are vectors some kind of linked list object?

This topic is closed to new replies.

Advertisement