How does one rotate a scanned image in openGL?

Member
Posts: 321
Joined: 2004.10
Post: #1
glRotate() only works with geometric data. Correct?

I've got an image - glDrawPixels() - displayed on my screen which
is slightly off kilter. Is there a way in openGL to rotate the
image slightly?

I've googled the problem, but I get tons of chaff and no wheat.

Something like this:

GLfloat newX = localX * cos(radians) - localY * sin(radians);
GLfloat newY = localX * sin(radians) + localY * cos(radians);

is only good for object vertexes. How about the whole enchilada?

thanks.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
The easiest way would be to use the image as a texture and draw it on a rotated quad.

- Alex Diener
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
Easier, and substantially faster -- DrawPixels is sloooooooooooooow!
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #4
Ah yes. But here's the rub. My images are very large and not a power
of 2. Actually the one in question is 2047x1997

Aren't textures limited to something like 1024x1024?

Speed is not a concern as the rotation will be made only once and not
in real time.

I don't suppose Adobe would be will to share some Photoshop rotate
code Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
Just to tell you how to actually do it:

// Bind your texure
glRotatef (angle, 0, 0, 1);
glBegin (GL_QUADS);
glTexCoord2f (0, 0); glVertex2f (-10, -10);
glTexCoord2f (1, 0); glVertex2f (10, -10);
glTexCoord2f (1, 1); glVertex2f (10, 10);
glTexCoord2f (0, 1); glVertex2f (-10, 10);
glEnd();

...provided that your projection makes those coordinates visible.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
the texture size limit depends on your video card. ATI cards better than the Rage 128 can do up to 2048x2048, and NVidia cards can do 4096x4096.

ATI cards better than the Rage 128 and NVidia cards also support the EXT_texture_rectangle extension, which lets you use non-power-of-two textures if you can put up with some more restrictions (no mipmaps, no REPEAT, integer texture coordinates)

If you can't put up with those restrictions, load the image into a sub-portion of a larger, power-of-two texture, and only display the relevant section.

If your image is larger than the maximum texture size, split it into several sub-images and draw several quads.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #7
On the Mac, only the Rage128 limits you to 1024 x 1024, all the rest are 2048 and higher. Also, the Rage128 is the only one that won't allow NPOT (non power of two) textures that I'm aware of. It doesn't really matter anyway since all you have to do is be creative about how you use your textures. You could just as easily break your image up into several smaller textures and put them on quads right next to each other to fake a single seamless image. And yet another thing to be aware of is that you're only limited to NPOT textures for the initial call to glTexImage2D. After that, all you have to do is call glTexSubImage2D to update that texture with a NPOT texture. It can be confusing to wrap your mind around at first, but it's really not that complicated in the end. Rest assured, textured quads is *the* way to go with what you're doing. When I first ran into this same problem a couple years ago, I too found it bizarre that there wasn't a good way to upload directly to a frame buffer. There just isn't. Textured quads is how it's done.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
Darn it! I JUST missed OSC. I even previewed and it wasn't there. I can't edit it to delete it with this new forum theme so I guess it'll just have to be a duplicate. Sorry.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #9
Ok, you've convinced me. I can live with 1028x1028 textures until I buy a new Mini or iMac.
Actually, I can also crop or fill the image to a POT. Come on TIGER!

However, just for argument's sake, I'm having trouble visualizing the part about splitting
up the Big image into say 4 smaller ones and then rotating each one. I don't think that
will work. This is obviously not a mathematical proof, but I took and old photo of an ex
girl-friend and cut it into 4 equal parts. 4 rotations was not the same as 1 rotation of the entire photo. However, I could be wrong and it was fun cutting up the photo.

Thanks.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
clearly, you don't rotate each quad around its center. You can, however, perfectly easily position four quads, each rotated, in such a way as to reconstruct the full image.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #11
I just came across this code in google. Seems like it might allow image
rotation of an arbitrarily large image programatically.

http://www.cs.umu.se/~dva95rng/twister/

uses bmp files. My favorite Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
if you don't care about speed Rasp
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #13
Or just about anything else for that matter Rasp

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #14
OpenGL's model matrix can help you do this without using a lot of math. Notice that Fenris' code makes a quad that is centered at the origin. If you tesselate this quad, you can use the exact same single glRotate command, and each quad will be rotated appropriately. e.g.
Code:
glRotatef (angle, 0, 0, 1);
for(int i=-1;i<=1;i++){
  for(int j=-1;j<=1;j++){
    // bind texture i,j
    glBegin (GL_QUADS);
    glTexCoord2f (0, 0); glVertex2f (0+i*5, 0+j*5);
    glTexCoord2f (1, 0); glVertex2f (5+i*5, 0+j*5);
    glTexCoord2f (1, 1); glVertex2f (5+i*5, 5+j*5);
    glTexCoord2f (0, 1); glVertex2f (0+i*5,5+j*5);
    glEnd();
  }
}
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #15
Of course if you are doing this with textures, you'll have to worry about filtering artifacts at the seams between quads. Apple's sample code shows how to do this correctly (usual bloated Apple code warning applies...)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Image Textures mikey 52 25,623 Jun 30, 2009 10:42 AM
Last Post: AnotherJake
  Cube auto-rotate sakrist 2 3,098 May 5, 2009 03:31 AM
Last Post: Ingemar
  Displaying image with OpenGL and DevIL in C? leRiCl 13 9,505 Jan 23, 2007 01:25 PM
Last Post: djork
  OpenGL Preferred Image Format (BMP, TIFF, ect) Justin Brimm 7 5,067 Apr 18, 2006 03:28 PM
Last Post: arekkusu
  2D Image Generation &amp; openGL LWStrike 2 3,422 Mar 28, 2006 03:34 PM
Last Post: OneSadCookie