PDA

View Full Version : Depth buffer (z-buffer)



deduu10
06-03-2012, 09:01 AM
Hai guys, I'm trying to occlude the virtual object with the real one by means of depth buffer. btw, does anybody know how to set the depth value to the virtual object we create? and how to know the depth value of the virtual object we created?

thanks

deduu10
06-03-2012, 09:08 AM
sorry, I forgot to say that what Im doing is corresponding to Augmented reality..

Dark Photon
06-03-2012, 12:11 PM
It's not clear what your problem is. How about posting a code snippet?

menzel
06-03-2012, 12:11 PM
Hi,

I don't quite understand waht you mean by 'set the depth of the virtual object'. But occluding virtual objects by real ones in AR is pretty straight forward:

* create a virtual model of the real object (phantom object)

each frame:
* estimate the pose of the real object (marker based or markerless tracking, object detection ...)
* render the video stream as the background (in case of video see-tru)
* disable color writes ( glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ) )
* render the phantom object (only the depth will get written)
* enable color writes ( glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) )
* render virtual objects

Depending on the quality of your phantom object and your pose estimation your real object will occlude your virtual ones.

deduu10
06-04-2012, 02:01 AM
Hi,

I don't quite understand waht you mean by 'set the depth of the virtual object'. But occluding virtual objects by real ones in AR is pretty straight forward:

* create a virtual model of the real object (phantom object)

each frame:
* estimate the pose of the real object (marker based or markerless tracking, object detection ...)
* render the video stream as the background (in case of video see-tru)
* disable color writes ( glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ) )

* render the phantom object (only the depth will get written)
* enable color writes ( glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) )
* render virtual objects

Depending on the quality of your phantom object and your pose estimation your real object will occlude your virtual ones.


hi, thanks for replying.

to render "phantom" object, should I enable GL_DEPTH_TEST?

here is my snippet code

static void draw( int object, double trans[3][4] )
{
double gl_para[16];
GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash_shiny[] = {50.0};
GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};

argDrawMode3D();
argDraw3dCamera( 0, 0 );
glEnable(GL_DEPTH_TEST);
glDepthRange(0.0, 1.0);
glClearDepth(1.0);

glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);


/* load the camera transformation matrix */
argConvGlpara(trans, gl_para);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd( gl_para );


glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMatrixMode(GL_MODELVIEW);

switch( object ) {
case 0:
glTranslatef( 0.0, 0.0, 25.0 );
glutSolidSphere(40.0, 24, 24);

break;
case 1:

//glDepthMask(GL_FALSE);
glDepthFunc(GL_LESS);
glTranslatef( 0.0, 0.0, 25.0 );
glutSolidCube(50.0);
break;
case 2:

glDepthFunc(GL_GREATER);
glDepthMask(GL_FALSE);
glRotatef(90.0, 1.0, 0.0, 0.0);
glTranslatef( 0.0, 0.0, 20.0 );
glutSolidCone(25.0, 100.0, 20, 24);
glDepthMask(GL_TRUE);
break;

}

glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
}

thanks

deduu10
06-04-2012, 11:15 PM
Hi Menzel,

I have modified my code to what you said


static void draw( int object, double trans[3][4] )
{
double gl_para[16];
GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash_shiny[] = {50.0};
GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};

argDrawMode3D();
argDraw3dCamera( 0, 0 );
glEnable(GL_DEPTH_TEST);
glDepthRange(0.0, 1.0);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glClearDepth(1.0);
glClearColor(0.0, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);


/* load the camera transformation matrix */
argConvGlpara(trans, gl_para);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd( gl_para );


glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMatrixMode(GL_MODELVIEW);

switch( object ) {
case 0:
glTranslatef( 0.0, 0.0, 25.0 );
glutSolidSphere(40.0, 24, 24);

break;
case 1:

glTranslatef( 0.0, 0.0, 25.0 );
glutSolidCube(50.0);

break;
case 2:
glCallList(Torus);
//glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

//glRotatef(90.0, 1.0, 0.0, 0.0);
//glTranslatef( 0.0, 0.0, 20.0 );
//glutSolidCone(25.0, 100.0, 20, 24);
//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);



break;

}

glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
}


the result is the "phantom" object does not occlude my virtual object. How do you think? =D
thanks

menzel
06-05-2012, 01:00 AM
It's
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
not
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);

The colormask code is commented out...
Can you explain what object is supposed to be what? How do you get the camera position, ARToolkit? Are you sure the position is correct? Can you render the phantom object visible above the real object? Maybe screenshots will help.

deduu10
06-06-2012, 08:56 AM
It's
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
not
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);

The colormask code is commented out...
Can you explain what object is supposed to be what? How do you get the camera position, ARToolkit? Are you sure the position is correct? Can you render the phantom object visible above the real object? Maybe screenshots will help.

Yes, I'm using ARToolkit to get the camera position. I have modified these code and the real scene became black background as I changed glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT); to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

here is the picture,
754

here is the code


static void draw( int object, double trans[3][4] )
{
double gl_para[16];
GLfloat mat_ambient[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash[] = {0.0, 0.0, 1.0, 1.0};
GLfloat mat_flash_shiny[] = {50.0};
GLfloat light_position[] = {100.0,-200.0,200.0,0.0};
GLfloat ambi[] = {0.1, 0.1, 0.1, 0.1};
GLfloat lightZeroColor[] = {0.9, 0.9, 0.9, 0.1};

argDrawMode3D();
argDraw3dCamera( 0, 0 );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


/* load the camera transformation matrix */
argConvGlpara(trans, gl_para);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd( gl_para );


glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMatrixMode(GL_MODELVIEW);

switch( object ) {
case 0:
glTranslatef( 0.0, 0.0, 25.0 );
glutSolidSphere(40.0, 24, 24);

break;
case 1:
// My virtual object
glTranslatef( 0.0, 0.0, 25.0 );
glutSolidCube(50.0);
break;
case 2:
//Supposed to be phantom object
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glCallList(Torus);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

//glDepthFunc(GL_LESS);
//glDepthMask(GL_TRUE);

break;

}

glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
}

Here is what I want to do
http://www.youtube.com/watch?v=dbpaLdVEE0g