SDL1.3/OpenGLES and iPhone Orientation

Member
Posts: 48
Joined: 2011.03
Post: #1
Hi. I'm trying to create an app that will work in landscape mode (as opposed to portrait). I have turned the iPhone simulator around so the orientation is correct but I can't seem to get SDL or OpenGLES to display my screen correctly.

I have a couple of globals that I used that store the screen dimensions (g_ScreenWidth and g_ScreenHeight). I've tried swapping the values around and this doesn't seem to help - it just makes the portrait rendered screen look even smaller.

I've just read that way back in Jan 2010 it wasn't possible to do landscape mode "yet".. but since its over a year on, surely the 1.3 'beta' version would have been extended to do this? I saw a posting somewhere where one guy had modified the SDL lib source code to work - is this the path I will have to take? I'm drawing lines and textured triangles etc with OpenGLES.. would it be possible to do a transformation or something to map "virtual landscape" screen coordinates to the actual portrait screen coordinates?

Does anybody know what the trick is to work in landscape mode?

Here's my initialisation code:

Code:
int g_ScreenWidth = 640;
int g_ScreenHeight = 480;

void InitGL( void )                                
{
    glViewport( 0, 0, g_ScreenWidth, g_ScreenHeight );                    // Reset The Current Viewport

glRotatef(-90, 0, 0, 1);  // This doesn't do anything
  
    glMatrixMode( GL_PROJECTION );                        // Select The Projection Matrix
    glLoadIdentity();                            // Reset The Projection Matrix
  
  glOrthof( 0.0f, g_ScreenWidth, g_ScreenHeight, 0.0f, -1.0f, 1.0f );
  
    glMatrixMode( GL_MODELVIEW );                        // Select The Modelview Matrix
    glLoadIdentity();                            // Reset The Modelview Matrix

  glTranslatef(0.5, 0.5, 0);
  glShadeModel( GL_SMOOTH );
  glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
  glClear( GL_COLOR_BUFFER_BIT );
}

Thanks
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
(Apr 9, 2011 08:41 PM)SparkyNZ Wrote:  I'm drawing lines and textured triangles etc with OpenGLES.. would it be possible to do a transformation or something to map "virtual landscape" screen coordinates to the actual portrait screen coordinates?

Yes, and as far as I'm aware this is the normally recommended way to do it. Just glRotate (or equivalent) to draw, and transpose coordinates to remap touch events.
Quote this message in a reply
Member
Posts: 48
Joined: 2011.03
Post: #3
(Apr 10, 2011 08:34 AM)ThemsAllTook Wrote:  Yes, and as far as I'm aware this is the normally recommended way to do it. Just glRotate (or equivalent) to draw, and transpose coordinates to remap touch events.

I must be doing something wrong. I couldn't make any sense of it whatsoever lastnight. In fact, it wasn't until I dropped the screen size from 480x960 to 320x480 when things started to make a little more sense.

