2
votes

I'm quite new to openGL and I have been trying out some sample codes from a book about game programming on the iphone that I'm currently reading. One of the samples was a 3d game and I am able to successfully compile and run it on the iOS simulator (fullscreen).

The problem I have is when I run it on the actual hardware (an ipod touch 4G), the game does not go full screen (it only occupies like 1/4 of the screen). I was able to narrow the problem down to the code below:

GLint backingWidth, backingHeight;

- (BOOL) bindLayer
{
....

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

....
}

For some reason, backingWidth and backingHeight is set to 640 and 960 respectively after the calls above (I expected it to be 320 x 480 which is the iphone's screen size). On the iOS simulator and an older ipod touch (2g), it works just fine.

The code above is part of an object which subclasses UIView, and overrides the drawRect method (it is empty though). The thing that baffled me is when I removed the drawRect method (hence not overriding it), it somehow fixed the problem I described above.

Can anyone shed some light on this? Is it not safe to override a view's drawRect method when you are drawing in openGL? Why does it work on the simulator and an older ipod touch?

1

1 Answers

4
votes

You're seeing an artifact that's due to the differences in points and pixels on a newer device with a Retina display. As I describe in my answer here, UIKit does its sizing using points, which have a 1:1 mapping to pixels on the older devices, but now have a 1:2 mapping on current Retina display ones.

OpenGL ES, instead, uses pixels for its measurements, which is why you are seeing 640x960 being returned when querying its backing size. This is the size that should be used for the viewport for your OpenGL ES scene.

I'm guessing that somewhere in the code you're trying to run is something that sets the viewport for your OpenGL ES scene to be the size in points of your view or layer, rather than the appropriate size in pixels. This causes it to shrink to only one quarter of the view, like what happened in the case of my linked answer above. Look for glViewPort() and related calls, and make sure they are using the backing width and height, and not the view or layer width and height.

-drawRect: should have nothing to do with this, unless somewhere in there is code for setting the viewport size.