I was hoping someone could solve my issue using OpenGL ES on the iPhone.
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.
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 as you might have already guessed and someone may have the answer straight away but I have included the code in question.
apologize if I should have post on another board like OpenGL ES or iPhone dev forums.
#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;
}