Rotating iPhone to landscape, mouse messed up?

I created an app which works great in portrait but when I rotate to landscape I’m getting some unexpected results. The screen renders fine but the location of my touches are orthogonal to what I actually clicked.

[Image: http://i51.tinypic.com/2cq07ya.jpg ]

The yellow dotted area from the top to the bottom was what I clicked and you can see my shapes in red rendered as if it was still in portrait perspective.

I am extremely new to opengl & SDL as you might have already guessed and someone may have the answer straight away but I have included the code in question.

The glRotateF(-90, 0,0,1) call is in the middle of the SDL_main() near the bottom of the code, this is where I have tried a lot of different solutions from other posts that said worked but I had no luck. For example after my glRotatef i tried,
glOrthof(-240.0f, 240.0f, -160.0f, 160.0f, 0.0f, 0.0f); or before the rotate glTranslatef(0.07f, 0.0f, 0.0f);

I am shooting in the dark now with no success.

Code:

#include “SDL.h”
#include “SDL_opengles.h”

#ifdef main
#undef main
#endif

extern int SDL_main(int argc, char *argv[]);

void
draw_circle(float x, float y)
{
GLfloat vertices[720];
const GLfloat xradius = 15.0f;
const GLfloat yradius = 15.0f;
for (int i = 0; i < 720; i+=2) {
vertices[i] = (cos( (3.14159265358979323846 * i / 180.0)) * xradius) + 0.0f;
vertices[i+1] = (sin( (3.14159265358979323846 * i / 180.0)) * yradius) + 0.0f;
}
glPushMatrix();
glTranslatef(x, y, 0.0);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
glPopMatrix();
}

void clear()
{
glPushMatrix();

GLuint bg_id;

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, bg_id);

glDisable(GL_BLEND);
GLfloat textureCoords[] = {
	1.0f, 0.0f,
	1.0f, 1.0f,
	0.0f, 1.0f,
	0.0f, 0.0f
};
GLfloat vertices[] = { 
	0.0f, 0.0f,
	0.0f, 0.0f,
	0.0f, 0.0f,
	0.0f, 0.0f
};
/*
 GLfloat vertices[] = { 
 0.0f, 0.0f,
 320.0f, 0.0f,
 320.0f, 480.0f,
 0.0f, 480.0f
 };
 */

glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glEnableClientState (GL_VERTEX_ARRAY);

glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
glVertexPointer (2, GL_FLOAT, 0, vertices);
glDrawArrays (GL_TRIANGLE_FAN, 0, 4);	

glDisableClientState (GL_TEXTURE_COORD_ARRAY);
glDisableClientState (GL_VERTEX_ARRAY);

glPopMatrix();

}

int
SDL_main(int argc, char *argv[])
{
srand(0);

SDL_Surface *screen;
SDL_Event event;
int deltaT;	
uint32_t time0, time1;

SDL_Init(SDL_INIT_VIDEO);		
screen = SDL_SetVideoMode(480, 320, 32, 
			SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_NOFRAME);

// Toggles off status bar
SDL_WM_ToggleFullScreen(screen);
SDL_ShowCursor( SDL_DISABLE );

glViewport(0, 0, 320, 480);

glPushMatrix();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
glOrthof(-240.0f, 240.0f, -160.0f, 160.0f, 0.0f, 0.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);	
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

glPopMatrix();

time0 = time1 = SDL_GetTicks();

do {
	while (SDL_PollEvent(&event)) {
		switch (event.type) {
				
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				if (event.button.button == SDL_BUTTON_LEFT) {
					draw_circle((float) event.button.x,
									 (float) event.button.y);
				}
				break;
		}
	}
	
	time0 = SDL_GetTicks();
	deltaT = time0 - time1;
	
	while (deltaT < (1000/60)) {
		SDL_Delay(1);
		time0 = SDL_GetTicks();
		deltaT = time0 - time1;
	}		
	time1 = time0;
	
	if (SDL_LockSurface(screen) == 0) {
		
		clear();
	
		SDL_UnlockSurface(screen);
		SDL_GL_SwapBuffers();
	}
	
} while (1);
	
SDL_Quit();

return 0;

}

[Question]

Hi,

