PDA

View Full Version : how to create an image programmatically to use with glDrawPixels()?



biot023
11-15-2006, 07:22 AM
Hi -- this is kind of a repost, to make what I was asking a little clearer!
I need to create, dynamically, some images that I will use to represent creatures in a 2D game I've written.
I've read the red book on images, but don't really get it, as I am a simple man, when all's told.
I want to generate these images & display them with glDrawPixels().
Can anyone offer any advice / pointers to tutorials / code that could help me understand more what I wish to do?
What if I had a 32x32x4 array of RGBA float or int values -- how would I make these into an image I could draw?
Thanks very much to anyone who can help!
Doug.

songho
11-15-2006, 08:12 AM
biot023,
glDrawPixels() simply copies an image data from system memory to OpenGL with given parameters; width, height, format, type, and pointer to the memory storage, then draws it on the screen.

You mentioned Ruby in the previous post, but I do not know about this script language. So here is C++ way;

// allocate image storage 32x32 pixels and RGBA
unsigned char* image[32][32][4];

// make the image red
for(int i=0; i < 32; i++) // height
{
for(int j=0; j < 32; j++) // width
{
// image are RGBA order
image[i][j][0] = (unsigned char)255; // red channel
image[i][j][1] = (unsigned char)0; // green channel
image[i][j][2] = (unsigned char)0; // blue channel
image[i][j][3] = (unsigned char)255; // alpha channel
}
}

// set where to start and draw image
glRasterPos2i(0, 0);
glDrawPixels(32, 32, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image);You can download the redbook examples from here;
http://www.sgi.com/products/software/opengl/examples/redbook/

biot023
11-15-2006, 10:30 AM
Thanks very much for that -- I shall get on w/ working it in Ruby & let you know how it goes!
I note that there is member rating on this board -- does this mean I can rate replies as a way of saying thanks?
&, in the meantime, thanks!
Doug.

biot023
11-15-2006, 01:15 PM
Hi -- I've been having a play, & I reckon a Ruby version of this could be:

image = []
1024.times{ image += [255, 0, 0, 255] }
GL.RasterPos( 0, 0 )
GL.DrawPixels(
CheckImageWidth,
CheckImageHeight,
GL::RGBA,
GL::UNSIGNED_BYTE, $checkImage.pack( "C*" ) )Not tried this, exactly, but similar stuff is working ok.
& thanks again, man!
Doug,

biot023
11-15-2006, 01:19 PM
Sorry, that should read:


image = []
1024.times{ image += [255, 0, 0, 255] }
GL.RasterPos( 0, 0 )
GL.DrawPixels(
32,
32,
GL::RGBA,
GL::UNSIGNED_BYTE, image.pack( "C*" ) )

songho
11-16-2006, 06:08 AM
It seems to me that Ruby is very neat and effective script language. At least I can read :)

I have a question about casting data type. How Ruby converts to the image list to "unsigned char" array in image.pack("C*")? I believe there is no char or byte type in Ruby, but probably only integer (32bit?). Do you need GL_INT type to call glDrawPixels()?

In most case, we use 24bit per pixel for rgb data and 32bit for rgba data, though.

biot023
11-16-2006, 07:11 AM
Hi again -- I'm far from an expert on data types in Ruby (it can make you a little lazy in some respects, I guess!), but the pack command there packs an array into a binary string.
The "C*" parameter is a template string & breaks into two parts (in this example -- it can be more complex).
The 'C' tells the pack method that we are using the packing directive for unsigned char, and the '*' is a count parameter -- obviously meaning the whole array, here.
What will be fun is I want to relearn C (and Objective C) at some point soon, so will probably be translating my more successful experiments back from Ruby for performance reasons!
Hope that's what you were asking, hope that answered it, & thanks very much again for your help!
Doug.

songho
11-16-2006, 11:23 AM
Originally posted by biot023:
The 'C' tells the pack method that we are using the packing directive for unsigned char, and the '*' is a count parameter -- obviously meaning the whole array, here.Okay, I got it now. Thanks Doug.