How can I add autorotate (landscape, portrait) to an OpenGL ES app?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi friends!

How can I add autorotate (landscape, portrait) to an OpenGL ES app?

I mean, must I use shouldAutorotateToInterfaceOrientation or something like that? I didn't see anything on UIView class.
BTW, I'm using glpaint sdk sample code.

Thanks for suggestions.

Reading this post:
http://www.idevgames.com/forum/showthread.php?t=15910
I don't know what is better, rotate my view manually with glrotate (I have to manually figure out what is my orientantion) or using any API call.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #2
Return YES for the orientations you will allow it to autorotate to, in your UIViewController.

As mentioned in that other thread, allowing autorotation presents problems for OpenGL performance. If you don't mind taking a bit of a performance hit, then you're all set. Otherwise it's a little more involved...

What I've done is (something like the following) allow it (the OS) to autorotate the view, but just before it does, I get the layer's transform matrix. Then let it rotate. Then let it stabilize for a few seconds, then transform my glView and simultaneously reset the layer's view transform to the identity matrix for best performance. When it's time to autorotate again I restore the matrix and remove my GL rotation, putting it back on the slow path temporarily for the autorotation, and then do it all over again when the autorotate completes. There are lots of subtle details that have to be taken care of along the way and it is a major-super-duper-pain-in-the-butt (I don't even remember all the little hacks I worked in). I highly recommend against it.

Personally, next time I want to do autorotation with a GLView, I am going to try mimicking the way the OS does it instead, and keep everything local to OpenGL. I don't know what all would be involved in that exactly, but I'd be willing to wager it'd be a far sight easier, and much less hackish, than using the built-in autorotation with OpenGL (efficiently).

BTW, you can poll the OS for the current orientation using [[UIDevice currentDevice] orientation]
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #3
AnotherJake Wrote:Return YES for the orientations you will allow it to autorotate to, in your UIViewController.

As mentioned in that other thread, allowing autorotation presents problems for OpenGL performance. If you don't mind taking a bit of a performance hit, then you're all set. Otherwise it's a little more involved...

What I've done is (something like the following) allow it (the OS) to autorotate the view, but just before it does, I get the layer's transform matrix. Then let it rotate. Then let it stabilize for a few seconds, then transform my glView and simultaneously reset the layer's view transform to the identity matrix for best performance. When it's time to autorotate again I restore the matrix and remove my GL rotation, putting it back on the slow path temporarily for the autorotation, and then do it all over again when the autorotate completes. There are lots of subtle details that have to be taken care of along the way and it is a major-super-duper-pain-in-the-butt (I don't even remember all the little hacks I worked in). I highly recommend against it.

Personally, next time I want to do autorotation with a GLView, I am going to try mimicking the way the OS does it instead, and keep everything local to OpenGL. I don't know what all would be involved in that exactly, but I'd be willing to wager it'd be a far sight easier, and much less hackish, than using the built-in autorotation with OpenGL (efficiently).

BTW, you can poll the OS for the current orientation using [[UIDevice currentDevice] orientation]

Thanks for reply AnotherJake!

I think I will firstly try API method, if I have a big perfomance hit, I will try the another one.

Only one question, I'm using glpaint sample and I don't see any UIViewController, I only see a UIView class and GLView (I'm a cocoa beginner). Could you tell me where is it?

Thanks.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #4
You can also do this:

Code:
glPushMatrix();
if ([Globals isInLandscapeMode])
{
    glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
    glTranslatef(0.0f, -320.0f, 0.0f);
}
    
//*****DRAW HERE*******//

[model draw];

//*****DRAW ABOVE******//

glPopMatrix();

Basically that will rotate your graphics context around the top left corner and then push it up the width of the screen. You'll also need to inverse your touch points to reflect this rotation. ( (x,y) becomes (-y,x) )
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #5
He's asking about autorotation. As you just pointed out, setting your GL transform to landscape is trivial.

riruilo Wrote:Only one question, I'm using glpaint sample and I don't see any UIViewController, I only see a UIView class and GLView (I'm a cocoa beginner). Could you tell me where is it?

I guess you'll have to hack one in then.

1) add new file... Cocoa Touch Classes->UIViewController subclass, and name it GLViewController
2) in GLViewController.m, add #import "PaintingView.h" at the top, and in the loadView method, add:

Code:
CGRect    rect = [[UIScreen mainScreen] applicationFrame];
self.view = [[PaintingView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];

and further down, make a modification:

Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr​ientation {
    return YES;
}

3) in AppController.m, add #import "GLViewController.h" at the top, and in applicationDidFinishLaunching, replace this:

Code:
//Create the OpenGL drawing view and add it to the window
drawingView = [[PaintingView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)]; // - kPaletteHeight
[window addSubview:drawingView];

with this:

Code:
GLViewController    *viewController = [[GLViewController alloc] init];
[window addSubview:[viewController view]];


You'll have to change your GL transforms and touch coordinates to suit, but this'll get you autorotating.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #6
Hi!

Thanks AnotherJake, I will try it in a few hours!


I'm curious, if I choose the other way (rotation manually my view with opengl) how should I manage touch events, I mean, should I also rotate or transform begintouch coordinates, for example? I guess I should also transform accelerometer coordinates, am I right?

Thanks a lot!
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #7
Yep, everything has to be adjusted. Actually, using either way they will have to be adjusted. The best tip I can think of for the touch coordinates is to print out coordinates in both orientations so you can compare and figure out what you have to do to adjust them to match. It's certainly not hard, but it can be confusing, so printing them out can help save your sanity. Wacko
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Attempting to add landscape IB buttons to an openGL View Madrayken 1 3,801 Oct 17, 2010 07:03 AM
Last Post: iamflimflam1
  Landscape app sometimes thinks portrait bruss14 1 2,241 Dec 30, 2009 01:32 PM
Last Post: bruss14
  Chipmunk moonbuggy dynamic landscape wonza 5 3,741 Nov 6, 2009 01:08 AM
Last Post: wonza
  iPhone opengl 2d sprites landscape mode mnorton 6 5,668 Sep 9, 2009 08:27 PM
Last Post: mnorton
  portrait opengl view while using UITabbar foobar8675 0 2,312 Mar 20, 2009 02:17 PM
Last Post: foobar8675