I cannot profess knowledge about how iPhone development works, but you might
try rotating the modelview matrix by 90 instead of the projection matrix.
Put it directly after the glLoadIdentity() call. This should rotate your
world instead of doing something weird.

This isn’t really an SDL question though. Next time, ask some OpenGL
specific mailing list.

Thanks,
Ian

Actually Ian, this is a SDL question for a couple of reasons.

  1.  Handling Landscape mode has been on the iPhone task list for some
    

time.

  1.  One of the issues the poster listed was that his mouse input was
    

wrong. That should be handled by SDL, and is not an OpenGL issue.

Ken RogowayFrom: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Ian Mallett
Sent: Friday, November 05, 2010 5:16 PM
To: sdl
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?

Hi,

I cannot profess knowledge about how iPhone development works, but you might
try rotating the modelview matrix by 90 instead of the projection matrix.
Put it directly after the glLoadIdentity() call. This should rotate your
world instead of doing something weird.

This isn’t really an SDL question though. Next time, ask some OpenGL
specific mailing list.

Thanks,
Ian

Thanks guys.

Ian I did exactly that and success, thank you.

Ken, I recall reading in the forums that landscape was not really inherent in SDL like you mentioned.

I suppose the engineering is limited so I want to ask a new question please.

How can I translate my mouse click coordinates CCW 1/2PI instead?

this will probably solve my issue since I am not handling too many events.

I suppose the engineering is limited so I want to ask a new question.

How can I translate my mouse click coordinates CCW -90 instead?

this will probably solve my issue since I am not handling too many events

Assuming you are using standard iPhone resolution of 320x480 Portrait or
480x320 Landscape then to convert your Portrait mouse coordinates to
Landscape just use this:

RotatedX = MouseY

RotatedY = 320-MouseXFrom: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of wiseguy99
Sent: Saturday, November 06, 2010 10:23 AM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?

I suppose the engineering is limited so I want to ask a new question.

How can I translate my mouse click coordinates CCW -90 instead?

this will probably solve my issue since I am not handling too many events

#ifdef LANDSCAPE
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#ifdef IPAD
printf(“got to ipad”);

	glViewport(0, 0, 768, 1024);
	if (isFlipped())
		glRotatef(90, 0, 0, 1);
	else
		glRotatef(-90, 0, 0, 1);
	glOrthof(0.0, (GLfloat) 1024, (GLfloat) 768, 0.0, 0, 100.0f);

#else
glViewport(0, 0, 320, 480);
if (isFlipped())
glRotatef(90, 0, 0, 1);
else
glRotatef(-90, 0, 0, 1);
glOrthof(0.0, (GLfloat) 480, (GLfloat) 320, 0.0, 0, 100.0f);
#endif
#else
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glMatrixMode(GL_MODELVIEW);
data->glLoadIdentity();
data->glViewport(0, 0, window->w, window->h);
data->glOrthof(0.0, (GLfloat) window->w, (GLfloat) window->h, 0.0,
0, 1.0);
#endif
data->updateSize = SDL_FALSE;
}

To determine if flipped set up a notification on the status bar orientation

UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (!UIInterfaceOrientationIsLandscape(interfaceOrientation) && gInitializedGL == false)
{
	interfaceOrientation = UIInterfaceOrientationLandscapeRight;
}

// check if we flipped upside down
if (deviceOrientation == UIDeviceOrientationLandscapeLeft && interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
	interfaceOrientation = UIInterfaceOrientationLandscapeRight;
else if (deviceOrientation == UIDeviceOrientationLandscapeRight && interfaceOrientation == UIInterfaceOrientationLandscapeRight)
	interfaceOrientation = UIInterfaceOrientationLandscapeLeft;

RogueProgrammer wrote:> Assuming you are using standard iPhone resolution of 320x480 Portrait or 480x320 Landscape then to convert your Portrait mouse coordinates to Landscape just use this:

RotatedX = MouseY
RotatedY = 320-MouseX

From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On Behalf Of wiseguy99
Sent: Saturday, November 06, 2010 10:23 AM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?

I suppose the engineering is limited so I want to ask a new question.

How can I translate my mouse click coordinates CCW -90 instead?

this will probably solve my issue since I am not handling too many events

Thanks to you all for sharing very helpful bits of information I needed, every reply was very useful. Michelle, thanks for sharing your solution. :o