HUD - matching touches to object?

Apprentice
Posts: 12
Joined: 2009.07
Post: #1
hi, what im tring to do is display images to be used as an HUD, ie no matter where the camera is, these objects will always be in the same position. i can do that no problem, but i want things to happen when the user touches these locations. the problem is i dont know exactly what coordinates on the screen the rasterized data fits on. is there anyway to match touches to rasterized images or a way to control where the images will sit on the 320x480 screen, or will i have to think about perspective and go through a long process to work things out manually?
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
I think you're asking the wrong question Smile
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #3
why do dogs sniff each others butts?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
check this out: http://www.lighthouse3d.com/opengl/picking/
maybe that is what you can use

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #5
Ther is no picking support in the GLES API.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Since you're not sure where your graphics are rasterizing to, I presume you're using OpenGL ES. The normal answer to this would be to look into gluUnProject, but since glu isn't included in ES, you'd have to bring your own implementation. There's one here: http://webcvs.freedesktop.org/mesa/Mesa/...iew=markup
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #7
someone must have done this before. maybe a seperate drawing mode which uses orthogonal viewpoint? am i right in thinking in using an orthogonal viewpoint the dimensions of an object dont reduce as z is further back?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
Yes, that's a common approach. The idea is pretty simple:
  • Set up perspective projection
  • Draw 3D objects
  • Clear depth buffer and/or disable depth test
  • Set up orthographic projection
  • Draw 2D overlay
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #9
ThemsAllTook Wrote:Yes, that's a common approach. The idea is pretty simple:
  • Set up perspective projection
  • Draw 3D objects
  • Clear depth buffer and/or disable depth test
  • Set up orthographic projection
  • Draw 2D overlay
right, so if i do this, will a 2d rectangle 320x by 480y fill up the whole screen when its z value is zero?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #10
cool mr croc Wrote:right, so if i do this, will a 2d rectangle 320x by 480y fill up the whole screen when its z value is zero?

Depends on how you set up your projection. To get 1-to-1 pixel correspondence, I think you want to do something like this: glOrthof(0, 320, 0, 480, -1, 1).

With orthographic projection, the z coordinate has no bearing on the projected position of a vertex. It's common to use two-component vertices (x and y) for 2D drawing in ortho mode, which implicitly sets z to 0.
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #11
thats sounds about right, cheers bruv Rasp
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #12
i think theres something i must be missing. ive got initwithcoder running setupview, drawview delcares some vertices. now ive added the HUD with the glorthof, i get a split second of things being drawn (not what i have in the hud), then its nothing but black.

Code:
- (void)drawView {

.............

glBindTexture(GL_TEXTURE_2D, textures[1]);
glVertexPointer(3, GL_FLOAT, 0, elementVerticies);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, &fontTexCoords[208]);

for (int i = 0; i < 5; i++) {
glPushMatrix();
{
  glTranslatef(-1.0, -1.0, -2.0+(i*-2.0));
  glRotatef(-90.0, 1.0, 0.0, 0.0);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
glPopMatrix();
glPushMatrix();
{
  glTranslatef(1.0, -1.0, -2.0+(i*-2.0));
  glRotatef(-90.0, 1.0, 0.0, 0.0);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
glPopMatrix();
}

[self setupOrtho];

glPushMatrix();
{
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
glPopMatrix();

[self setupView]

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
[self checkGLError:NO];
}

-(void)setupOrtho {
glDisable(GL_DEPTH_TEST);
glOrthof(0, 320, 0, 480, -1, 1);
}

- (void)setupView {
    const GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0;
    GLfloat size;
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
// This give us the size of the iPhone display
    CGRect rect = self.bounds;
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size / (rect.size.width / rect.size.height), zNear, zFar);
    glViewport(0, 0, rect.size.width, rect.size.height);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #13
glLoadIdentity() and glMatrixMode() ftw
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.07
Post: #14
DoG Wrote:glLoadIdentity() and glMatrixMode() ftw
please continue
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  detecting 2 touches, is order of touch significant? sefiroths 4 9,871 Sep 30, 2011 07:29 AM
Last Post: @iOS_blog
  Advice regarding Views &amp; Touches for iPhone game Elphaba 5 3,211 Jul 17, 2009 01:54 PM
Last Post: Frank C.