#### Archived

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

# Can someone help me with glDrawPixels() function?

This topic is 5312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive been struggling with this specific problem for a while now, and Ive even posted a request for help, to which no one replied. However, I have just extremely simplified the problem and can illustrate it in about 2 lines of code. Hopefully someone has encountered this before and it would be no problem for them to tell me what I did wrong: Im using the glDrawPixels function to display a bitmap that I load in in previous code. This part works fine. When I load the bitmap from the filesystem and display it with glDrawPixels() function, it works no problem (I am loading a 32 x 32 pixel bitmap, and telling glDrawPixels that it is 32 by 32). However, for various reasons I want to be able to display a smaller subsection of the loaded bitmap. However, no matter what I try, the image gets screwed up when I try to display it. Ive found that the only way to fix it is to tell glDrawPixels that either the width OR the height of my sum-image is one less than it actually is. Ill post an example below. I could just keep telling glDrawPixels that the width or height is one less, but I dont like doing this without understand why. for example, here is some simple code to make a home-made 32 by 32 pixel image, and display it with glDrawPixels(): Note that the space I allocate for the 32 by 32 image is actually multiplied by 3. This is because Ive been dealing with 24 bit bitmaps, so each pixel actually takes 3 byes. // this code results in the picture on the RIGHT unsigned char *fedUp = new unsigned char[32*32*3]; glDrawPixels(32, 32, GL_RGB, GL_UNSIGNED_BYTE, fedUp); // why doesnt this work!? // this code results in the picture on the LEFT unsigned char *fedUp = new unsigned char[32*32*3]; glDrawPixels(31, 32, GL_RGB, GL_UNSIGNED_BYTE, fedUp); // why does this work!?

##### Share on other sites
When you''re only displaying smaller sections of a larger bitmap, you need to use the glPixelStore function to let OpenGL know how the data is aligned in memory before you try to display subsets. Because it will otherwise assume it''s all linear, which is incorrect as the raster actually has to skip pixels to display regions that are "in" from the sides of the bitmap.

// Set the width of the large bitmapglPixelStorei(GL_UNPACK_ROW_LENGTH, largeWidth);// Set x-offset of regionglPixelStorei(GL_UNPACK_SKIP_PIXELS, xOffset);// Set y-offset of regionglPixelStorei(GL_UNPACK_SKIP_ROWS, yOffset);// RasterglDrawPixels(smallWidth, smallHeight, GL_RGB, GL_UNSIGNED_BYTE, data);

Also, all rows have to be aligned on a 32-bit boundary due to bugs in Microsoft''s OpenGL implementation, but it appears you''re using widths that are multiples of 4, so I don''t think that''s the source of any problems. But try the above and see if that solves anything

##### Share on other sites
Zipster,

I might have confused you as to what I am trying to do.

quote:

Because it will otherwise assume it''s all linear, which is incorrect as the raster actually has to skip pixels to display regions that are "in" from the sides of the bitmap.

I wasnt directly trying to tell openGL to display only a portion of a bitmap. Instead, I copied a portion of the bitmap data to another new location, so that the entire new location was the data I wanted to display. Thus I should have no such problems with ''regions that are "in" from the sides of the bitmap'', since I want to display this entire new bitmap.

The example I gave in my original post above gives an extremely simple example of the problem I''m having. It has nothing to do with capturing a part of a larger bitmap or anything like that. It is just a simple use of the glDrawPixels() function, which I cant get to work. The data I was trying to display is just a chunk of bitmap data, not from some larger bitmap, becuse I wanted to leave that out for simplicity.

I incorporated the code you posted into what I was trying to do and came up with this:

// this code results in the picture on the RIGHTunsigned char *fedUp = new unsigned char[32*32*3];// Set the width of the large bitmapglPixelStorei(GL_UNPACK_ROW_LENGTH, 32); // ''large bitmap is the only bitmap// Set x-offset of regionglPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); // display the whole thing// Set y-offset of regionglPixelStorei(GL_UNPACK_SKIP_ROWS, 0); // display the whole thing// RasterglDrawPixels(32, 32, GL_RGB, GL_UNSIGNED_BYTE, fedUp); // display the whole thing

This still produces similar crappy results as in my original post!

I guess I could try loading the original bitmap, and then displaying it directly using the glPixelStorei() command, instead of copying portions of it to a different location, and then displaying the whole ''new'' bitmap, but I dont see how that would produce a different result.

I hope you can understand what I''ve said. Sometimes things are clear in my head but not so clear when I write them down ;P

##### Share on other sites
No, you were clear, I just wasn''t aware you were actually copying the data you wanted and not trying to raster directly.

In that case, there is a chance it could be a problem with the code that copies the data? I haven''t seen it so I don''t want to jump to any conclusions.

##### Share on other sites
Yea I thought that at first as well, that I might have mucked up the code copying part. Which is why I put all that mess aside for now and did something extremely simple for the time being, just to see if I could make glDrawPixels() work as I expected it to. And I could not make it work.

Instead of copying a part of the Bitmap over to another location and trying to draw that, I smiply made the data from scratch in the first place, using this code:

unsigned char *fedUp = new unsigned char[32*32*3];// the above line allocates space for a 32 x 32 pixel bitmap, at 24 bits per pixl (hence the multiply by 3)for (int i = 0 ; i < 32*32*3 ; i ++) { fedUp[i] = 255; // make all the pixels white}

and then i drew this 32 by 32 pixel area using glDrawPixels(), and It still resulted in the wierd streaky line thing I got in my original post. I dont know of any other way to simplify it more than it already is, and thats why Im confused that it still doesnt work.

[edited by - AndreTheGiant on January 5, 2004 12:32:35 AM]

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×