OpenGL ES view: how to orient it to landscape?
Looking for clues about orienting an OpenGL ES app in landscape, most information I found dates back from 2008, most of it refering to the early versions of the SDK. Apparently, back in the days, in the case of GL it was recommended to not rotate the view, but instead to apply the rotation as a GL transformation. 开发者_JAVA百科Is it still the case with the current SDKs? It would be so much simpler to simply rotate the window: all the touch events would be in sync with the rotation.
In other words: how to set up an OpenGL view in landscape mode?
(I'll answer my own question with the solution I found. I'll be happy to consider other answers though.)
In the CAEAGLLayer docs, Apple states (a bit clumsily) that you should make the rotation within GL itself: "When drawing landscape content on a portrait display, you should rotate the content yourself rather than using the CAEAGLLayer transform to rotate it." They don't explain why, but I've read in multiple places about a noticeable drop in performance.
Luckily I solved it with the addition of just a few lines. This is for landscape orientation right, where the home button is on the right.
glPushMatrix();
glRotatef(90, 0.0, 0.0, 1.0);
glTranslatef(0.0f, -320.0f, 0.0f );
// *** ALL RENDERING GOES HERE ***
glPopMatrix();
If you're targeting the iPad, replace -320 by -768.
I also convert the coordinates from the incoming UITouches:
int touchx = touch.y;
int touchy = viewWidth - touch.x;
Maybe, it's changed now. If you look into the latest OpenGL ES programming guide you can find the below sentence:
"In iOS 4.2 and later, the performance of Core Animation rotations of renderbuffers have been significantly improved, and are now the preferred way to rotate content between landscape and portrait mode. For best performance, ensure the renderbuffer’s height and width are each a multiple of 32 pixels."
As like this way:
[eaglLayer setAffineTransform:CGAffineTransformMakeRotation( -90 * M_PI / 180)];
But, CAEAGLLayer Class Reference mentioned "When drawing landscape content on a portrait display, you should rotate the content yourself rather than using the CAEAGLLayer transform to rotate it.". Maybe, documentation is not updated yet. Last updated time is 2008-05-19.
Just FYI who're visited this Q&A as like me.
You need to set up you're camera's matrix with the up vector on the X axis as opposed to the Y axis, which is what you would normally do. Rotating the world by 90 degrees works, but makes working with everything else difficult.
Use somthing like:
up.x = 1; // instead of up.y
up.y = 0;
up.z = 0;
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, position.x, position.y, position.z, lookAt.x, lookAt.y, lookAt.z, up.x, up.y, up.z);
精彩评论