0

I want to get depth buffer images that have been captured from different views of a 3D object. To do this with pyOpenGL I use the following code

def get_depth(LookAt_x, LookAt_y, LookAt_z)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    gluLookAt(LookAt_x, LookAt_y, LookAt_z, 0, 0, 0, 0, 1, 0)
    draw_object() // glBegin() for ... glvertex3f........ glEnd()
    glReadBuffer(GL_FRONT)
    depth_image = glReadPixels(0, 0, image_size, image_size, GL_DEPTH_COMPONENT, GL_FLOAT)

def draw_object(self):
    glBegin(GL_TRIANGLES)
    for tri in self.get_triangles():
        glNormal3f(tri.normal.x,tri.normal.y,tri.normal.z)
        glVertex3f(tri.points[0].x,tri.points[0].y,tri.points[0].z)
        glVertex3f(tri.points[1].x,tri.points[1].y,tri.points[1].z)
        glVertex3f(tri.points[2].x,tri.points[2].y,tri.points[2].z)
    glEnd()

code link: https://www.linux.com/blog/python-stl-model-loading-and-display-opengl

I call this function with different viewpoints LookAt_x, LookAt_y, LookAt_z. However, to draw the object every time costs too much time.

Is there a possible way, once the object is drawn, just change viewpoints to get depth images?

HYY
  • 1
  • 1
  • 3
    If you change the view, then you have to redraw. – BDL Nov 09 '17 at 14:30
  • It should not take a lot of time to draw the object repeatedly, please note that [you shouldn't be using glBegin/glEnd anyway](https://kos.gd/posts/dont-use-old-opengl/). Please rewrite your code using [vertex buffers](https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object), performance should be much better. – Kos Nov 09 '17 at 16:40
  • 1
    @BDL: "If you change the view, then you have to redraw" In general, I do agree with your statement, but in certain situations, one could also get away with re-projecting a current depth buffer for a new viewpoint and/or viewing direction. Not sure if this would be valid for this case, though. – derhass Nov 09 '17 at 20:13
  • @HYY: Your comments suggest that you use `glBegin()/glEnd()` to draw your objects, which will have a huge overhead and generally result in very poor performance (except when the vertex count is really low), so it might be possible to speed up the rendering. Also, if you only care about the depth map, there are a lot of optimizations you could do (disabling color buffer writes would be a good start). – derhass Nov 09 '17 at 20:17
  • @derhass : Thanks for your comment, I measured the running time, and it showed most of time is cost when drawing the object which using glBegin()/glEnd() to draw each vertex of the object. It takes near 0.12 seconds for a 20000 faces 3D mesh (.stl). Total time for get 42 differently viewed depth buffer is near 5.2 seconds (0.12x42=5.04). So I think speed up rendering does not help much. – HYY Nov 10 '17 at 01:04

0 Answers0