Efficient "C++" to draw some images with glDrawPixels!

Hey All OpenGL People!

Im working on a app which is supposed to draw some images on screen, its a kind of flipbook so I need to load every image to memory than pass the data to glDrawPixels!

The problem is that I’ve been working in C before and I want to port my code to C++. My old malloc/realloc methods doesnt work good with my new C++ classes. Its also using old void pointers which is not allowed in new ISO C++. So what is best way to store the image data? Im using libtiff to read the information from tiff files…and remember everything needs to be loaded to the mem cause I really need the speed! Is there any example somewhere or something of the new “new” c++ types?

Thanks!

Mike

Use a char * to store the image.

Hey!

Thanks for the reply! Do you have a code snippet which takes a image and read its image data and puts i in a array[whcomp] array?

Tried it but I cant get it to work properly!

Some code would be very useful!

/Thanks!

Joda

Originally posted by Bob:
Use a char * to store the image.

Using libtiff to load images is easy. Here’s a simplified version of the code I use. I have removed things like error checking and handling to show you the basic idea. Not sure if it works as it is, but you shouldn’t copy-paste code anyway, you should understand it.

int width;
int height;
char *data;

TIFF *tiff = TIFFOpen("my image.tiff, “r”);
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);

data = new char[height * width * 4];

TIFFReadRGBAImage(tiff, width, height, reinterpret_cast<unsigned long *>(data));
TIFFClose(tiff);

Hey!

Very nice! Thanks!!!

Yeah, I see Im not a beginner and its like you’r saying I should understand it not just copy…will try it later but is possible to just pass a pointer to the character array to glDrawPixels?

Thanks, very nice that you supplied some code!!!

  • Joda

Btw, is it faster to use new C++ memory calls instead of using old malloc or realloc…

Originally posted by Bob:
[b]Using libtiff to load images is easy. Here’s a simplified version of the code I use. I have removed things like error checking and handling to show you the basic idea. Not sure if it works as it is, but you shouldn’t copy-paste code anyway, you should understand it.

[quote]

int width;
int height;
char *data;

TIFF *tiff = TIFFOpen("my image.tiff, “r”);
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);

data = new char[height * width * 4];

TIFFReadRGBAImage(tiff, width, height, reinterpret_cast<unsigned long *>(data));
TIFFClose(tiff);

[/b][/QUOTE]

is possible to just pass a pointer to the character array to glDrawPixels?

A pointer is the only thing it accepts. You can’t pass an array to a function by value.

Btw, is it faster to use new C++ memory calls instead of using old malloc or realloc…

Faster in what way? Time take to allocate the memory? That depends on your runtime library. After allocation, it’s just a piece of memory, so the access is the same.

Ok I see about the memory, but when it comes to assigning a char variable data, say that I’ve got not just one image, I’ve got a stream, what is the best way to declare a varible like that?

Here’s my method, suggestions?

In my class:
protected:
char *buffer;

in my constructor:
buffer = new char[whcomp]

but how do I define a array of char values with size whcomp? like … new char[streamNum][whcomp]???

And how do I assign the image data to specific instance in the array, say that I’ll store based on frames…

Thanks for all of the help!!!

/Joda

Originally posted by Bob:
[b] [quote]
is possible to just pass a pointer to the character array to glDrawPixels?

A pointer is the only thing it accepts. You can’t pass an array to a function by value.

Btw, is it faster to use new C++ memory calls instead of using old malloc or realloc…

Faster in what way? Time take to allocate the memory? That depends on your runtime library. After allocation, it’s just a piece of memory, so the access is the same.[/b][/QUOTE]

When you have loaded one frame and drawn it in OpenGL, do you need to keep it for further operations, or can you just forget it ever existed? If you can forget about it, why not allocate only one frame and overwrite it with new frames over and over again?

Hey again!

Thats one way to do it, I did some research and I think Im going to use and array of pointer, like (char *_data) _data= new char[TOTFRAMES]; and then use a simple _data[n]=new char[whCOMP] when I load in the different frames, is that a good idea? I need to store all of the frames in the buffer cause I want speed and be able to flip forward/backward etc in the sequence…

What do you think? I think its a good way to allocate memory “on the fly”…I mean somethimes each image doesnt have the same size etc…

Thanks!

/Joda

Originally posted by Bob:
When you have loaded one frame and drawn it in OpenGL, do you need to keep it for further operations, or can you just forget it ever existed? If you can forget about it, why not allocate only one frame and overwrite it with new frames over and over again?

If you need to store all frames, then your idea will work fine.

If the sequence is long, it can consume quite alot of memory. If possible, you can store only a part of the sequence at any time. Say you have a few thousand frames, you can keep only the 100 latest frames in memory (in a circular buffer or something), saving lots of memory. You can still go backwards, but only 100 frames, before you have to start reloading some old frames.

It’s a combination of memory usage, functionality and performance. Either consume lots of memory, don’t allow the user to back more than a few frames or take a performance hit when reloading frames.

Yeah, that’s true, like you’r saying the app will take lots of memory, 200 frames in preview resolution is ~100Mb which is much!

Another idea is to use a compression method which is really fast! And then uncompress data for each frame…Im usually working with max ~300 frames so I hope it will be ok withoutb it but its always if it allocates less memory!!!

Thanks for the reply!

/Joda

Originally posted by Bob:
[b]If you need to store all frames, then your idea will work fine.

If the sequence is long, it can consume quite alot of memory. If possible, you can store only a part of the sequence at any time. Say you have a few thousand frames, you can keep only the 100 latest frames in memory (in a circular buffer or something), saving lots of memory. You can still go backwards, but only 100 frames, before you have to start reloading some old frames.

It’s a combination of memory usage, functionality and performance. Either consume lots of memory, don’t allow the user to back more than a few frames or take a performance hit when reloading frames.[/b]