★ TouchArcade needs your help. Click here to support us on Patreon.

drawing HUD in opengl es

09-12-2009, 07:38 AM
#1
Joined: May 2009
Posts: 41
drawing HUD in opengl es

pleez halp. im trying to make this 3d crossword app for my dad, my mac is working again after a week of being broken, and i may have an interview at ubisoft coming up with nothing but an old 2d bat and ball game as a demo.

im trying to switch from perspective to ortho after drawing what i need to so i can set up a 2d hud to deal with touches, but as soon as i make the switch everything goes black. can you see what im doing wrong or leaving out? this code is just a test to see if i can draw a floor in 3d then 1 square as a HUD.

Code:
- (void)drawView {

/////
//this bit has vertices delcared
/////

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);
}
it displays the floor for a split second then nothing but blackness.
09-13-2009, 02:20 AM
#2
Joined: Feb 2009
Location: Sweden
Posts: 1,634
Just looked at it briefly, and one thing that could be the problem is that when you go ortho, you set z min and max to [-1, 1] and then you translate -2.0+(i*-2.0) in z.

09-15-2009, 07:03 AM
#3
Joined: Apr 2009
Location: Stockholm, Sweden
Posts: 19
I belive you are missing setting the correct matrix mode in your setupOrtho

glMatrixMode(GL_PROJECTION);
:
:
glMatrixMode(GL_MODEL);


You maybe need to check your draw and setupView methods that you are setting matrix mode correctly there too. ie are your glTranslate really working on the model stack or the projection stack in your draw method?

Last edited by epatel; 09-15-2009 at 07:06 AM.
09-15-2009, 07:16 AM
#4
Joined: May 2009
Posts: 41
Quote:
Originally Posted by epatel View Post
I belive you are missing setting the correct matrix mode in your setupOrtho

glMatrixMode(GL_PROJECTION);
:
:
glMatrixMode(GL_MODEL);


You maybe need to check your draw and setupView methods that you are setting matrix mode correctly there too. ie are your glTranslate really working on the model stack or the projection stack in your draw method?
it errors around GL_MODEL, it doesnt recognise the word.

and ive changed the -1.5 z translation to 0.0, its still doing the same thing.
09-15-2009, 07:34 AM
#5
Joined: Feb 2009
Location: Sweden
Posts: 1,634
Quote:
Originally Posted by cool mr croc View Post
it errors around GL_MODEL, it doesnt recognise the word.

and ive changed the -1.5 z translation to 0.0, its still doing the same thing.
Ah yeah, you probably need to be in model/view space, thought you did that somewhere else. And don't forget to push/pop your matrices as well. The matrix is defined as GL_MODELVIEW and not GL_MODEL.

Use http://www.khronos.org/opengles/sdk/1.1/docs/man/ extensively.
09-15-2009, 07:45 AM
#6
Joined: May 2009
Posts: 41
k this is what i hav in setupOrtho() now but it still isnt working

Code:
-(void)setupOrtho {
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glOrthof(0, 320, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
also added those last 2 function calls to setupView

09-15-2009, 08:10 AM
#7
Joined: Apr 2009
Location: Stockholm, Sweden
Posts: 19
Quote:
Originally Posted by Anders View Post
Ah yeah, you probably need to be in model/view space, thought you did that somewhere else. And don't forget to push/pop your matrices as well. The matrix is defined as GL_MODELVIEW and not GL_MODEL.

Use http://www.khronos.org/opengles/sdk/1.1/docs/man/ extensively.
Ah...sorry for the miss-spelling...
09-15-2009, 08:12 AM
#8
Joined: Feb 2009
Location: Sweden
Posts: 1,634
Quote:
Originally Posted by cool mr croc View Post
k this is what i hav in setupOrtho() now but it still isnt working

Code:
-(void)setupOrtho {
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glOrthof(0, 320, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
also added those last 2 function calls to setupView

Try disabling stuff you don't need, for instance textures and lighting, and set the color to 1111.
09-15-2009, 08:18 AM
#9
Joined: Apr 2009
Location: Stockholm, Sweden
Posts: 19
Quote:
Originally Posted by cool mr croc View Post
k this is what i hav in setupOrtho() now but it still isnt working

Code:
-(void)setupOrtho {
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glOrthof(0, 320, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
also added those last 2 function calls to setupView

What is the size and position of the square you want to draw? is it like a unit in size and positioned at origo it will be like one pixel big in the lower left corner.

Maybe you should set glOrtho() to something like

glOrthof(-2, 2, -2, 2, -2, 2);
09-15-2009, 08:28 AM
#10
Joined: May 2009
Posts: 41
the problem is nothing gets drawn after orthof() is called for the first time. and the reason im calling glOrthof(0, 320, 0, 480, -1, 1); is to match vertices to pixels on screen. my vertices at the moment are -1 to 1 for x and y with z always at 0. what should be happening is the floor should be drawing with a tiny square being drawn right in front of the screen.