problem mixing 3d with 2d render

Nibbie
Posts: 2
Joined: 2010.02
Post: #1
hallo to everyone, first post here Smile

back to us... I know this may sound trivial, but I'm having problems mixing 3d and 2d render (like drawing a 2d gui "over" a rotating cube).

[edit]problem solved: I was missing a couple of glPush/PopMatrix calls. I will post the correct code later (cannot do it right now).

the strange thing is that some time ago I was able to do such thing, but maybe I've altered the wrong portion of code and nothing works as espected (and obviously I do not find what's the problem).

actually I do something like this:

Code:
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);

//clear color and depth buffer, enable depth test

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

//switch to projection

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(fScreenL, fScreenR, fScreenB, fScreenT, nearL, farL);  //where fScreenX are used to define the frustum size
  //they are correctly calculated before the very first draw procedure call

/* CALL LOOKAT */

//switch to modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);

/* DRAW 3D SCENE (with texture, lightning) */
/* basically here I've got some draw calls */
/* inside a glPushMatrix/glPopMatrix block */

/* SWITCH TO ORTHO */
/* seems I've got some problems here... */
/* the procedure should be */

glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(oScreenL, oScreenR, oScreenB, oScreenT, -1.0f, 1.0f);  //where oScreenX are used to define the size of the screen
   //in my case oScreenL = -240.0f, oScreeR = 240.0f, oScreenB = -160.0f, oScreenT = 160.0f

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;

/* DRAW 2D CONTENT (like text, GUI parts) */
/* BUT nothing happens... */
/* 2D content draw calls are inside a glPushMatrix/glPopMatrix block */

/* END OF THE DRAWING BLOCK */

what am I doing wrong? Blink

I tried to push, then pop projection and modelview, firts for the 3D stuff, then for 2D stuff, but nothing happens.

I tried not to call lookat, but nothing right happens.

I've noticed that putting glLoadIdentity after the glMatrixMode(GL_PROJECTION) call related to ortho drawing, instead of before, make text and sprite appear, but the screen size is not correct (it seems to be "locked" on the size I specify for frustum setup) and I do not think this is the right approach (it should be switch to matrix, then reset it with glLoadIdentity).

if i do not try to switch to orthogonal projection sprites and text draws correctly... but in 3D space (which is expected).

any help will be appreciated.

Code:
[correct code]
1) to3DProjectionWithCamera:cameraInfo
2) toModelView

-- draw 3d stuff

3) to2DProjectionFrom3DProjection
4) toModelView

-- draw 2d stuff

5) to3DProjectionFrom2DProjection
6) toModelView

-- back to 3d mode

1)
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(fScreenL, fScreenR, fScreenB, fScreenT, nearL, farL);
-- eventually update call lookAt

2)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

3)
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(oScreenL, oScreenR, oScreenB, oScreenT, -1.0f, 1.0f);

4)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

5)
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();

6)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

[/correct code]

saluti.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Turning off mixing iPod music dsk00 0 2,636 Jun 4, 2009 10:25 PM
Last Post: dsk00
  Mixing obj-c and c++ DesertPenguin 2 3,461 Aug 29, 2006 07:47 AM
Last Post: TomorrowPlusX
  Mixing SDL with Cocoa Taxxodium 1 2,641 Nov 22, 2003 06:28 PM
Last Post: Mars_999
  Mixing SDL & Carbon mars 2 2,947 Jul 3, 2003 07:13 AM
Last Post: aaronsullivan
  OpenGL - mixing perspective & orthographic projections mars 5 5,581 Jul 2, 2003 06:34 PM
Last Post: Iceman