Depth buffer (z-buffer)

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

sorry, I forgot to say that what Im doing is corresponding to Augmented reality…

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.

It’s not clear what your problem is. How about posting a code snippet?

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 ) )
  • 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

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

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.

[QUOTE=menzel;1238379]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.[/QUOTE]

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,
[ATTACH=CONFIG]189[/ATTACH]

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