Hello,
I am currently trying to display some bitmaps along with some 3d models
(like games that use a 2d image as background, above which characters are
drawn as 3d models).
I need to draw some bitmaps at a specific depth (for the 3d models to be
correctly depth sorted when going in front or behind this bitmap), but
the depth written in the depth buffer seems wrong, and I don't know why.
3D models are drawn with perspective:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, 4.0f/3.0f, 1.0f, 100000.0f);
and bitmaps with glOrtho:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, videoWidth, videoHeight, 0.0f, 1.0f, 100000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, (float) -bitmapDepth);
glScalef((float) bitmapWidth, (float) bitmapHeight, 1.0f);
glBegin(GL_QUADS);
glVertex2f(0.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glEnd();
So I expect any bitmap rendering to use 'bitmapDepth' as value written in
the depth buffer. It is not the case, and I have no idea what I did wrong.
Whatever the value (between 1.0f and 100000.0f for bitmapDepth), the 3d
models are always drawn behind.
Copying depth to color buffer show me the depth is in totally different
range between the bitmap and the 3d models.
--
Patrice Mandin
WWW: http://pmandin.atari.org/
Programmeur Linux, Atari
Spécialité: Développement, jeux
|
|
0
|
|
|
|
Reply
|
patmandin (2)
|
1/31/2010 11:04:33 AM |
|
Patrice Mandin wrote:
> So I expect any bitmap rendering to use 'bitmapDepth' as value written in
> the depth buffer. It is not the case, and I have no idea what I did wrong.
> Whatever the value (between 1.0f and 100000.0f for bitmapDepth), the 3d
> models are always drawn behind.
In perspective projection the depth buffer values map like 1/z, while in
orthographic mapping depth buffer values map linearily.
So what to do: Keep things in the perspecticve mode, calculate the proper
vertex positions for the backdrop, then draw this. For that you might want
to use gluUnProject - I on the other hand prefer to do it manually.
Wolfgang
--
OpenGL tip #42:
How to exactly map texture texels to screen pixels:
<http://preview.tinyurl.com/cgndc8>
|
|
0
|
|
|
|
Reply
|
Wolfgang
|
1/31/2010 1:07:36 PM
|
|
Le Sun, 31 Jan 2010 14:07:36 +0100, Wolfgang Draxinger a écrit:
Le Sun, 31 Jan 2010 14:07:36 +0100, Wolfgang Draxinger a écrit:
> Patrice Mandin wrote:
>
>> So I expect any bitmap rendering to use 'bitmapDepth' as value written
>> in the depth buffer. It is not the case, and I have no idea what I did
>> wrong. Whatever the value (between 1.0f and 100000.0f for bitmapDepth),
>> the 3d models are always drawn behind.
>
> In perspective projection the depth buffer values map like 1/z, while in
> orthographic mapping depth buffer values map linearily.
Well, after having checked that, you are plain right.
> So what to do: Keep things in the perspective mode, calculate the proper
> vertex positions for the backdrop, then draw this. For that you might
> want to use gluUnProject - I on the other hand prefer to do it manually.
I tried gluUnproject, but failed to get any correct result, because I
have the same problem as for drawing my bitmaps: giving as parameter the
right Z value as input.
GLint gluUnProject(
GLdouble winX, GLdouble winY, GLdouble winZ,
const GLdouble *model, const GLdouble *proj,
const GLint *view,
GLdouble* objX, GLdouble* objY, GLdouble* objZ);
Here I already know the objZ; this is the depth where I want my bitmap to
be written. I need to calculate the winZ (in 0.0-1.0 range) needed for
my drawing (either in perspective, or with glortho).
Of course, (bitmapDepth-zNear)/(zFar-zNear) only works for ortho
rendering.
For perspective matrix, gluPerspective() calcs objZ with ax+by+cz+dw;
(w being 1.0)
a=b=0
c=-(zNear+zFar)/(zFar-zNear),
d=(-2.0*zFar*zNear)/(zFar-zNear)
and objW with a=b=d=0, c=-1
Either with objZ, or objZ/objW I do not get a usable value for my
drawing. I am sure I forget something simple, as simply calculating the Z
value using the same perspective should give me the depth value I need.
--
Patrice Mandin
WWW: http://pmandin.atari.org/
Programmeur Linux, Atari
Spécialité: Développement, jeux
|
|
0
|
|
|
|
Reply
|
Patrice
|
2/6/2010 11:05:17 PM
|
|
Hi,
Patrice Mandin <patmandin@gmail.com> schrieb:
> Le Sun, 31 Jan 2010 14:07:36 +0100, Wolfgang Draxinger a �crit:
>>
>> In perspective projection the depth buffer values map like 1/z, while in
>> orthographic mapping depth buffer values map linearily.
>
> Well, after having checked that, you are plain right.
>
>> So what to do: Keep things in the perspective mode, calculate the proper
>> vertex positions for the backdrop, then draw this. For that you might
>> want to use gluUnProject - I on the other hand prefer to do it manually.
>
> I tried gluUnproject, but failed to get any correct result, because I
> have the same problem as for drawing my bitmaps: giving as parameter the
> right Z value as input.
>
> GLint gluUnProject(
> GLdouble winX, GLdouble winY, GLdouble winZ,
> const GLdouble *model, const GLdouble *proj,
> const GLint *view,
> GLdouble* objX, GLdouble* objY, GLdouble* objZ);
>
> Here I already know the objZ; this is the depth where I want my bitmap to
> be written. I need to calculate the winZ (in 0.0-1.0 range) needed for
> my drawing (either in perspective, or with glortho).
>
> Of course, (bitmapDepth-zNear)/(zFar-zNear) only works for ortho
> rendering.
>
> For perspective matrix, gluPerspective() calcs objZ with ax+by+cz+dw;
> (w being 1.0)
> a=b=0
> c=-(zNear+zFar)/(zFar-zNear),
> d=(-2.0*zFar*zNear)/(zFar-zNear)
>
> and objW with a=b=d=0, c=-1
>
> Either with objZ, or objZ/objW I do not get a usable value for my
> drawing. I am sure I forget something simple, as simply calculating the Z
> value using the same perspective should give me the depth value I need.
You have (c being clip space coordidates, v eye space coordinates, M the
matrices):
perspective transform: c1 = M1 * v1
othro transform c2 = M2 * v2
you also have the mapping: c1.z/c1.w <-> c2.z/c2.w
you want: v2.z/v2.w for a specific v1
So the rest should be simple maths (especially in this case, as one
could assume that there are no "strange" transforms in M1 and M2, so
c.z and c.w only depend on v.z and v.w)
Regards,
Marcel
|
|
0
|
|
|
|
Reply
|
Marcel
|
2/8/2010 8:27:31 AM
|
|
Le Mon, 08 Feb 2010 08:27:31 +0000, Marcel Heinz a écrit:
> Hi,
>
> Patrice Mandin <patmandin@gmail.com> schrieb:
>> Le Sun, 31 Jan 2010 14:07:36 +0100, Wolfgang Draxinger a écrit:
>>>
>>> In perspective projection the depth buffer values map like 1/z, while
>>> in orthographic mapping depth buffer values map linearily.
>>
>> Well, after having checked that, you are plain right.
>>
>>> So what to do: Keep things in the perspective mode, calculate the
>>> proper vertex positions for the backdrop, then draw this. For that you
>>> might want to use gluUnProject - I on the other hand prefer to do it
>>> manually.
>>
>> I tried gluUnproject, but failed to get any correct result, because I
>> have the same problem as for drawing my bitmaps: giving as parameter
>> the right Z value as input.
>>
>> GLint gluUnProject(
>> GLdouble winX, GLdouble winY, GLdouble winZ,
>> const GLdouble *model, const GLdouble *proj,
>> const GLint *view,
>> GLdouble* objX, GLdouble* objY, GLdouble* objZ);
>>
>> Here I already know the objZ; this is the depth where I want my bitmap
>> to be written. I need to calculate the winZ (in 0.0-1.0 range) needed
>> for my drawing (either in perspective, or with glortho).
>>
>> Of course, (bitmapDepth-zNear)/(zFar-zNear) only works for ortho
>> rendering.
>>
>> For perspective matrix, gluPerspective() calcs objZ with ax+by+cz+dw;
>> (w being 1.0)
>> a=b=0
>> c=-(zNear+zFar)/(zFar-zNear),
>> d=(-2.0*zFar*zNear)/(zFar-zNear)
>>
>> and objW with a=b=d=0, c=-1
>>
>> Either with objZ, or objZ/objW I do not get a usable value for my
>> drawing. I am sure I forget something simple, as simply calculating the
>> Z value using the same perspective should give me the depth value I
>> need.
>
> You have (c being clip space coordidates, v eye space coordinates, M the
> matrices):
>
> perspective transform: c1 = M1 * v1
> othro transform c2 = M2 * v2
> you also have the mapping: c1.z/c1.w <-> c2.z/c2.w you want: v2.z/v2.w
> for a specific v1
>
> So the rest should be simple maths (especially in this case, as one
> could assume that there are no "strange" transforms in M1 and M2, so c.z
> and c.w only depend on v.z and v.w)
I found this post from comp.graphics.algorithms,
http://newsgroups.derkeiler.com/Archive/Comp/
comp.graphics.algorithms/2006-02/msg00051.html
which gives the formula I need, and I suppose the one I should have come
to, if I followed your hint:
orthoZ = (zFar/(zFar-zNear)) * (1.0-(zNear/bitmapZ))
So with setting glOrtho(..,0.0,1.0) I just have to use orthoZ value to
draw my bitmaps, and the value in the depth buffer matches the 3D models.
Thanks to all.
--
Patrice Mandin
WWW: http://pmandin.atari.org/
Programmeur Linux, Atari
Spécialité: Développement, jeux
|
|
0
|
|
|
|
Reply
|
Patrice
|
2/9/2010 12:24:05 PM
|
|
|
4 Replies
1586 Views
(page loaded in 0.066 seconds)
|