I'm still not there though - I desperately tried coding this without SDL lastnight with ObjC and still found that I couldn't get the iPhoneSimulator to behave in 480x960 mode (unless it just doesn't??) Surely the simulator will approximate to the retina resolution if its not able to display it? I had the same results on my iPhone though - I tried drawing a rectangle the size of the screen (just under) and the left and top lines are missing if I use 480x960. At the moment I'm stuck with the lower resolution.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
(Apr 10, 2011 11:44 AM)SparkyNZ Wrote:  I'm still not there though - I desperately tried coding this without SDL lastnight with ObjC and still found that I couldn't get the iPhoneSimulator to behave in 480x960 mode (unless it just doesn't??) Surely the simulator will approximate to the retina resolution if its not able to display it?

Works fine for me. Maybe it'd help if you started with the OpenGL ES iPhone application template Xcode provides, and work backward to figure out what it does differently from your application?

There's an option in the iPhone simulator to display in Retina mode, under Hardware -> Device -> iPhone (Retina).
Quote this message in a reply
Member
Posts: 48
Joined: 2011.03
Post: #5
Hi Thems. I had another go lastnight.. I changed the simulator to retina and changed my screen dimensions to 480x960 but all my graphics seems to have doubled in size.

My code draws a rectangle around the screen edges and a line through the middle of the screen width from top to bottom. It works fine in 320x480 mode but when I use the retina display and bump up my dimensions to 480x960, only a corner of the rectangle can be seen.

I've just quickly created an OpenGL ES Xcode template project as suggested. How do I change the pixel screen resolution? glGetRenderbufferParameteriv returns 320 and 480 in the code below. Is it simply a matter of calling glSetRenderbufferParameteriv with 480 and 960? (I've ran out of time to try this out and have to go to work again Sad ).

Code:
- (void)createFramebuffer
{
    if (context && !defaultFramebuffer) {
        [EAGLContext setCurrentContext:context];
        
        // Create default framebuffer object.
        glGenFramebuffers(1, &defaultFramebuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
        
        // Create color render buffer and allocate backing store.
        glGenRenderbuffers(1, &colorRenderbuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
        [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
You have to set your view's contentScaleFactor to [UIScreen mainScreen].scale. Here's how I do it (you can simplify a bit if you don't need to support pre-4.0 iOS versions):

Code:
#ifndef __IPHONE_4_0
@interface UIScreen ()
@property(nonatomic,readonly) CGFloat scale;
@end
@interface UIView ()
@property(nonatomic) CGFloat contentScaleFactor;
@end
#endif

@implementation EAGLView

- (id) initWithFrame: (CGRect) frame {
    if ((self = [super initWithFrame: frame]) != nil) {
        if ([[[UIDevice currentDevice] systemVersion] compare: @"4.0" options: NSNumericSearch] != NSOrderedAscending) {
            self.contentScaleFactor = [UIScreen mainScreen].scale;
        }
        // ...
    }
    return self;
}

// ...

@end
Quote this message in a reply
Moderator
Posts: 700
Joined: 2002.04
Post: #7
(Apr 11, 2011 11:52 AM)SparkyNZ Wrote:  [...]I use the retina display and bump up my dimensions to 480x960[...]

[off-topic] I'm probably being ignorant, but I thought the resolution of the Retina display was 640x960? Huh [/off-topic]

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 48
Joined: 2011.03
Post: #8
(Apr 11, 2011 02:12 PM)sealfin Wrote:  [off-topic] I'm probably being ignorant, but I thought the resolution of the Retina display was 640x960? Huh [/off-topic]

Yes you are correct. I made a mistake in my posting.
Quote this message in a reply
Member
Posts: 48
Joined: 2011.03
Post: #9
(Apr 11, 2011 12:48 PM)ThemsAllTook Wrote:  You have to set your view's contentScaleFactor to [UIScreen mainScreen].scale. Here's how I do it

Hi Thems.. Do you have to do something special to get your initWithFrame method to be invoked? I've tried pasting your code snippet into two different OpenGL ES projects - the second time round I created a brand new project using Xcode's OpenGL ES Application template and pasted it into the EAGLView.m file. I added a breakpoint and its not getting hit at all.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #10
Which init method is invoked will depend on how your view is instantiated. If it's deserialized from a nib, you'll want to put that code into -initWithCoder: instead of -initWithFrame:. I sometimes forget that my setup is unusual, in that I instantiate the view directly without loading a nib.
Quote this message in a reply
Member
Posts: 48
Joined: 2011.03
Post: #11
Thanks Thems. It worked great! Awesome! :-)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SDL1.3 Audio with OpenGL ES Template SparkyNZ 1 5,172 Apr 14, 2011 11:36 AM
Last Post: SparkyNZ
  For 480x960 resolution would you ditch SDL1.3? SparkyNZ 0 3,920 Apr 10, 2011 02:20 AM
Last Post: SparkyNZ
  Orientation/rotation events shyguy 1 4,507 Dec 30, 2010 04:43 PM
Last Post: reubert
  OpenGL orientation on iPad Fred9000 7 6,712 Nov 27, 2010 01:21 AM
Last Post: Fred9000
  "Simple" OpenGLES (2D) efficiency question .. for OpenGLES experts! fattoh 19 9,809 Apr 14, 2010 06:00 AM
Last Post: Skorche